Friday, 24 March 2017

Creating a global logger in python #pythonLogger



Python global logger


To start with the logger:
You need to understand the Log Levels. The following table clearly shows the various levels of logger supported in python
DEBUG
Detailed information, typically of interest only when diagnosing problems.
INFO
Confirmation that things are working as expected.
WARNING
An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
ERROR
Due to a more serious problem, the software has not been able to perform some function.
CRITICAL
A serious error, indicating that the program itself may be unable to continue running.
-       
-   - Default level is warning
-      - Just remember the above order of levels. If you set level to 'debug' then all the messages will be printed. if it is error, then only error and critical. Hope you get this. 

Simple logging:
    import logging as lo
    lo.warning("Input is risky")
    output: Input is risky

Interpreter output:
>>> import logging as l
>>> l.warning("my warning")
WARNING:root:my warning
>>> l.debug("debug will not be displayed as it is above warning level")
>>> l.error("My error")
ERROR:root:My error

Now here comes the awaited global logger, So when we have multiple python files (say in a python project), having a single file to handle logger works well.

Creating a global logger: Single logger module/class which can be used to
1.     Create a new log file or
2.     Returns logger for a global log file.

Steps:
1) Create a module called myLogger.py :
This will have the log creation code

myLogger.py: 

import logging
def myLog(name, fname = 'myGlobalLog.log'):
'''Debug Log'''                                                                                                 
    logger = logging.getLogger(name);                                                                              
    logger.setLevel(logging.DEBUG)                                                                                  
    fhan = logging.FileHandler(fname)                                                                              
    fhan.setLevel(logging.DEBUG)                                                                                    
    logger.addHandler(fhan)                                                                                        
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')                          
    fhan.setFormatter(formatter)
    '''comment this to enable requests logger'''                                                                   
    #logger.disabled = True
    return logger

Please note, the argument fname is a default argument here.
 
 2) Now to create a new log, you have to pass the file name

from myLogger import myLog
log = myLog(__name__, 'newLog.log')
log.debug("In new log file")

Above code creates a new log file called newLog.log

3) To get the global logger

from myLogger import myLog
#Do not pass the filename to get the global logger
log = myLog(__name__)
log.debug("In myGlobalLog file") 

This will return the global logger called myGlobalLog.log
Need not pass the file name here, 


7 comments: