How-to logging in python Python 10.11.2013

Logging introduce many advantages

  • output to multiple destinations;
  • different levels of urgency for messages;
  • save execution of process to file;
  • put a timestamp to each message.

The main parts of the logging are Logger, Handler and Formatter.

  • Loggers expose the interface that application code directly uses.
  • Handlers send the log records (created by loggers) to the appropriate destination.
  • Filters provide a finer grained facility for determining which log records to output.
  • Formatters specify the layout of log records in the final output.

Logger also has a few convenience methods defined - debug, warning, error, critical and so on, each of which has a pre-defined severity level.

Formatter is responsible for converting a LogRecord to a string which can be interpreted by either a human or an external system. The LogRecord has a number of attributes.

There are many Handlers

  • StreamHandler - logging to a stream (default, sys.stderr);
  • FileHandler - logging to disk files;
  • RotatingFileHandler - logging to disk files with support for rotating;
  • SocketHandler - logging to a streaming socket;
  • DatagramHandler - logging to a UDP socket;
  • SMTPHandler - logging to an email address;
  • SysLogHandler - logging to Unix syslog;
  • MemoryHandler - buffering records in memory until a specific trigger occurs;
  • HTTPHandler - sends events to a Web server using either GET or POST semantics.

Log to the screen using base settings

import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('debug message')

Log to file using base settings

import logging
logging.basicConfig(filename='file.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('debug message')

Log to file using object

import logging
l = logging.getLogger('boo')

fh = logging.FileHandler('boo.log')

frmt = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

l.warn('warn message')

Let's log to different places (file and screen)

import logging
l = logging.getLogger()

frmt = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

fh = logging.FileHandler('file.log')

sh = logging.StreamHandler()

l.debug('debug message')