Skip to content

Instantly share code, notes, and snippets.

@blepfo
Last active August 2, 2017 20:52
Show Gist options
  • Save blepfo/b93c14a558eb646b7fd9baca1c91f589 to your computer and use it in GitHub Desktop.
Save blepfo/b93c14a558eb646b7fd9baca1c91f589 to your computer and use it in GitHub Desktop.
Quick-start guide for using the logging Module in Python

Logging in Python 2.7

This Gist summarizes some information about using the logging module in Python 2.7. The examples here are taken from the references.

The logging module makes it easy to track what's going on in your code in a more graceful way than just putting print statements during debugging. What we can do with the logging module is create handlers which will be responsible for sending log messages to a particular destination (a file, the terminal, etc).

logging can report information at multiple levels:

  1. DEBUG
  2. INFO
  3. WARNING
  4. ERROR
  5. CRITICAL

Different handlers can be configured to report information at above a certain level. For example, we may want to log DEBUG messages to the terminal, but only care about saving messages at least as important as WARNING to a logfile.

To log messages at different levels, just use the logging function for the desired level:

logging.debug('A message used for debugging')
logging.critical('An important message')

Basic Setup

At its most basic level, we can configure a logging to work in a single file like so:

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

Now if we look at the file example.log, we will see:


DEBUG:root:This message should go to the log file
INFO:root:So should this
WARNING:root:And this, too

For more info, see Basic Logging Tutorial

Intermediate Setup

With a little bit of extra work we can configure the logger in our main script and have code in multiple different modules all log to the same destinations with their module name, the time, etc.

Essentially, we use Logging handlers to add a location where logs should be written. Each handler has a Formatter to control how the log message is printed out.

Thus, in our main script, we can configure logging by adding handlers:

logger = logging.getLogger('module_name')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(fmt='%(asctime)s - %(name)s - %(levelname)s: %(message)s',
                              datefmt='%m/%d/%Y %H:%M:%S')

# Log to output file 'experiment.log'
filehandler = logging.FileHandler(output_dir+'experiment.log')
filehandler.setLevel(logging.DEBUG)
filehandler.setFormatter(formatter)

# Log to console
streamhandler = logging.StreamHandler()
streamhandler.setLevel(logging.DEBUG)
streamhandler.setFormatter(formatter)

# Add handlers to the logger
logger.addHandler(filehandler)
logger.addHandler(streamhandler)

In order to get code in a different module to access the same logger with the same handlers. we need to call logging.getLogger('first_module_name.other_module_name).

For example,

other_module_logger = logging.getLogger('module_name.other')

For more info, see Logging Cookbook

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment