Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python Logging Tutorial

elprup
March 05, 2013

Python Logging Tutorial

Brief introduce logging module usage

elprup

March 05, 2013
Tweet

More Decks by elprup

Other Decks in Programming

Transcript

  1. 3 Concepts: Level import logging logging.warning('Watch out!') # will print

    a message to the console logging.info('I told you so') # will not print anything WARNING:root:Watch out!
  2. 4 Concepts: Format • A string like printf style in

    C import logging logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s') logging.warning(‘%s was logged.‘, ‘this event’) 2010-12-12 11:41:42,612 WARNING this event was logged.
  3. 5 Concepts : Category • Loggers: expose the interface that

    application code directly uses. • Handlers: send the log records (created by loggers) to the appropriate destination. • Formatters: specify the layout of log records in the final output. • Filters: provide a finer grained facility for determining which log records to output.
  4. 6 Concepts : Category (cont.) • Logger – Handler 1:*

    • Handler – Formatter 1:1 • Logger – Filter 1:*
  5. 9 Usage: Configure import logging # create logger logger =

    logging.getLogger('simple_example') logger.setLevel(logging.DEBUG) # create console handler and set level to debug ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # create formatter formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # add formatter to ch ch.setFormatter(formatter) # add ch to logger logger.addHandler(ch)
  6. 12 Usage: Logging Tree • Logging property rely (also works

    for logger’s filter) import logging logging.getLogger().addHandler(logging.StreamHandler()) logging.getLogger('papaya').addHandler(logging.StreamHandler()) logging.getLogger('papaya.child').addHandler(logging.StreamHandler()) logging.getLogger('papaya.child').info('child msg') # Nothing
  7. 13 Usage: Logging Tree • Level judge – Logger ->

    Logger Filter -> Handler -> Handler Filter – (Propagate) Father’s Handler -> Handler’s Filter • Propagate
  8. 14 Usage: Exception import logging import logging try: a =

    1/0 except: logging.getLogger(‘papaya.exception’).exception('error')
  9. 15 Usage: More Context import logging class ConnInfo: def __getitem__(self,

    name): if name == 'ip': result = '192.168.0.1' else: result = self.__dict__.get(name, '?') return result def __iter__(self): keys = ['ip'] keys.extend(self.__dict__.keys()) return keys.__iter__()
  10. 16 Usage: More Context if __name__ == '__main__': a1 =

    logging.LoggerAdapter(logging.getLogger('a.b.c'), { 'ip' : '123.231.231.123' }) logging.basicConfig(level=logging.DEBUG, format='%(asctime)-15s %(name)-5s %(levelname)-8s IP: %(ip)-15s User: %(user)-8s %(message)s') a1.debug('A debug message') a1.info('An info message with %s', 'some parameters') a2 = logging.LoggerAdapter(logging.getLogger('d.e.f'), ConnInfo()) a2.info('An info message with %s', 'some parameters')
  11. 18 Current Practice: EN web server • Root Logger –

    webdebug • level DEBUG, scribe Handler • Module Logger – webaccess • level INFO, scribe Handler, propagate 0 – offersdk • level INFO, scribe Handler, propagate 0