Skip to content

Instantly share code, notes, and snippets.

@luminoso
Created February 4, 2020 16:06
Show Gist options
  • Save luminoso/b58199813f6763821f6e742a297a1a63 to your computer and use it in GitHub Desktop.
Save luminoso/b58199813f6763821f6e742a297a1a63 to your computer and use it in GitHub Desktop.
python double file and console logger different levels
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