How do I change the default format of log messages in python app engine?

前端 未结 2 661
礼貌的吻别
礼貌的吻别 2021-02-19 23:15

I would like to log the module and classname by default in log messages from my request handlers.

The usual way to do this seems to be to set a custom format string by c

2条回答
  •  抹茶落季
    2021-02-19 23:37

    I found this to be working for Python 3.6, it will set the logging level / format for all subsequent logging calls, even if logging is called by previous imports.

    logging_level = logging.INFO
    logging_fmt = "%(levelname)s:%(name)s:%(message)s"   # the default
    try:
        root_logger = logging.getLogger()
        root_logger.setLevel(logging_level)
        root_handler = root_logger.handlers[0]
        root_handler.setFormatter(logging.Formatter(logging_fmt))
    except IndexError:
        logging.basicConfig(level=logging_level, format=logging_fmt)
    

提交回复
热议问题