Skip to content

Instantly share code, notes, and snippets.

@davewalk
Created May 6, 2014 18:09
Show Gist options
  • Save davewalk/05b9a6614e2b5fe9b640 to your computer and use it in GitHub Desktop.
Save davewalk/05b9a6614e2b5fe9b640 to your computer and use it in GitHub Desktop.
Python standard logging module
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