Software Engineering

The way to get Python logger to Print to std out

The way to get Python logger to Print to std out
Written by admin


In the event you 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 Customary Output (stdout), then you are able to do the next:

Setup Logger to print to Customary 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 manner now you can log straight to the console as properly:

logger.information('Some textual content will get logged right here')

About the author

admin

Leave a Comment