IKH

Advance Logging

Advanced logging Module Features: Logger

  • Logger is more advanced than basic logging.
  • It is highly recommended to use and it provides several extra features.

Steps for Advanced Logging

  • Creation of Logger object and set log level
    logger = logging.getLogger(‘demologger’)
    logger.setLevel(logging.INFO)
  • Creation of Handler object and set log level
    There are several types of Handlers like StreamHandler, FileHandler etc
    consoleHandler = logging.StreamHandler()
    consoleHandler.setLevel(logging.INFO)

Note

  • If we use StreamHandler then log messages will be printed to console
  • Creation of Formatter object
    formatter = logging.Formatter(‘%(asctime)s – %(name)s – %(levelname)s: %(message)s’,
    datefmt=’%d/%m/%Y %I:%M:%S %p’)
  • Add Formatter to Handler
    consoleHandler.setFormatter(formatter)
  • Add Handler to Logger
    logger.addHandler(consoleHandler)
  • Write messages by using logger object and the following methods
    logger.debug(‘debug message’)
    logger.info(‘info message’)
    logger.warn(‘warn message’)
    logger.error(‘error message’)
    logger.critical(‘critical message’)

Note

  • Bydefault logger will set to WARNING level. But we can set our own level based on our
  • requirement.
  • logger = logging.getLogger(‘demologger’)
  • logger.setLevel(logging.INFO)
  • logger log level by default available to console and file handlers. If we are not satisfied with logger
  • level, then we can set log level explicitly at console level and file levels.
  • consoleHandler = logging.StreamHandler()
  • consoleHandler.setLevel(logging.WARNING)
  • fileHandler=logging.FileHandler(‘abc.log’,mode=’a’)
  • fileHandler.setLevel(logging.ERROR)

Note

  • console and file log levels should be supported by logger. i.e logger log level should be lower than
  • console and file levels. Otherwise only logger log level will be considered.

Example

Python
logger==>DEBUG console===>INFO ------->Valid and INFO will be considered
logger==>INFO console===>DEBUG ------->Invalid and only INFO will be considered to the 
console.

Demo Program for Console Handler

Python
import logging 
class LoggerDemoConsole: 
 
def testLog(self): 
logger = logging.getLogger('demologger') 
logger.setLevel(logging.INFO) 
 
consoleHandler = logging.StreamHandler() 
consoleHandler.setLevel(logging.INFO) 
 
formatter = logging.Formatter('%(asctime)s - %(name)s -
%(levelname)s: %(message)s', 
datefmt='%m/%d/%Y %I:%M:%S %p') 
 
consoleHandler.setFormatter(formatter) 
logger.addHandler(consoleHandler) 
 
logger.debug('debug message') 
logger.info('info message') 
logger.warn('warn message') 
logger.error('error message') 
logger.critical('critical message') 
 
demo = LoggerDemoConsole() 
demo.testLog() 

Output

PowerShell
D:\durgaclasses>py loggingdemo3.py
06/18/2018 12:14:15 PM - demologger - INFO: info message
06/18/2018 12:14:15 PM - demologger - WARNING: warn message
06/18/2018 12:14:15 PM - demologger - ERROR: error message
06/18/2018 12:14:15 PM - demologger - CRITICAL: critical message

Note

  • If we want to use class name as logger name then we have to create logger object as follows
  • logger = logging.getLogger(LoggerDemoConsole.name)
  • In this case output is:
  • D:\durgaclasses>py loggingdemo3.py
  • 06/18/2018 12:21:00 PM – LoggerDemoConsole – INFO: info message
  • 06/18/2018 12:21:00 PM – LoggerDemoConsole – WARNING: warn message
  • 06/18/2018 12:21:00 PM – LoggerDemoConsole – ERROR: error message
  • 06/18/2018 12:21:00 PM – LoggerDemoConsole – CRITICAL: critical message

Demo Program for File Handler

Python
import logging 
class LoggerDemoConsole: 
 
def testLog(self): 
logger = logging.getLogger('demologger') 
logger.setLevel(logging.INFO) 
 
fileHandler = logging.FileHandler('abc.log',mode='a') 
fileHandler.setLevel(logging.INFO) 
 
formatter = logging.Formatter('%(asctime)s - %(name)s -
%(levelname)s: %(message)s', 
datefmt='%m/%d/%Y %I:%M:%S %p') 
 
fileHandler.setFormatter(formatter) 
logger.addHandler(fileHandler) 
 
logger.debug('debug message') 
logger.info('info message') 
logger.warn('warn message') 
logger.error('error message') 
logger.critical('critical message') 
 
demo = LoggerDemoConsole() 
demo.testLog() 

Output

abc.log

PowerShell
07/05/2018 08:58:04 AM - demologger - INFO: info message
07/05/2018 08:58:04 AM - demologger - WARNING: warn message
07/05/2018 08:58:04 AM - demologger - ERROR: error message
07/05/2018 08:58:04 AM - demologger - CRITICAL: critical message

Logger with Configuration File

  • In the above program, everything we hard coded in the python script. It is not a good
  • programming practice. We will configure all the required things inside a configuration file and we
  • can use this file directly in our program.
  • logging.config.fileConfig(‘logging.conf’)
  • logger = logging.getLogger(LoggerDemoConf.name)

Note

  • The extension of the file need not be conf. We can use any extension like txt or durga etc

logging.conf

  • [loggers]
  • keys=root,LoggerDemoConf
  • [handlers]
  • keys=fileHandler
  • [formatters]
  • keys=simpleFormatter
  • [logger_root]
  • level=DEBUG
  • handlers=fileHandler
  • [logger_LoggerDemoConf]
  • level=DEBUG
  • handlers=fileHandler
  • qualname=demoLogger
  • [handler_fileHandler]
  • class=FileHandler
  • level=DEBUG
  • formatter=simpleFormatter
  • args=(‘test.log’, ‘w’)
  • [formatter_simpleFormatter]
  • format=%(asctime)s – %(name)s – %(levelname)s – %(message)s
  • datefmt=%m/%d/%Y %I:%M:%S %p

test.py

Python
import logging 
import logging.config 
class LoggerDemoConf(): 
 
def testLog(self): 
logging.config.fileConfig('logging.conf') 
logger = logging.getLogger(LoggerDemoConf.__name__) 
 
logger.debug('debug message') 
logger.info('info message') 
logger.warn('warn message') 
logger.error('error message') 
logger.critical('critical message') 
 
demo = LoggerDemoConf() 
demo.testLog() 

Output

test.log

PowerShell
06/18/2018 12:40:05 PM - LoggerDemoConf - DEBUG - debug message
06/18/2018 12:40:05 PM - LoggerDemoConf - INFO - info message
06/18/2018 12:40:05 PM - LoggerDemoConf - WARNING - warn message
06/18/2018 12:40:05 PM - LoggerDemoConf - ERROR - error message
06/18/2018 12:40:05 PM - LoggerDemoConf - CRITICAL - critical message
  • Case-1: To set log level as INFO:
  • [handler_fileHandler]
  • class=FileHandler
  • level=INFO
  • formatter=simpleFormatter
  • args=(‘test.log’, ‘w’)
  • Case-2: To set Append Mode:
  • [handler_fileHandler]
  • class=FileHandler
  • level=INFO
  • formatter=simpleFormatter
  • args=(‘test.log’, ‘a’)

Creation of Custom Logger

customlogger.py

Python
import logging 
import inspect 
def getCustomLogger(level): 
# Get Name of class/method from where this method called 
loggername=inspect.stack()[1][3] 
logger=logging.getLogger(loggername) 
logger.setLevel(level) 
 
fileHandler=logging.FileHandler('abc.log',mode='a') 
fileHandler.setLevel(level) 
 
formatter = logging.Formatter('%(asctime)s - %(name)s -
%(levelname)s: %(message)s',datefmt='%m/%d/%Y %I:%M:%S %p') 
fileHandler.setFormatter(formatter) 
logger.addHandler(fileHandler) 
 
return logger 

test.py

Python
import logging 
from customlogger import getCustomLogger 
class LoggingDemo: 
def m1(self): 
logger=getCustomLogger(logging.DEBUG) 
logger.debug('m1:debug message') 
logger.info('m1:info message') 
logger.warn('m1:warn message') 
logger.error('m1:error message') 
logger.critical('m1:critical message') 
def m2(self): 
logger=getCustomLogger(logging.WARNING) 
logger.debug('m2:debug message') 
logger.info('m2:info message') 
logger.warn('m2:warn message') 
logger.error('m2:error message') 
logger.critical('m2:critical message') 
def m3(self): 
logger=getCustomLogger(logging.ERROR) 
logger.debug('m3:debug message') 
logger.info('m3:info message') 
logger.warn('m3:warn message') 
logger.error('m3:error message')
logger.critical('m3:critical message') 
 
l=LoggingDemo() 
print('Custom Logger Demo') 
l.m1() 
l.m2() 
l.m3()

Output

abc.log

Python
06/19/2018 12:17:19 PM - m1 - DEBUG: m1:debug message
06/19/2018 12:17:19 PM - m1 - INFO: m1:info message
06/19/2018 12:17:19 PM - m1 - WARNING: m1:warn message
06/19/2018 12:17:19 PM - m1 - ERROR: m1:error message
06/19/2018 12:17:19 PM - m1 - CRITICAL: m1:critical message
06/19/2018 12:17:19 PM - m2 - WARNING: m2:warn message
06/19/2018 12:17:19 PM - m2 - ERROR: m2:error message
06/19/2018 12:17:19 PM - m2 - CRITICAL: m2:critical message
06/19/2018 12:17:19 PM - m3 - ERROR: m3:error message
06/19/2018 12:17:19 PM - m3 - CRITICAL: m3:critical message

How to create seperate log file Based on Caller

Python
import logging 
import inspect 
def getCustomLogger(level): 
loggername=inspect.stack()[1][3] 
logger=logging.getLogger(loggername) 
logger.setLevel(level) 
 
fileHandler=logging.FileHandler('{}.log'.format(loggername),mode='a') 
fileHandler.setLevel(level) 
 
formatter = logging.Formatter('%(asctime)s - %(name)s -
%(levelname)s: %(message)s',datefmt='%m/%d/%Y %I:%M:%S %p') 
fileHandler.setFormatter(formatter) 
logger.addHandler(fileHandler) 
 
return logger 

Output

test.py

  • Same as previous
Python
import logging 
from customlogger import getCustomLogger 
class LoggingDemo: 
def m1(self): 
logger=getCustomLogger(logging.DEBUG) 
logger.debug('m1:debug message') 
logger.info('m1:info message') 
logger.warn('m1:warn message') 
logger.error('m1:error message') 
logger.critical('m1:critical message') 
def m2(self): 
logger=getCustomLogger(logging.WARNING) 
logger.debug('m2:debug message') 
logger.info('m2:info message') 
logger.warn('m2:warn message') 
logger.error('m2:error message') 
logger.critical('m2:critical message') 
def m3(self): 
logger=getCustomLogger(logging.ERROR) 
logger.debug('m3:debug message') 
logger.info('m3:info message') 
logger.warn('m3:warn message') 
logger.error('m3:error message') 
logger.critical('m3:critical message') 

l=LoggingDemo() 
print('Logging Demo with Seperate Log File') 
l.m1() 
l.m2() 
l.m3() 
  • m1.log:
  • 06/19/2018 12:26:04 PM – m1 – DEBUG: m1:debug message
  • 06/19/2018 12:26:04 PM – m1 – INFO: m1:info message
  • 06/19/2018 12:26:04 PM – m1 – WARNING: m1:warn message
  • 06/19/2018 12:26:04 PM – m1 – ERROR: m1:error message
  • 06/19/2018 12:26:04 PM – m1 – CRITICAL: m1:critical message
  • m2.log:
  • 06/19/2018 12:26:04 PM – m2 – WARNING: m2:warn message
  • 06/19/2018 12:26:04 PM – m2 – ERROR: m2:error message
  • 06/19/2018 12:26:04 PM – m2 – CRITICAL: m2:critical message
  • m3.log:
  • 06/19/2018 12:26:04 PM – m3 – ERROR: m3:error message
  • 06/19/2018 12:26:04 PM – m3 – CRITICAL: m3:critical message

Advantages of customized logger

  • We can reuse same customlogger code where ever logger required.
  • For every caller we can able to create a seperate log file
  • For different handlers we can set different log levels.

Another Example for Custom Handler

  • customlogger.py
Python
import logging 
import inspect 
def getCustomLogger(level): 
loggername=inspect.stack()[1][3] 
 
logger=logging.getLogger(loggername) 
logger.setLevel(level) 
fileHandler=logging.FileHandler('test.log',mode='a') 
fileHandler.setLevel(level) 
formatter=logging.Formatter('%(asctime)s - %(name)s -
%(levelname)s: %(message)s',datefmt='%m/%d/%Y %I:%M:%S %p') 
fileHandler.setFormatter(formatter) 
logger.addHandler(fileHandler) 
return logger 

test.py

Python
import logging 
from customlogger import getCustomLogger 
class Test: 
def logtest(self): 
logger=getCustomLogger(logging.DEBUG) 
logger.debug('debug message') 
logger.info('info message') 
logger.warning('warning message') 
logger.error('error message') 
logger.critical('critical message') 
t=Test() 
t.logtest()

student.py

Python
import logging 
from customlogger import getCustomLogger 
def studentfunction(): 
logger=getCustomLogger(logging.ERROR) 
logger.debug('debug message') 
logger.info('info message') 
logger.warning('warning message') 
logger.error('error message') 
logger.critical('critical message') 
studentfunction() 

Note

  • we can disable a partcular level of logging as follows:
  • logging.disable(logging.CRITICAL.

Name
Email
Phone

Report an error