Logging on Python 2.7

Something actually useful… logging! Not talking about lumber but logging code issues, data, access, anything useful in an application environment. Python makes this super easy!

Logging is part of the Python standard library and can be imported with the following line of code.

import logging


First step after importing the library is to initiate the logger. 

log = logging.getLogger(__name__) #create a log object

The logger name hierarchy is analogous/identical to the Python package hierarchy if you organize your loggers on the recommended per-module basis. This is why you should use “__name__” which is the module’s name in the Python package namespace.

After creating a log, now the level needs to be specified. There are different declared levels of severity for logging. Below is a list of these levels ordered from least to most severe.

  • DEBUG – Detailed information for diagnosing problems.
  • INFO – Confirmation that things are working as expected.
  • WARNING – Notice that something unexpected happened, however the software is still working as expected.
  • ERROR – Data on an issue that has happened, which has preventing a function from being performed.
  • CRITICAL – Information about a serious problem! The program may not even run at all with this going on!

Items are logged based on the level of severity you set for the log. The default level is “warning”, this will track warning, error and critical items. If you selected debug, everything on up would be tracked.

Remember, you are the one who determines the level for a log message, so say you create a debug warning but your logging level is set to error. The debug message will not be printed because it is not of the error severity or higher.

log.setLevel(logging.<LOGGING LEVEL>)

Formats the log all pretty. This format, places the level first followed by the message I choose to insert.

formatter = logging.Formatter('[%(levelname)s] %(message)s')

This creates a handler for logging everything to a specified file.

handler = logging.FileHandler(<FILE PATH>)

Set the file handler with the format specified earlier.


Enforce the log to use the handler.



All together

#!/usr/bin/env python
import logging
log = logging.getLogger(__name__)
log.setLevel(logging.error) #error level
formatter = logging.Formatter('[%(levelname)s] %(message)s')
handler = logging.FileHandler('error.log') #log to file error.log

Implementation – Logging Items

It’s simple, just use the following syntax wherever you want a log entry to be made in your code.



logging.info('This is awesome!')

I like to use logging to report errors from “try and except” statements in a situation where I don’t want the loop to stop because of an error.
while 1:
  except, Exception E:
                  logging.error(str(datetime.datetime.now())+" "+e) #I’ll add the timestamp with datetime for kicks

There you have it, very useful and simple python programming. More documentation at http://docs.python.org/2/howto/logging.html.