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.
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 -
logging.debug('Running for loop with {} value'.format(value))
logging.error('Exception occurred, unable to process for {} value'.format(value))
ERROR
log. So, after deployment, we simply change the loglevel by running export loglevel=ERROR
which will limit the log messages to ERROR
type only. Again, if we want to debug the production application and want to view the DEBUG
level as well, we can simple change the loglevel, without making any code or application state updates.