Package: logsna
_    ___   ___ ___ _  _   _
| |  / _ \ / __/ __| \| | /_\
| |_| (_) | (_ \__ \ .` |/ _ \
|____\___/ \___|___/_|\_/_/ \_\logsnais a small Python library that provides a sane log output
format.http://logsna.readthedocs.orgInstallation$ [sudo] pip install logsnaOr the bleeding edge version from the git master branch:$ [sudo] pip install git+https://github.com/rspivak/logsna.git#egg=logsnaHow to use itlogsnaprovides a custom formatter classlogsna.Formatterthat can be used in a logging config file:# sanefmt.py
import logging
import logging.config
from StringIO import StringIO

CONFIG = """\
[loggers]
keys=root

[handlers]
keys=console

[handler_console]
class=logging.StreamHandler
args=(sys.stderr,)
formatter=sane

[formatters]
keys=sane

[logger_root]
level=DEBUG
handlers=console

# Our custom formatter class
[formatter_sane]
class=logsna.Formatter
"""

config = StringIO(CONFIG)
logging.config.fileConfig(config)

log = logging.getLogger('mylogger.component1')

log.debug('debug message')
log.info('info message')
log.warning('warning message')
log.critical('critical message')
try:
    1 / 0
except:
    log.exception('Houston we have a problem')This is how to use it in code directly:import logging

import logsna

# create logger
log = logging.getLogger('mylogger.component1')
log.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create an instance of the sane formatter
formatter = logsna.Formatter()

# add our formatter to the console handler
ch.setFormatter(formatter)

# add the console handler to the logger
log.addHandler(ch)

log.debug('debug message')
log.info('info message')
log.warning('warning message')
log.critical('critical message')
try:
    1 / 0
except:
    log.exception('Houston we have a problem')The Log FormatHere is an output from the above program:DEBUG    [2012-05-21 01:59:23,686] mylogger.component1: debug message
INFO     [2012-05-21 01:59:23,686] mylogger.component1: info message
WARNING  [2012-05-21 01:59:23,686] mylogger.component1: warning message
CRITICAL [2012-05-21 01:59:23,686] mylogger.component1: critical message
ERROR    [2012-05-21 01:59:23,686] mylogger.component1: Houston we have a problem
! Traceback (most recent call last):
!   File "/home/alienoid/python/sanefmt.py", line 67, in <module>
!     1 / 0
! ZeroDivisionError: integer division or modulo by zeroThe Log Format GoalsTo be human readable as much as possibleMake it easy to use with standard Unix utilitiestailandgrepto help quickly figure out why things are going southThe Log Format NotesAll timestamps are inISO8601andUTCformatTo grep for messages of a specific level$ tail -f sanefmt.log | grep '^INFO'To grep for messages from a particular logger$ tail -f sanefmt.log | grep 'component1:'To pull out full exception tracebacks with a corresponding log message$ tail -f sanefmt.log | grep -B 1 '^\!'The output of the above command will look like thisERROR    [2012-05-21 01:59:23,686] mylogger.component1: Houston we have a problem
! Traceback (most recent call last):
!   File "fmttest.py", line 72, in <module>
!     1 / 0
! ZeroDivisionError: integer division or modulo by zeroEnhancing The Log Format OutputHere is the format string used byLogsnaformatter:'%(levelname)-8s [%(asctime)s] %(name)s: %(message)s'You can explicitly specify it in your configuration file usingformatdirective# Our custom formatter class
[formatter_sane]
format=%(levelname)-8s [%(asctime)s] %(name)s: %(message)s
class=logsna.FormatterAnd you can also enhance the format string by adding your custom
attributes to it if you need to.
For a set of predefined log record attributes seehereAcknowledgmentsRelease It!LogulaLicenseCopyright (c) 2012 Ruslan SpivakPublished under The MIT License, see LICENSEChange History1.2 (2012-10-02)Python 2.6.x compatibility1.1 (2012-05-21)Typo in setup.py:https://github.com/rspivak/logsna/pull/11.0 (2012-05-21)Public release