0

I want to log to stream of io.StringIO, but end up with empty stream. Here is my code:

import logging, io

log_handler = logging.StreamHandler(stream)
log_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
log_handler.setFormatter(log_format)
log_handler.setLevel(logging.INFO)

logger.addHandler(log_handler)

logger.info("This is test info log")
print(stream.getvalue())

What am I doing wrong?

UPDATE It seems to be working if I replace level with "Warning" like this

log_handler.setLevel(logging.WARNING)
logger.warning("This is test info log")

It also prints into console for some reason

3

1 Answer 1

1

Configure the root logger: logging.basicConfig(level=logging.DEBUG).

Then you can set log_handler.setLevel(logging.INFO) to whatever level you wish, it will capture logs to stream accordingly.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.