Skip to content

Instantly share code, notes, and snippets.

@hxu
Created January 23, 2014 03:58
Show Gist options
  • Save hxu/8572587 to your computer and use it in GitHub Desktop.
Save hxu/8572587 to your computer and use it in GitHub Desktop.
Generic python logger setup
import logging
logger = logging.getLogger('log')
logger.setLevel(logging.DEBUG)
log_formatter = logging.Formatter('%(asctime)s - %(module)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
# Log to file
logfile = logging.FileHandler('run.log')
logfile.setLevel(logging.DEBUG)
logfile.setFormatter(log_formatter)
# Log to console
logstream = logging.StreamHandler()
logstream.setLevel(logging.INFO)
logstream.setFormatter(log_formatter)
logger.addHandler(logfile)
logger.addHandler(logstream)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment