Skip to content

How to get Python logger to Print to std out

If you use Python’s logger as follows:

import logging logger = logging.getLogger() logger.setLevel(logging.DEBUG) # or logging.INFO
Code language: Python (python)

Perhaps you want to get it to print to Standard Output (stdout), then you can do the following:

Setup Logger to print to Standard 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 - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler)
Code language: Python (python)

This way you can now log straight to the console as well:

logger.info('Some text gets logged here')
Code language: Python (python)

See also  How to append to an Array in Elasticsearch using elasticsearch-py
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x