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

前端 未结 2 651
礼貌的吻别
礼貌的吻别 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:32

    I cooked this up by reading the logging module's __init__.py. I don't know if this is proper, but it seems to work:

    import logging
    
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                        datefmt='%m-%d %H:%M',
                        )
    
    logging.info('Danger Will Robinson!')
    # 03-31 20:00 root         INFO     Danger Will Robinson!
    root = logging.getLogger()
    hdlr = root.handlers[0]
    fmt = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
    hdlr.setFormatter(fmt)
    logging.info('Danger Will Robinson!')
    # root        : INFO     Danger Will Robinson!
    

提交回复
热议问题