views:

309

answers:

3

I am using python's logging module in a django project. I am performing the basic logging configuration in my settings.py file. Something like this:

import logging   
import logging.handlers
logger = logging.getLogger('project_logger')
logger.setLevel(logging.INFO)

LOG_FILENAME = '/path/to/log/file/in/development/environment'
handler = logging.handlers.TimedRotatingFileHandler(LOG_FILENAME, when = 'midnight')
formatter = logging.Formatter(LOG_MSG_FORMAT)
handler.setFormatter(formatter)
logger.addHandler(handler)

I have a separate settings file for production. This file (production.py) imports everything from settings and overrides some of the options (set DEBUG to False, for instance). I wish to use a different LOG_FILENAME for production. How should I go about it? I can repeat the entire configuration section in production.py but that creates problems if /path/to/log/file/in/development/environment is not present in the production machine. Besides it doesn't look too "DRY".

Can anyone suggest a better way to go about this?

+1  A: 

Why don't you put this statements at the end of settings.py and use the DEBUG flal es indicator for developement?

Something like this:

import logging   
import logging.handlers
logger = logging.getLogger('project_logger')
logger.setLevel(logging.INFO)

[snip]
if DEBUG:
    LOG_FILENAME = '/path/to/log/file/in/development/environment'
else:
    LOG_FILENAME = '/path/to/log/file/in/production/environment'

handler = logging.handlers.TimedRotatingFileHandler(LOG_FILENAME, when = 'midnight')
formatter = logging.Formatter(LOG_MSG_FORMAT)
handler.setFormatter(formatter)
logger.addHandler(handler)
Martin
Thanks.I can see your point but I would like ALL my production specific settings to be in one place rather than split across two different files. Also since I am importing everything from my settings file to production.py and *then* overriding DEBUG, LOG_FILENAME would end up pointing to development location.
Manoj Govindan
Another possibility is using two separate Python logging configuration files for development and production. Again this is not too "DRY" as these files would be identical except for the destination file part.
Manoj Govindan
This looks promising: http://stackoverflow.com/questions/342434/python-logging-in-django/343575#343575 I am going to try it.
Manoj Govindan
+1  A: 

Found a reasonably "DRY" solution that worked. Thanks to http://stackoverflow.com/questions/342434/python-logging-in-django/343575#343575

I now have a log.py which looks something like this:

import logging, logging.handlers
from django.conf import settings

LOGGING_INITIATED = False
LOGGER_NAME = 'project_logger'

def init_logging():
    logger = logging.getLogger(LOGGER_NAME)
    logger.setLevel(logging.INFO)
    handler = logging.handlers.TimedRotatingFileHandler(settings.LOG_FILENAME, when = 'midnight')
    formatter = logging.Formatter(LOG_MSG_FORMAT)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

if not LOGGING_INITIATED:
    LOGGING_INITIATED = True
    init_logging()

My settings.py now contains

LOG_FILENAME = '/path/to/log/file/in/development/environment

and production.py contains:

from settings import *
LOG_FILENAME = '/path/to/log/file/in/production/environment'
Manoj Govindan
A: 

In this instance, how would actually you start logging when you start up your server? If I

import log
is the global object "logger" available in my views? It doesn't seem to be working right now.

xtrahotsauce
Do this in your view: logger = logging.getLogger(LOGGER_NAME) and you'll be all set. I prefer to set LOGGER_NAME in my settings but it can be defined anywhere.
Manoj Govindan
Is there anyway to do this if I'm already using the global logging object (http://code.google.com/p/django-logging/) to do logging? So instead of doing logger.info('BLAH'), I'd just like to do logging.info('BLAH'). Thanks!
xtrahotsauce