Skip to content

Instantly share code, notes, and snippets.

@bnrubin
Created August 17, 2015 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bnrubin/7f8813666c6ece9344f6 to your computer and use it in GitHub Desktop.
Save bnrubin/7f8813666c6ece9344f6 to your computer and use it in GitHub Desktop.
import logging
import sys
import traceback
import os
import time
import Queue
class DatedLogFile(logging.FileHandler):
"""A logging handler class that uses a site standard
filename and format.
"""
def __init__(self, path, debug=False):
"""Open a file in the `dir` directory as a stream for logging.
The log's filename will be automatically formatted to the current date in following Python strftime form::
``time.strftime("%Y%m%d")+".log")``
:param path: Logfile directory
:param debug: Should debug messages be logged?
"""
filename = os.path.join(path, time.strftime("%Y%m%d") + ".log")
logging.FileHandler.__init__(self, filename)
self.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
if debug:
logging.getLogger().setLevel(logging.DEBUG)
else:
logging.getLogger().setLevel(logging.INFO)
import logging
from DatedLogFile import DatedLogFile
LOG_DIR = '/var/log/foo/'
# attach to root logger
logging.getLogger().addHandler(DatedLogFile(LOG_DIR))
logging.info('stuff')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment