Detail the two methods of using python's logging module in stdout output

  • 2020-06-01 10:15:48
  • OfStack

Detail using python's logging module in stdout output


When using python's logging module, in addition to logging in files, you also want to be able to output the logs directly to standard output std.out when the python script is executed in the foreground.


The logging module can do this in two ways:

Option 1: basicconfig

import sys
import logging

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

Option 2: handler

Multiple handler can be added to logging, so you only need to add one handler to log.

import sys
import logging

log = logging.getLogger()
stdout_handler = logging.StreamHandler(sys.stdout)

Thank you for reading, I hope to help you, thank you for your support of this site!

Related articles: