Last active
August 24, 2016 14:37
-
-
Save nzer0/85054780cbadc595eafd to your computer and use it in GitHub Desktop.
python logging snippet
This file contains hidden or 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
| # Configure | |
| import logging, sys | |
| reload(logging) | |
| logger = logging.getLogger('program_name') | |
| logger.setLevel(logging.INFO) | |
| fh = logging.FileHandler('logfilename.log') | |
| fh.setLevel(logging.INFO) | |
| ch = logging.StreamHandler(sys.stdout) | |
| ch.setLevel(logging.INFO) | |
| formatter = logging.Formatter('%(asctime)s %(name)s [%(levelname)s] %(message)s') | |
| fh.setFormatter(formatter) | |
| ch.setFormatter(formatter) | |
| logger.addHandler(fh) | |
| logger.addHandler(ch) | |
| # After configuration use as follows | |
| logger.info('hello') | |
| logger.debug('hello') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment