logging.info doesn't show up on console but warn and error do

前端 未结 3 1757
慢半拍i
慢半拍i 2021-01-30 19:29

When I log an event with logging.info, it doesn\'t appear in the Python terminal.

import logging
logging.info(\'I am info\')  # no output

相关标签:
3条回答
  • 2021-01-30 19:51

    The above solutions didn't work for me, but the code here did:

    # set up logging to file
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                        datefmt='%m-%d %H:%M',
                        filename='/temp/myapp.log',
                        filemode='w')
    # define a Handler which writes INFO messages or higher to the sys.stderr
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    # add the handler to the root logger
    logging.getLogger('').addHandler(console)
    

    (I omitted parts of the code for the sake of readability)

    0 讨论(0)
  • 2021-01-30 19:55

    The root logger always defaults to WARNING level. Try calling

    logging.getLogger().setLevel(logging.INFO)
    

    and you should be fine.

    0 讨论(0)
  • 2021-01-30 19:59

    Like @ztyx said that default logger level is WARNING. You have to set it to a lower level

    You can do it by using logging.basicConfig and setting logger level:

    logging.basicConfig(level=logging.DEBUG)
    
    0 讨论(0)
提交回复
热议问题