How to use python logging in multiple modules

前端 未结 2 849
刺人心
刺人心 2020-12-24 09:33

I was wondering what the standard set up is for performing logging from within a Python app.

I am using the Logging class, and I\'ve written my own logger class that

2条回答
  •  不知归路
    2020-12-24 09:58

    Try using logging.getLogger() to get your logging object instance:

    http://docs.python.org/3/library/logging.html#logging.getLogger

    All calls to this function with a given name return the same logger instance. This means that logger instances never need to be passed between different parts of an application.

    UPDATE:

    The recommended way to do this is to use the getLogger() function and configure it (setting a handler, formatter, etc...):

    # main.py
    import logging
    import lib
    
    
    def main():
        logger = logging.getLogger('custom_logger')
        logger.setLevel(logging.INFO)
        logger.addHandler(logging.FileHandler('test.log'))
        logger.info('logged from main module')
        lib.log()
    
    if __name__ == '__main__':
        main()
    
    # lib.py
    import logging
    
    
    def log():
        logger = logging.getLogger('custom_logger')
        logger.info('logged from lib module')
    

    If you really need to extend the logger class take a look at logging.setLoggerClass(klass)

    UPDATE 2:

    Example on how to add a custom logging level without changing the Logging class:

    # main.py
    import logging
    import lib
    
    
    # Extend Logger class
    CUSTOM_LEVEL_NUM = 9
    logging.addLevelName(CUSTOM_LEVEL_NUM, 'CUSTOM')
    def custom(self, msg, *args, **kwargs):
        self._log(CUSTOM_LEVEL_NUM, msg, args, **kwargs)
    logging.Logger.custom = custom
    
    # Do global logger instance setup
    logger = logging.getLogger('custom_logger')
    logger.setLevel(logging.INFO)
    logger.addHandler(logging.FileHandler('test.log'))
    
    
    def main():
        logger = logging.getLogger('custom_logger')
        logger.custom('logged from main module')
        lib.log()
    
    if __name__ == '__main__':
        main()
    

    Note that adding custom level is not recommended: http://docs.python.org/2/howto/logging.html#custom-levels

    Defining a custom handler and maybe using more than one logger may do the trick for your other requirement: optional output to stderr.

提交回复
热议问题