Skip to content

Instantly share code, notes, and snippets.

@llazzaro
Created October 6, 2014 01:16
Show Gist options
  • Save llazzaro/959e65492b9beaa20738 to your computer and use it in GitHub Desktop.
Save llazzaro/959e65492b9beaa20738 to your computer and use it in GitHub Desktop.
python logger initialize code
import logging
def init_logging():
logger = logging.getLogger('logger_name')
logger.setLevel(logging.INFO)
# create file handler which logs even debug messages
fh = logging.FileHandler('logger_name.log')
fh.setLevel(logging.INFO)
# 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')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment