Python logging — a basic implementation
Python logging — a basic implementation
Published at: 06/11/2019
Introduction
Generating appropriate log is a primary necessity in a software application. The main benefits of a well configured log output in comparison of print statements are such as -
-
Provides diagnostic details like module name, line number, traceback, loglevel etc
-
Log stream can be directed to various output like file, central logging system, container log etc
-
Loglevel can be modified to control the log output, without modifying code & restarting application.
Python Configuration
Below code create a basic logging configuration using python standard ‘Logging’ package.
import logging
loglevel = os.environ.get('loglevel','DEBUG').upper()
logging.basicConfig(filename='logfile.log',
format='%(asctime)s %(name)-8s %(levelname)-8s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',
level=loglevel)
Here we are setting some common options like log file name, attributes of log output, datetime format, loglevel. We are getting the loglevel from environment variable instead of providing fixed value.
Python logging package have following loglevel — DEBUG, INFO, WARNING, ERROR, CRITICAL, to be used accordingly. By setting the loglevel from environment variable, we can use the below workflow -
- In a piece of code, we write a debug level log message to track an expected output
logging.debug('Running for loop with {} value'.format(value)) - In a exception case, we catch the exception and generate a ERROR level log
logging.error('Exception occurred, unable to process for {} value'.format(value)) - Now, in development stage we would want to view both the logs. However in production, we will only be interested in the
ERRORlog. So, after deployment, we simply change the loglevel by runningexport loglevel=ERRORwhich will limit the log messages toERRORtype only. Again, if we want to debug the production application and want to view theDEBUGlevel as well, we can simple change the loglevel, without making any code or application state updates.