Friday, May 12, 2023
HomeSoftware EngineeringThe right way to get Python logger to Print to std out

The right way to get Python logger to Print to std out


For those who use Python’s logger as follows:

import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG) # or logging.INFO

Maybe you need to get it to print to Normal Output (stdout), then you are able to do the next:

Setup Logger to print to Normal Output

import logging

logger = logging.getLogger()
# logger.setLevel(logging.INFO)
logger.setLevel(logging.DEBUG)

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(identify)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

This fashion now you can log straight to the console as properly:

logger.data('Some textual content will get logged right here')
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments