custom handler does not write to log file

dj d.a.abernathy at gmail.com
Tue Mar 31 14:13:17 EDT 2009


It seems that you can create custom handlers and add them to the
logging.handlers namespace(http://mail.python.org/pipermail/python-
list/2008-May/493826.html.)
But for reasons beyond my understanding my log file (test.log) is not
written to.

####################  my handler class
#######################################################
import logging.handlers


# create my handler class
class MyHandler(logging.handlers.RotatingFileHandler):
    def __init__(self, filename):
        logging.handlers.RotatingFileHandler.__init__(self, filename,
 
maxBytes=10485760, backupCount=5)


# Register handler in the "logging.handlers" namespace
logging.handlers.MyHandler = MyHandler

################  test app.py
##################################################################
import logging
import logging.handlers

from myhandler import MyHandler

# log file path
LOG_FILE_PATH='H:/python_experiments/logging/test.log'  # log file
path
#log file formatter
myformatter = logging.Formatter('%(asctime)s %(levelname)s %(filename)
s %(lineno)d %(message)s')

# setup a log instance for myHandler
logger2 = logging.getLogger('myLog2')
logger2.setLevel(logging.CRITICAL)
hdlr2 = logging.handlers.MyHandler(LOG_FILE_PATH)
hdlr2.setFormatter(myformatter)
hdlr2.setLevel(logging.CRITICAL)
logger2.addHandler(hdlr2)

# give it a try
print 'using myHandler'
logger2.debug('this is a test of myHandler')
print 'after logger using myHandler'

Thanks in advance for your help.



More information about the Python-list mailing list