imcflibs.log

Common logging singleton for the package.

Example
>>> from log import LOG as log

From there on a logger is available for usage with e.g. log.warn(), even if the import statement from above happens in multiple places across modules, it will always use the same logger instance (that "singleton" functionality is built into the logging module, we just do the setup here). This can easily be checked by looking at the log handlers in the different modules.

The logging levels, in increasing order of importance, are:

10 DEBUG 20 INFO 30 WARN 40 ERROR 50 CRITICAL

 1"""Common logging singleton for the package.
 2
 3Example
 4-------
 5>>> from log import LOG as log
 6
 7From there on a logger is available for usage with e.g. log.warn(), even if the
 8import statement from above happens in multiple places across modules, it will
 9always use the same logger instance (that "singleton" functionality is built
10into the logging module, we just do the setup here). This can easily be checked
11by looking at the log handlers in the different modules.
12
13The logging levels, in increasing order of importance, are:
14
1510 DEBUG
1620 INFO
1730 WARN
1840 ERROR
1950 CRITICAL
20"""
21
22
23import logging
24
25
26LOG = logging.getLogger(__name__)
27
28
29def enable_console_logging():
30    """Add a stream handler logging to the console.
31
32    Returns
33    -------
34    logging.StreamHandler
35    """
36    stream_handler = logging.StreamHandler()
37    LOG.addHandler(stream_handler)
38    return stream_handler
39
40
41def enable_file_logging(fname, mode="a"):
42    """Add a logging handler writing to a file.
43
44    Returns
45    -------
46    logging.FileHandler
47    """
48    file_handler = logging.FileHandler(fname, mode=mode)
49    LOG.addHandler(file_handler)
50    return file_handler
51
52
53def set_loglevel(verbosity):
54    """Calculate the default loglevel and set it accordingly.
55
56    This is a convenience function that wraps the calculation and setting of
57    the logging level. The way our "log" module is currently built (as a
58    singleton), there is no obvious better way to have this somewhere else.
59
60    Example
61    -------
62    This will set the loglevel to DEBUG:
63    >>> set_loglevel(2)
64
65    This will set the loglevel to INFO:
66    >>> set_loglevel(1)
67
68    To set the verbosity level when you're e.g. using argparse to count the
69    number of occurences of '-v' switches on the commandline into a variable
70    'verbosity', this code can be used:
71    >>> log.set_loglevel(args.verbosity)
72    """
73    # default loglevel is 30 while 20 and 10 show more details
74    loglevel = (3 - verbosity) * 10
75    LOG.setLevel(loglevel)
def enable_console_logging():
30def enable_console_logging():
31    """Add a stream handler logging to the console.
32
33    Returns
34    -------
35    logging.StreamHandler
36    """
37    stream_handler = logging.StreamHandler()
38    LOG.addHandler(stream_handler)
39    return stream_handler

Add a stream handler logging to the console.

Returns
  • logging.StreamHandler
def enable_file_logging(fname, mode='a'):
42def enable_file_logging(fname, mode="a"):
43    """Add a logging handler writing to a file.
44
45    Returns
46    -------
47    logging.FileHandler
48    """
49    file_handler = logging.FileHandler(fname, mode=mode)
50    LOG.addHandler(file_handler)
51    return file_handler

Add a logging handler writing to a file.

Returns
  • logging.FileHandler
def set_loglevel(verbosity):
54def set_loglevel(verbosity):
55    """Calculate the default loglevel and set it accordingly.
56
57    This is a convenience function that wraps the calculation and setting of
58    the logging level. The way our "log" module is currently built (as a
59    singleton), there is no obvious better way to have this somewhere else.
60
61    Example
62    -------
63    This will set the loglevel to DEBUG:
64    >>> set_loglevel(2)
65
66    This will set the loglevel to INFO:
67    >>> set_loglevel(1)
68
69    To set the verbosity level when you're e.g. using argparse to count the
70    number of occurences of '-v' switches on the commandline into a variable
71    'verbosity', this code can be used:
72    >>> log.set_loglevel(args.verbosity)
73    """
74    # default loglevel is 30 while 20 and 10 show more details
75    loglevel = (3 - verbosity) * 10
76    LOG.setLevel(loglevel)

Calculate the default loglevel and set it accordingly.

This is a convenience function that wraps the calculation and setting of the logging level. The way our "log" module is currently built (as a singleton), there is no obvious better way to have this somewhere else.

Example

This will set the loglevel to DEBUG:

>>> set_loglevel(2)

This will set the loglevel to INFO:

>>> set_loglevel(1)

To set the verbosity level when you're e.g. using argparse to count the number of occurences of '-v' switches on the commandline into a variable 'verbosity', this code can be used:

>>> log.set_loglevel(args.verbosity)