Skip to content

Instantly share code, notes, and snippets.

@codeinthehole
Created June 2, 2011 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeinthehole/1004240 to your computer and use it in GitHub Desktop.
Save codeinthehole/1004240 to your computer and use it in GitHub Desktop.
Environment-based logger
from logging import FileHandler as BaseFileHandler
import os
class EnvFileHandler(BaseFileHandler):
"""
Custom filehandler that uses the LOG_ROOT setting to determine the folder
to store log files in.
We have to do some tricky stuff to avoid circular imports. To this end,
we pass /dev/null to the parent handler but specify opening to be delayed.
Then when we try to first open the file, we join the LOG_ROOT with the
passed filename.
"""
def __init__(self, filename, *args, **kwargs):
self.filename = filename
kwargs['delay'] = True
BaseFileHandler.__init__(self, "/dev/null", *args, **kwargs)
def _open(self):
from django.conf import settings
self.baseFilename = os.path.join(settings.LOG_ROOT, self.filename)
return BaseFileHandler._open(self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment