Manoj Govindan
Manoj Govindan

Reputation: 74795

What is the DRY way to configure different log file locations for different settings?

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?

Upvotes: 4

Views: 1871

Answers (2)

Manoj Govindan
Manoj Govindan

Reputation: 74795

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'

Upvotes: 1

Martin Thurau
Martin Thurau

Reputation: 7654

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)

Upvotes: 2

Related Questions