1

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

3
  • The configuration attribute classneeds the fully qualified name of the handler class which in your case is myhandler.FooBar right? But do you import logging.handler.RotatingFileHandler as RotatingFileHandlerin the classes implementation? Commented Jun 3, 2016 at 14:15
  • FooBar is just an example. I want to change behaviour of emit function and copy log file to AWS S3 upon rotate Commented Jun 3, 2016 at 14:47
  • The point was the missing import - cf. ErikR's answer including hints on skipping derivation altogether Commented Jun 3, 2016 at 15:05

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.