custom handler does not write to log file

D

dj

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.
 
D

dj

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.

Kindly ingnore this message, turns out the problem was a
misunderstanding of the severity for the logging levels.
My bad. Thanks anyway :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top