Use fileConfig to configure custom handlers in Python

前端 未结 3 1187
别跟我提以往
别跟我提以往 2021-01-03 07:36

I\'m using a config file to configure my logger in a Python application. This is the file:

[loggers]
keys=root

[logger_root]
level=INFO
handlers=console

[h         


        
相关标签:
3条回答
  • 2021-01-03 07:38

    The class= is evaluated in the namespace of the logging module, and by default this does not have a binding to handlers. So you could do

    import logging, logging.handlers
    logging.handlers = logging.handlers
    

    before calling fileConfig(), and then class=handlers.TimedRotatingHandler should work.

    0 讨论(0)
  • 2021-01-03 07:51

    I ran into the same problem when using dictConfig The solution for me was to fully qualify the module path like this:

    [handler_file_rotating]
    class=logging.handlers.TimeRotatingFileHandler
    level=DEBUG
    formatter=file
    args=('../logs/twicker.log', 'd', 1, 5)
    

    You might want to give that a try

    0 讨论(0)
  • 2021-01-03 07:58

    To extend on this a little if you decide to make a custom handler all you need to do is define that handler near the top of your code and then define it as an object in logging.handlers.

    Example:

    class MyCustomRotatingClass(logging.handlers.RotatingFileHandler):
       """custom handler that queues messages
          to be uploaded in batches to the portal
          in a background thread
       """
       def __init__(self, basedir, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0):
           logging.handlers.RotatingFileHandler.__init__(self, 'TestSavvyExecute.log','a',250000,40,'utf-8',0)
           self.maxBytes = maxBytes
    
       def emit(self, record):
           try:
               if logging.handlers.RotatingFileHandler.shouldRollover(self, record):
                   logging.handlers.RotatingFileHandler.doRollover(self)
    
               #ASCII characters use 1 byte each
               if len(record.msg) > self.maxBytes:
                   oldMsg = record.msg
    
                   record.msg = record.msg[0:self.maxBytes]
                   logging.FileHandler.emit(self, record)
    
                   record.msg = oldMsg[self.maxBytes + 1:]
                   self.emit(record)
               else:
                  logging.FileHandler.emit(self, record)
           except (KeyboardInterrupt, SystemExit):
               raise
           except:
               logging.handlers.RotatingFileHandler.handleError(self, record)          
    
    logging.handlers.MyCustomRotatingClass = MyCustomRotatingClass
    logging.config.fileConfig('translator.log.config')
    

    Now you can easily reference it in your config file.

    [handler_rollinglog]
    class=handlers.MyCustomRotatingClass
    
    0 讨论(0)
提交回复
热议问题