Created
August 17, 2015 19:45
-
-
Save bnrubin/7f8813666c6ece9344f6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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) |
This file contains hidden or 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 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