forvaidya
forvaidya

Reputation: 3315

Writing custom log handler in python

I am trying to extend RotatingFile Handler to say FooBar.


class FooBar(RotatingFileHandler)  :
    def __init__(self,  filename, mode='a', maxBytes=0,backupCount=0, encoding=None, delay=0) :
        RotatingHandler.__init__(self, filename, mode, maxBytes, backupCount, encoding, delay)

I configure it using


LOGGING = {
  'version': 1,
  'disable_existing_loggers': True,
    'formatters': {
    'verbose': {
      'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
      'simple': {
        'format': '%(levelname)s %(asctime)s %(message)s'
        },
      },
    'handlers': {
      'file':{
        'level': 'ERROR',
        'class': 'myhandler.FooBar',
          'formatter': 'simple',
            'filename': '/tmp/cattle.txt',
              'mode': 'a',
              'maxBytes': 16,
                'backupCount' : 100,
                  },


#-- Remaining part truncated ###


logging.config.dictConfig(LOGGING)  ### === ERROR here 


When I use it ; I get an error


  File "/usr/lib/python2.7/logging/config.py", line 576, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler 'file': global name 'RotatingHandler' is not defined

Upvotes: 1

Views: 1523

Answers (1)

ErikR
ErikR

Reputation: 52029

RotatingHandler is not in scope, so you would need something like this to bring it into scope:

from logging.handlers import RotatingFileHandler

However, have a look at this example:

How to log everything into a file using RotatingFileHandler by using logging.conf file?

You may not need to create your own class to accomplish what you want to do.

Upvotes: 3

Related Questions