logsna

A small Python library that provides a sane log output format
Download

logsna Ranking & Summary

Advertisement

  • Rating:
  • License:
  • MIT/X Consortium Lic...
  • Price:
  • FREE
  • Publisher Name:
  • Ruslan Spivak
  • Publisher web site:
  • http://github.com/rspivak/

logsna Tags


logsna Description

logsna is a small Python library that provides a sane log output format.http://logsna.readthedocs.orgInstallation pip install logsnaOr the bleeding edge version from the git master branch: pip install git+https://github.com/rspivak/logsna.git#egg=logsnaHow to use itlogsna provides a custom formatter class logsna.Formatter that can be used in a logging config file:# sanefmt.pyimport loggingimport logging.configfrom StringIO import StringIOCONFIG = """\keys=rootkeys=consoleclass=logging.StreamHandlerargs=(sys.stderr,)formatter=sanekeys=sanelevel=DEBUGhandlers=console# Our custom formatter classclass=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 / 0except: log.exception('Houston we have a problem')This is how to use it in code directly:import loggingimport logsna# create loggerlog = logging.getLogger('mylogger.component1')log.setLevel(logging.DEBUG)# create console handler and set level to debugch = logging.StreamHandler()ch.setLevel(logging.DEBUG)# create an instance of the sane formatterformatter = logsna.Formatter()# add our formatter to the console handlerch.setFormatter(formatter)# add the console handler to the loggerlog.addHandler(ch)log.debug('debug message')log.info('info message')log.warning('warning message')log.critical('critical message')try: 1 / 0except: log.exception('Houston we have a problem')The Log FormatHere is an output from the above program:DEBUG mylogger.component1: debug messageINFO mylogger.component1: info messageWARNING mylogger.component1: warning messageCRITICAL mylogger.component1: critical messageERROR 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 Goals1. To be human readable as much as possible2. Make it easy to use with standard Unix utilities tail and grep to help quickly figure out why things are going southThe Log Format Notes1. All timestamps are in ISO8601 and UTC format2. To grep for messages of a specific level tail -f sanefmt.log | grep '^INFO'3. To grep for messages from a particular logger tail -f sanefmt.log | grep 'component1:'4. To pull out full exception tracebacks with a corresponding log message tail -f sanefmt.log | grep -B 1 '^\!'Product's homepage


logsna Related Software