Created
February 4, 2020 16:06
-
-
Save luminoso/b58199813f6763821f6e742a297a1a63 to your computer and use it in GitHub Desktop.
python double file and console logger different levels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
filename = 'spam.log' | |
logger = logging.getLogger() | |
logger.setLevel(logging.DEBUG) | |
# create file handler which logs even debug messages | |
fh = logging.FileHandler(filename) | |
fh.setLevel(logging.DEBUG) | |
# create console handler with a higher log level | |
ch = logging.StreamHandler() | |
ch.setLevel(logging.INFO) | |
# create formatter and add it to the handlers | |
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
ch.setFormatter(formatter) | |
fh.setFormatter(formatter) | |
# add the handlers to logger | |
logger.addHandler(ch) | |
logger.addHandler(fh) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment