Last active
December 18, 2015 19:08
-
-
Save vanzaj/5830385 to your computer and use it in GitHub Desktop.
logging example with syslog like formatting
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 | |
import logging.handlers | |
LOG_FILE = 'test.log' | |
#logging.basicConfig(filename=LOG_FILE, | |
#level=logging.DEBUG, | |
#) | |
#logging.debug('this should be in the log file') | |
log = logging.getLogger(__file__) | |
log.setLevel(logging.DEBUG) | |
fmt = '%(asctime)s %(name)s: [%(levelname)s] %(message)s' | |
formatter = logging.Formatter(fmt, datefmt='%b %d %H:%M:%S') | |
handler = logging.handlers.RotatingFileHandler( | |
LOG_FILE, maxBytes=300, backupCount=3) | |
handler.setFormatter(formatter) | |
log.addHandler(handler) | |
for i in range(50): | |
msg = 'i = %d' % (i) | |
if i % 2: | |
log.debug(msg) | |
else: | |
log.warning(msg) | |
# log.debug('debug message') | |
# log.info('info message') | |
# log.warn('warn message') | |
# log.error('error message') | |
# log.critical('critical message') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment