Created
May 6, 2014 18:09
-
-
Save davewalk/05b9a6614e2b5fe9b640 to your computer and use it in GitHub Desktop.
Python standard logging module
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 | |
from logging import handlers | |
logger = logging.getLogger() | |
# Change logging level here (CRITICAL, ERROR, WARNING, INFO or DEBUG) | |
logger.setLevel(logging.DEBUG) | |
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') | |
# Logging variables | |
MAX_BYTES = 200000 | |
# Max number appended to log files when MAX_BYTES reached | |
BACKUP_COUNT = 5 | |
log_file = 'log.txt' | |
fh = logging.handlers.RotatingFileHandler(log_file, | |
'a', | |
MAX_BYTES, | |
BACKUP_COUNT) | |
fh.setLevel(logging.DEBUG) | |
fh.setFormatter(formatter) | |
logger.addHandler(fh) | |
# Uncomment this block if you would like to log to the console. | |
ch = logging.StreamHandler() | |
ch.setLevel(logging.DEBUG) | |
ch.setFormatter(formatter) | |
logger.addHandler(ch) | |
logger.info('And here\'s a sample INFO log message') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment