Answers for "python logging to a file but also to the console"

1

python logging to console exqmple

import sys
# ...
logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
Posted by: Guest on January-04-2021
18

python logging to file

import logging
import sys

logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s | %(levelname)s | %(message)s', 
                              '%m-%d-%Y %H:%M:%S')

stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(logging.DEBUG)
stdout_handler.setFormatter(formatter)

file_handler = logging.FileHandler('logs.log')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)

logger.addHandler(file_handler)
logger.addHandler(stdout_handler)
Posted by: Guest on January-27-2021

Code answers related to "python logging to a file but also to the console"

Python Answers by Framework

Browse Popular Code Answers by Language