4

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?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Manoj Govindan
  • 72,339
  • 21
  • 134
  • 141

2 Answers2

1

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 Thurau
  • 7,564
  • 7
  • 43
  • 80
  • 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 Jul 18 '09 at 15:40
  • 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 Jul 18 '09 at 16:11
  • This looks promising: http://stackoverflow.com/questions/342434/python-logging-in-django/343575#343575 I am going to try it. – Manoj Govindan Jul 18 '09 at 16:43
1

Found a reasonably "DRY" solution that worked. Thanks to Python logging in Django

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'
Community
  • 1
  • 1
Manoj Govindan
  • 72,339
  • 21
  • 134
  • 141