Skip to content

Python Logging Module

Python Logging Module published on No Comments on Python Logging Module

I started using Python’s logging module in my Python scripts.

Below is an example on how I created a python logger object.

First I created this constructor method to ease the logger object creation.

The create_logger function receives the following parameters:

  • logName
  • This is the only parameter that is mandatory and does not have a default value.

  • logLevel=’debug’
  • This is the logging level in lower case

  • logFile=’test.log’
  • This is the log path/file were log will be written

  • stream=0
  • 0 no STDOUT, 1 print to STDOUT

Hardcoded you will see the max size of the log before it rotates. Log rotation is done using logging.handlers.RotatingFileHandler if you do not want to use log rotation you would probably need to use logging.handlers.FileHandler instead.

#my_log.py
import logging
import logging.handlers
 
def create_logger(logName, logLevel='debug', logFile='test.log', stream=0):
	LOG_FILENAME=logFile
	# Files will rotate at 200MB
	MAX_BYTES = 209715200
	# Files versions to keep
	BACKUP_COUNT = 10
	LEVELS = {'debug': logging.DEBUG,
        	  'info': logging.INFO,
	          'warning': logging.WARNING,
	          'error': logging.ERROR,
	          'critical': logging.CRITICAL}
        logLevel = LEVELS.get(logLevel, logging.NOTSET)
 
	# Set up a specific logger with our desired output level
	my_logger = logging.getLogger(logName)
	my_logger.setLevel(logLevel)
        if(logFile != 0  and logFile != ''):
		# Add the log message handler to the logger
		handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=MAX_BYTES, backupCount=BACKUP_COUNT)
	        # create formatter
	        formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
	        # add formatter to ch
	        handler.setFormatter(formatter)
		my_logger.addHandler(handler)
	# If required we print out stream, or if no logFile was specified
	if(stream != 0 or logFile == 0 or logFile == ''):
	        handler = logging.StreamHandler()
        	my_logger.addHandler(handler)
	return my_logger

This is a simple example on how the logging module is used:

#test_log.py
import sys
import my_log.py
# We create the logger, using the script name sys.arv[0] and that is the logName, can be an arbitrary string.
# Named arguments are optional, and all have default values if left undeclared
my_logger=cacti_util.create_logger(sys.argv[0],stream=1,logFile='testing.log', logLevel='debug')
 
# We log the program start
my_logger.info(sys.argv[0] + ' started')
 
i=0
try:
	while(1<2):
		i = i + 1
		my_logger.warn('i = %d' % i) # this is an example warning in the log
except:
    my_logger.exception(sys.exc_info()[0]) # logger.exception is used to print full details on python exceptions
    my_logger.info(sys.argv[0] + ' died')

This is an example of what would be written in the log.


2010-04-30 16:47:33,200 - ping.py - INFO - ping.py started
2010-04-30 16:47:33,201 - ping.py - WARNING - i = 1
2010-04-30 16:47:33,201 - ping.py - WARNING - i = 2
2010-04-30 16:47:33,201 - ping.py - WARNING - i = 3
# ... AFTER SEVERAL LINES LATER I PRESS CTRL + C TO BREAK THE LOOP
2010-04-30 16:47:33,254 - ping.py - WARNING - i = 407
2010-04-30 16:47:33,254 - ping.py - ERROR -
Traceback (most recent call last):
File "ping.py", line 14, in
my_logger.warn('i = %d' % i) # this is an example warning in the log
File "/usr/lib/python2.6/logging/__init__.py", line 1060, in warning
self._log(WARNING, msg, args, **kwargs)
File "/usr/lib/python2.6/logging/__init__.py", line 1165, in _log
self.handle(record)
File "/usr/lib/python2.6/logging/__init__.py", line 1175, in handle
self.callHandlers(record)
File "/usr/lib/python2.6/logging/__init__.py", line 1212, in callHandlers
hdlr.handle(record)
File "/usr/lib/python2.6/logging/__init__.py", line 673, in handle
self.emit(record)
File "/usr/lib/python2.6/logging/handlers.py", line 73, in emit
logging.FileHandler.emit(self, record)
File "/usr/lib/python2.6/logging/__init__.py", line 852, in emit
StreamHandler.emit(self, record)
File "/usr/lib/python2.6/logging/__init__.py", line 792, in emit
self.flush()
File "/usr/lib/python2.6/logging/__init__.py", line 754, in flush
self.stream.flush()
KeyboardInterrupt
2010-04-30 16:47:33,271 - ping.py - INFO - ping.py died

The logging module offers other features such as email logging.
Here is a nice simple example.

Leave a Reply

Your email address will not be published. Required fields are marked *