Opened 15 years ago

Closed 15 years ago

Last modified 13 years ago

#55 closed defect (wontfix)

add a logger to django

Reported by: James Zhu Owned by: Adrian Holovaty
Component: Tools Version:
Severity: normal Keywords: log
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


insert the block into django.conf.settings file at line 32.

a = me.SETTINGS_MODULE.split( '.' )
if 'settings' in a:
    idx = a.index( 'settings' )
    if idx > 0:
        projname = a[idx-1]
        projmod = __import__( projname , '', '', [''])
        projname = 'django'
        import django as projmod
    logdir = os.path.split( projmod.__file__ )[0] + os.sep + 'logs'
    if not os.access( logdir , os.W_OK ):
        raise 'Cannot write log file in dir: ' + logdir
    import logging
    logger = logging.getLogger( projname )
    hdlr = logging.FileHandler( logdir + os.sep + projname + '.log' )
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    logger.addHandler( hdlr )
    logger.setLevel( logging.DEBUG )
    __builtins__['log'] = logger 'project logger start' )

make sure you have created 'logs' folder at your project or django src.
then you can use log object anywhere.

ATTENTION: NOT fully tested.

Change History (1)

comment:1 Changed 15 years ago by Jacob

Resolution: wontfix
Status: newclosed

I fail to see what this gains Django: if you'd like to use Python's logging, nothing prevents you, and hardcoding the log level and such into django.conf.settings is scary.

Note: See TracTickets for help on using tickets.
Back to Top