Last active
September 20, 2016 09:20
-
-
Save flyhigher139/117ebd47d1c0c182193078989c429cb1 to your computer and use it in GitHub Desktop.
python log
This file contains 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
#!/usr/local/bin/python | |
# -*- coding: utf-8 -*- | |
import logging | |
logging.debug('debug message') | |
logging.info('info message') | |
logging.warn('warn message') | |
logging.error('error message') | |
logging.critical('critical message') | |
# add log file and log level | |
logging.basicConfig(filename='logger.log', level=logging.INFO) | |
logging.debug('debug message') | |
logging.info('info message') | |
logging.warn('warn message') | |
logging.error('error message') | |
logging.critical('critical message') |
This file contains 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
import logging | |
# create logger | |
logger = logging.getLogger('simple_example') | |
# Set default log level | |
logger.setLevel(logging.DEBUG) | |
# create console handler and set level to debug | |
ch = logging.StreamHandler() | |
ch.setLevel(logging.WARN) | |
ch2 = logging.FileHandler('logging.log') | |
ch2.setLevel(logging.INFO) | |
# create formatter | |
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
# add formatter to ch | |
ch.setFormatter(formatter) | |
ch2.setFormatter(formatter) | |
# add ch to logger | |
# The final log level is the higher one between the default and the one in handler | |
logger.addHandler(ch) | |
logger.addHandler(ch2) | |
# 'application' code | |
logger.debug('debug message') | |
logger.info('info message') | |
logger.warn('warn message') | |
logger.error('error message') | |
logger.critical('critical message') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment