Skip to content

Instantly share code, notes, and snippets.

@tcotav
Created December 12, 2014 20:13
Show Gist options
  • Save tcotav/e7843df353c72e780934 to your computer and use it in GitHub Desktop.
Save tcotav/e7843df353c72e780934 to your computer and use it in GitHub Desktop.
python logging example
import time
import logging
from logging.handlers import TimedRotatingFileHandler
loglvl = logging.DEBUG
logname="py.log"
logger=logging.getLogger(logname)
logger.setLevel(loglvl)
path="./%s" % logname
# create file handler which logs even debug messages
fh = TimedRotatingFileHandler(path, when="m", interval=1, backupCount=5)
fh.setLevel(loglvl)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
if __name__ == "__main__":
while True:
logger.info("now")
time.sleep(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment