简体   繁体   English

Python 记录器 - 具有多个级别的多个记录器实例 - 最佳实践

[英]Python logger - multiple logger instances with multiple levels - best practice

I have the following requirements:我有以下要求:

  1. To have one global logger which you can configure (setup level, additional handlers,..)拥有一个可以配置的全局记录器(设置级别、附加处理程序、..)
  2. To have per module logger which you can configure (setup level, additional handlers,..)拥有可以配置的每个模块记录器(设置级别、附加处理程序、..)

In other words we need more logs with different configuration换句话说,我们需要更多不同配置的日志

Therefore I did the following因此我做了以下

  1. create method to setup logger:创建设置记录器的方法:
def setup_logger(module_name=None, level=logging.INFO, add_stdout_logger=True):

   print("Clear all loggers")
   for _handler in logging.root.handlers:
       logging.root.removeHandler(_handler)

   if add_stdout_logger:
       print("Add stdout logger")
       stdout_handler = logging.StreamHandler(sys.stdout)
       stdout_handler.setLevel(level)
       stdout_handler.setFormatter(logging.Formatter(fmt='%(asctime)-11s [%(levelname)s] [%(name)s] %(message)s'))
       logging.root.addHandler(stdout_handler)


   print("Set root level log")
   logging.root.setLevel(level)

   if module_name:
       return logging.getLogger(module_name)
   else:
       return logging.getLogger('global')

Then I create logger as following:然后我创建记录器如下:

logger_global = setup_logger(level=logging.DEBUG)
logger_module_1 = setup_logger(module_name='module1', level=logging.INFO)
logger_module_2 = setup_logger(module_name='module2', level=logging.DEBUG)

logger_global.debug("This is global log and will be visible because it is setup to DEBUG log")

logger_module_1.debug("This is logger_module_1 log and will NOT be visible because it is setup to INFO log") 

logger_module_2.debug("This is logger_module_2 log and will be visible because it is setup to DEBUG log")

Before I will try what works and what not and test it more deeply I want to ask you if this is good practice to do it or do you have any other recommendation how to achieve our requrements?在我尝试什么可行,什么不可行并更深入地测试之前,我想问你这是否是一种好的做法,或者你有任何其他建议如何实现我们的要求?

Thanks for help感谢帮助

Finally I found how to do it:最后我找到了如何做到这一点:

def setup_logger(module_name=None, level=logging.INFO, add_stdout_logger=True):

   custom_logger = logging.getLogger('global')
   if module_name:
       custom_logger = logging.getLogger(module_name)     

   print("Clear all handlers in logger") # prevent multiple handler creation
   module_logger.handlers.clear()

   if add_stdout_logger:
       print("Add stdout logger")
       stdout_handler = logging.StreamHandler(sys.stdout)
       stdout_handler.setLevel(level)
       stdout_handler.setFormatter(logging.Formatter(fmt='%(asctime)-11s [%(levelname)s] [%(name)s] %(message)s'))
       module_logger.addHandler(stdout_handler)

    # here you can add another handlers ,...

    # because we use custom handlers which have the different type of log level,
    # then our logger has to have the lowest level of logging
    custom_logger.setLevel(logging.DEBUG)

   return custom_logger 

Then simply call the following然后只需调用以下

logger_module_1 = setup_logger(module_name='module1', level=logging.INFO)
logger_module_2 = setup_logger(module_name='module2', level=logging.DEBUG)

logger_module_1.debug("This is logger_module_1 log and will NOT be visible because it is setup to INFO log") 

logger_module_2.debug("This is logger_module_2 log and will be visible because it is setup to DEBUG log")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM