How do I change the format of a Python log message on a per-logger basis?

后端 未结 2 398
情书的邮戳
情书的邮戳 2021-02-05 00:30

After reading the documentation on logging, I know I can use code like this to perform simple logging:

import logging

def main():
    logging.basicConfig(filena         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 01:02

    You have to create or use an existing subclass of logging.Handler and call the setformatter() method of an instance thereof with an instance of a custom subclass of logger.Formatter. If you set the formatter for a handler that was already attached to the logger you want to modify the output of, you are fine, otherwise you have to retrieve a logger object with logging.getLogger() and call its addHandler() method with the instance of your handler class that you set the formatter on as the argument.

提交回复
热议问题