[Python Patterns] Logging your errors

Let's face it, we make mistakes. Sometimes more than others, but in general most of us are not perfect. The code we write will also not be perfect, and will probably break at some point.

Let's face it, we make mistakes. Sometimes more than others, but in general most of us are not perfect. The code we write will also not be perfect, and will probably break at some point.  

We can log out our errors and messages to a nice file so that we can review later.  On the Enterprise side you could then forward them to Splunk or something to aggregate the messages, but for your everyday home Python developer dumping errors to log files will work nicely.  At least log something right?!

I am using the built in logging functionality within the Python language to make this happen.  I also am setting up a handler and log rotation, as most of the time I break my scripts out into separate functionality or use this pattern with Flask websites.  You could get rid of the handler and use a basic config if needed. The links below will show how to do that.

Code:

import logging
import logging.handlers
import os

# Make the output directory if it doesn't exist
if not os.path.exists('output_log'):
    os.mkdir('output_log')

# Log the things
LOG_FILENAME = 'my_super_cool_logfile.log'
logger = logging.getLogger(LOG_FILENAME)
logger.setLevel(logging.ERROR)

# Create file handler which logs event error messages
fh = logging.FileHandler('output_log/{0}'.format(LOG_FILENAME))
fh.setLevel(logging.ERROR)

# Create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)

# Add rotation based on size and 5 log files
log_rotation = logging.handlers.RotatingFileHandler('output_log/{0}'.format(LOG_FILENAME), maxBytes=5242880, backupCount=5)

# Add the handlers to logger
logger.addHandler(fh)
logger.addHandler(log_rotation)

# Log something
logger.info("Start logging now")

logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')

logger.info("Stop logging now")

Output:

You will notice that only the Error and Critical event made it into the log file. That is due to setting the level to ERROR in the code.  Change to the level that suites your needs.

Levels available:

  • CRITICAL
  • ERROR
  • WARNING
  • INFO
  • DEBUG
  • NOTSET
cat output_log/my_super_cool_logfile.log 
2019-09-08 12:11:11,962 - my_super_cool_logfile.log - ERROR - This is an error message
This is an error message
2019-09-08 12:11:11,965 - my_super_cool_logfile.log - CRITICAL - This is a critical message
This is a critical message

Links:

https://docs.python.org/3/howto/logging.html

http://zetcode.com/python/logging/

https://appdividend.com/2019/06/08/python-logging-tutorial-with-example-logging-in-python/


My blog posts tagged with "Python Patterns" are designed to be a quick look reference for some Python code snippets I use a lot.  They are written to be a quick starting point for future projects so I do not need to type as much.