2

I would like to capture the logs printed by a python program, and save it to either a variable or a file. Is there any way to achieve this without adding handlers or modifying logger config? (this is because the logger class will be used by many other modules, and we want it to be generic)

Code snippet:

import logging
from io import StringIO
from contextlib import redirect_stdout

logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s")

with StringIO() as buf, redirect_stdout(buf), open("test.txt", "w+") as f:
    logger.debug("Debug message")
    logger.info("Info message")
    logger.error("Error message")
    buf.flush()
    f.write(buf.getvalue())

Console output:

xxxx-xx-xx xx:xx:xx,xxx DEBUG Debug message
xxxx-xx-xx xx:xx:xx,xxx INFO Info message
xxxx-xx-xx xx:xx:xx,xxx ERROR Error message

What confuses me is, since logger prints all the logs to stdout by default, redirecting stdout using context manager should do the trick. However, the logs are still printed to the console, and nothing is written to file. Any idea?

Serena
  • 31
  • 4
  • conditionally add additional handler would be the proper way for you, says while initializing, if os.environ["YourTeam_YourLogger_Debug"] == "ON" , you attach another rsys log handler. Otherwise, you'll have to follow / sniffing on file change, while is much worst. – Jack Wu Jan 03 '20 at 07:40

1 Answers1

0

Logging library already have a utility for that.

Suppose you want to start logging event in file my.log then

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.info('Some info')
logging.warning('Some warning')
logging.debug('Debug messages')

For more check the python documentation.

Edit: OP asked if there is alternative way to do that without using basicConfig method. Here is another approach I found that utilize file handler. Using this you can declare a file handler separately and then assign it to a logger.

logger = logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s")

# create file handler
fh = logging.FileHandler('my.log')
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
Saharsh
  • 1,056
  • 10
  • 26