Saturday, March 21, 2020

Enable Logging In Python

Python’s logging module is part of the Python Standard Library. The logging module’s basicConfig() method 
is the quickest way to configure the desired behavior of your logger. However,  applications use file-based or 
dictionary-based logging configuration requires a slightly different configuration.

One simple way to set up file-based logging is to use a config file and fileConfig module to read the log configuration properties. 

The log config file will have a format like the one below:

[loggers]
keys=root,pipeline

[handlers]
keys=fileHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=fileHandler

[logger_pipeline]
level=DEBUG
handlers=fileHandler
qualname=pipeline
propagate=0

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=simpleFormatter
args=('./log/pipeline.log','a')

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

To make it a bit simple to enable logging inside the application, a utility module can be created to handle the logging.

import logging
from logging.config import fileConfig
class AppLogger():
   #ensure a singleton. We only want one log per applcation.
   __instance = None

   #create a static methold to get a logger instance.
   # The staticmethod() built-in function returns a static method for a given function.
   @staticmethod
   def get_applog():
       if AppLogger.__instance is None:
           AppLogger()

       return AppLogger.__instance

   def __init__(self):
       if AppLogger.__instance is not None:
           raise Exception("Only one application log should be activated at a time")
       AppLogger.__instance = AppLogger.create_applogger()

   @staticmethod
   def create_applogger():
       fileConfig('./config/applog_config.cfg', disable_existing_loggers=False)
       logger = logging.getLogger('pipeline')
       return logger

Now, in any of the modules within the application, we can involve the logger by calling the log module.

from util.log import AppLogger

Logger = AppLogger.get_applog()

class TestClass() :

    def  a_fuction():
       try:
           # codes here
       
       except Exception as e:
           Logger.error(“Error occurred {0}“.format(str(e))

         finally:
           #do something

The Python documention provides good reference on how to implement logging:


No comments: