how to avoid hardcoding of logfile path in logging.conf file

445 views
Skip to first unread message

Rama

unread,
Jan 27, 2009, 5:31:58 AM1/27/09
to django...@googlegroups.com, Lakshman Prasad, Shabda Raaj
please look at the below logging.conf file.
http://dpaste.com/hold/113443/

For RotatingFileHandler [handler_rfileHandler]  i have hard coded the log file path as "/home/rama/djangoprojects/doloto/logs/logs.txt"
how to avoid such hard coding of log file path in the logging.conf file.

1) i have tried using  ./logs/logs.txt
     a.  It is working well whenever iam using the  (python manage.py runserver )
     b. but it is not working whenever i run a standalone python script which uses django module which in turn uses this logging configuration.

can any one  guide me on how to avoid hardcoding of log file path ?

-rama


Thomas Guettler

unread,
Jan 27, 2009, 8:28:56 AM1/27/09
to django...@googlegroups.com
Rama schrieb:

> please look at the below logging.conf file.
> ...

> can any one guide me on how to avoid hardcoding of log file path ?
>
>
Hi,

I am not a logging expert, maybe there is a solution, but if you
do your setup with python code, you can use e.g. os.environ['HOME'] or
settings.FOO.

I never used a logging.conf file.

HTH,
Thomas

BTW: This is a pure python question. You get more and better answers
on the newsgroup comp.lang.python.

--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de

Eric Abrahamsen

unread,
Jan 27, 2009, 8:37:09 AM1/27/09
to django...@googlegroups.com

On Jan 27, 2009, at 9:28 PM, Thomas Guettler wrote:

>
> Rama schrieb:
>> please look at the below logging.conf file.
>> ...
>> can any one guide me on how to avoid hardcoding of log file path ?
>>
>>
> Hi,
>
> I am not a logging expert, maybe there is a solution, but if you
> do your setup with python code, you can use e.g. os.environ['HOME'] or
> settings.FOO.

This last (specifying a logfile in settings.py) has always worked very
nicely for me.

E

Rama Vadakattu

unread,
Jan 27, 2009, 8:45:57 AM1/27/09
to Django users
Eric,
can you please show me the code snippet of specifying a logfile in
settings.py.

On Jan 27, 6:37 pm, Eric Abrahamsen <gir...@gmail.com> wrote:
> On Jan 27, 2009, at 9:28 PM, Thomas Guettler wrote:
>
>
>
> > Rama schrieb:
> >> please look at the below logging.conf file.
> >> ...
> >> can any one  guide me on how to avoid hardcoding of log file path ?
>
> > Hi,
>
> > I am not a logging expert, maybe there is a solution, but if you
> > do your setup with python code, you can use e.g. os.environ['HOME'] or
> > settings.FOO.
>
> This last (specifying a logfile in settings.py) has always worked very  
> nicely for me.
>
> E
>
> > I never used a logging.conf file.
>
> > HTH,
> >  Thomas
>
> > BTW: This is a pure python question. You get more and better answers
> > on the newsgroup comp.lang.python.
>
> > --
> > Thomas Guettler,http://www.thomas-guettler.de/

bruno desthuilliers

unread,
Jan 27, 2009, 9:41:44 AM1/27/09
to Django users
On 27 jan, 11:31, Rama <ramaakrish...@gmail.com> wrote:
> please look at the below logging.conf file.http://dpaste.com/hold/113443/
>
> For RotatingFileHandler [handler_rfileHandler]  i have hard coded the log
> file path as "/home/rama/djangoprojects/doloto/logs/logs.txt"
> how to avoid such hard coding of log file path in the logging.conf file.


1/ in your settings.py file:
LOG_PATH = "/whatever/you/want.log"


2/ in the code calling logging.config.fileConfig:

from django.core.exceptions import ImproperlyConfigured
from django.conf import settings

try:
LOG_PATH = settings.LOG_PATH
except AttributeError:
raise ImproperlyConfigured("missing configuration : LOG_PATH")

import logging
logging.config.fileConfig(LOG_PATH, defaults=dict(log_path=LOG_PATH))


3/ in your logging.conf

[handler_rfileHandler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=simpleFormatter
args=(%(log_path)s,'a',5000000,5)


Not tested, but this should do the trick. If it doesn't, you may have
to provide the whole value for 'args', ie:

in 2/

log_args = (LOG_PATH, 'a', 5000000, 5)
logging.config.fileConfig(LOG_PATH, defaults=dict(log_args=log_args))

in 3/

args=%(log_args)s


HTH

Eric Abrahamsen

unread,
Jan 27, 2009, 9:48:08 AM1/27/09
to django...@googlegroups.com

On Jan 27, 2009, at 9:45 PM, Rama Vadakattu wrote:

>
> Eric,
> can you please show me the code snippet of specifying a logfile in
> settings.py.

Sure, here's something I use to keep a separate log of requests to an
RSS feed:

settings.py
#######
LOG_FILE = '/path/to/log/file.log'

feeds.py
######
import logging
from mysite.settings import LOG_FILE as lf

... # later in the file

feedlog = logging.FileHandler(lf)
form = logging.Formatter("%(asctime)s: %(message)s")
feedlog.setFormatter(form)
root = logging.getLogger('')
root.addHandler(feedlog)

def feedlogger(fn):
def wrapper(request,*args,**kwargs):
root.info("%s %s" % (request.path,
request.META.get('REMOTE_ADDR','')))
resp = fn(request,*args,**kwargs)
return resp
return wrapper


Then I import the feedlogger wrapper into my urls.py and wrap the
appropriate function there. The syntax for the logging handler is a
bit of a PITA, but that's just how Python logging works...

Hope that helps,
Eric

Rama Vadakattu

unread,
Jan 28, 2009, 6:07:31 AM1/28/09
to Django users
Thanks bruno desthuilliers
the tip which you have given is working.

One more tweak which i have done is
instead of LOG_PATH = "/whatever/you/want.log" in setting.py
i have used LOG_PATH = "\"/whatever/you/want.log\"" otherwise it is
throwing out some exception.

Thanks Eric for your code.

--rama

Vinay Sajip

unread,
Jan 28, 2009, 7:03:00 AM1/28/09
to Django users


On Jan 28, 11:07 am, Rama Vadakattu <ramaakrish...@gmail.com> wrote:
> Thanks bruno desthuilliers
> the tip which you have given is working.
>

I think it's better just to have the logging configuration done at the
bottom of settings.py (either programmatically or via fileConfig).
Typically, if you have code in your apps which adds logging handlers,
and if your code gets imported more than once, you have to be careful
not to add handlers multiple times (otherwise your log messages get
emitted multiple times). Then, in your app modules, you only have to
use loggers:

import logging

logger = logging.getLogger(__name__) # name of module

...

logger.debug("A debug message")

...

Typically, you change logging configuration on a site-wide basis (e.g.
turning verbosity up and down for different apps in the site) so
settings.py is a reasonable place to keep this. Then individual apps
never need be concerned about where their logging output is going (in
fact they shouldn't hardcode this type of information, especially if
they are meant to be reusable).

Regards,

Vinay Sajip
Reply all
Reply to author
Forward
0 new messages