How to change the format of logged messages temporarily, in Python?

后端 未结 4 1742
长发绾君心
长发绾君心 2020-12-06 00:33

What is the simplest method for temporarily changing the logging message format, in Python (through the logging module)?

The goal is to have some standard message fo

4条回答
  •  天涯浪人
    2020-12-06 01:13

    if you want to dynamic change format of log. it could be made like this.

    logger = logging.getLogger()
    # Change format of handler for the logger
    logger.handlers[0].setFormatter(logging.Formatter('%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s'))
    # Print log
    logging.info(log)
    # return other format
    logger.handlers[0].setFormatter(logging.Formatter('%(message)s'))
    

提交回复
热议问题