1.3.3.10. Logging utilitiesΒΆ

See also the api documentation: log

The vacumm.misc.log.Logger class provides shortcuts for logging features:
  • Some non standard levels are added (verbose and notice)
  • Logging can be customized with different configurations for messages sent to the standard output (terminal) and to a file.
  • Some configuration can be massivelly changed
  • Handlers can be shared between several loggers

Sample code using loggers:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, sys
import vacumm.misc.log as L

l1 = L.Logger(name='L1', level='info', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
l2 = L.Logger(name='L2', level='debug', format='[%(asctime)s %(name)s %(levelname)s] %(message)s', date_format='%H:%M:%S')
l1.info('info from logger l1')
l1.debug('debug from logger l1')
l2.info('info from logger l2')
l2.debug('debug from logger l2')
l1.notice('logger l1 setting config from logger l2')
l1.config(l2)
l1.info('info from logger l1')
l1.debug('debug from logger l1')

lf = '%s.%s.log'%(os.path.splitext(sys.argv[0])[0], os.getpid())
l = L.Logger(
    name=os.path.basename(sys.argv[0]),
    level='debug',
    logfile=dict(
        filePath=lf, maxFileSize=1024*50, maxFileBkp=1,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        date_format='%Y-%m-%d %H:%M:%S %Z',
        level='info'
    ),
    console=dict(
        stream=sys.stdout, colorize=False,
        format='%(name)s - %(levelname)s - %(message)s',
        #level='verbose'
    )
)
l.info('logging to %s', lf)
l.debug('debug')
l.verbose('verbose')
l.warning('warning')
with file(lf) as lff:
    l.info('content of %s:\n%s', lf, lff.read(2**20))
if os.path.isfile(lf):
    l.debug('removing log file %s', lf)
    os.remove(lf)

The code above produce the following output:

$ ../../../scripts/tutorials/misc.log.py
/local/tmp/sraynaud/miniconda2/lib/python2.7/site-packages/cmocean/tools.py:76: MatplotlibDeprecationWarning: The is_string_like function was deprecated in version 2.1.
  if not mpl.cbook.is_string_like(rgbin[0]):
[L1 INFO] info from logger l1
[L2 INFO] info from logger l2
[L2 DEBUG] debug from logger l2
[L1 NOTICE] logger l1 setting config from logger l2
[13:55:17 L1 INFO] info from logger l1
[13:55:17 L1 DEBUG] debug from logger l1
misc.log.py - INFO - logging to ../../../scripts/tutorials/misc.log.23698.log
misc.log.py - DEBUG - debug
misc.log.py - VERBOSE - verbose
misc.log.py - WARNING - warning
misc.log.py - INFO - content of ../../../scripts/tutorials/misc.log.23698.log:
2018-06-28 13:55:17 CEST - misc.log.py - INFO - logging to ../../../scripts/tutorials/misc.log.23698.log
2018-06-28 13:55:17 CEST - misc.log.py - WARNING - warning

misc.log.py - DEBUG - removing log file ../../../scripts/tutorials/misc.log.23698.log