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
[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
[15:25:03 L1 INFO] info from logger l1
[15:25:03 L1 DEBUG] debug from logger l1
misc.log.py - INFO - logging to ../../../scripts/tutorials/misc.log.9996.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.9996.log:
2019-01-21 15:25:03 CET - misc.log.py - INFO - logging to ../../../scripts/tutorials/misc.log.9996.log
2019-01-21 15:25:03 CET - misc.log.py - WARNING - warning

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