Skip to content

Instantly share code, notes, and snippets.

@gwding
Created May 8, 2017 00:07
Show Gist options
  • Save gwding/276353c2150968d6fc70eac20c3d3618 to your computer and use it in GitHub Desktop.
Save gwding/276353c2150968d6fc70eac20c3d3618 to your computer and use it in GitHub Desktop.
quick example of how to let logging output to a file and stdout
"""Logging to file and stdout at the same time"""
import logging
import sys
def config_logging(filename='example.log', format="%(message)s"):
logging.basicConfig(
filename=filename,
format=format,
level=logging.DEBUG)
root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
root.addHandler(ch)
config_logging()
logging.info("info")
logging.debug('debug')
logging.info('info')
logging.warning('warning')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment