custom handler does not write to log file

Discussion in 'Python' started by dj, Mar 31, 2009.

  1. dj

    dj Guest

    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.
     
    dj, Mar 31, 2009
    #1
    1. Advertising

  2. dj

    dj Guest

    On Mar 31, 1:13 pm, dj <> wrote:
    > 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 :).
     
    dj, Mar 31, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VG9tIFdpbmdlcnQ=?=

    My.Log.Writeexception not writing to Application Event Log.

    =?Utf-8?B?VG9tIFdpbmdlcnQ=?=, Jan 20, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    2,390
    =?Utf-8?B?VG9tIFdpbmdlcnQ=?=
    Jan 20, 2006
  2. Petterson Mikael

    Write to error log using redirect:write

    Petterson Mikael, Sep 11, 2006, in forum: XML
    Replies:
    1
    Views:
    848
    Joe Kesselman
    Sep 11, 2006
  3. Replies:
    0
    Views:
    359
  4. Lowell Alleman
    Replies:
    3
    Views:
    543
    Vinay Sajip
    May 29, 2008
  5. Replies:
    0
    Views:
    1,313
Loading...

Share This Page