how to specify user defined objects for handlers in yaml file for logging python

Viewed 458

I have 3 files in myapp folder:

myapp
 ->test.py
 -> hybrid.py
 ->config.yaml

I have developed a custom logging handler in hybrid.py file:

class WatchedTimedRotatingFileHandler(RotatingFileHandler, WatchedFileHandler):

    def __init__(self, filename, **kwargs):
        super().__init__(filename, **kwargs)
        self.dev, self.ino = -1, -1
        self._statstream()
    

    def emit(self, record):
        self.reopenIfNeeded()
        super().emit(record)

I just need to specify the WatchedTimedRotatingFileHandler class in config.yaml to configure the logging

config.yaml

version: 1

formatters:
  simple:
    format: "%(asctime)s %(name)s: %(message)s"
  extended:
    format: "%(asctime)s %(name)s %(levelname)s: %(message)s"

 handlers:
    console:
       class: logging.StreamHandler
       level: INFO
       formatter: simple

    file_handler:
       class: logging.handlers.RotatingFileHandler
       level: INFO
       filename: abc.log
       formatter: extended
       maxBytes: 100
       backupCount: 3

    file:
      (): hybrid.WatchedTimedRotatingFileHandler
      filename: test.log
      formatter: extended
      maxBytes: 100
      backupCount: 3   

loggers:
    dev:
      handlers: [console, file_handler,file]
    test:
      handlers: [file_handler]
    root:
      handlers: [file_handler]

I have used () to refer the user defined object from https://docs.python.org/3.6/library/logging.config.html#logging-config-dict-userdef

and similar question in stackoverflow but no luck.

but getting error:

Traceback (most recent call last):
File "C:\python3.6\lib\logging\config.py", line 558, in configure
handler = self.configure_handler(handlers[name])
File "C:\python3.6\lib\logging\config.py", line 704, in configure_handler
c = self.resolve(c)
File "C:\python3.6\lib\logging\config.py", line 378, in resolve
found = self.importer(used)
File "C:\Users\akshay.ajay.indalkar\Downloads\client9\hybrid.py", line 5, in <module>
EmailConfig.loadProperties()
File "C:\Users\akshay.ajay.indalkar\Downloads\client9\email_config.py", line 19, in loadProperties
EmailConfig.config.read(root_dir)
File "C:\python3.6\lib\configparser.py", line 694, in read
for filename in filenames:
TypeError: 'NoneType' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "test_yaml.py", line 9, in <module>
logging.config.dictConfig(log_cfg)
File "C:\python3.6\lib\logging\config.py", line 795, in dictConfig
dictConfigClass(config).configure()
File "C:\python3.6\lib\logging\config.py", line 566, in configure
'%r: %s' % (name, e))
ValueError: Unable to configure handler 'file': 'NoneType' object is not iterable
1 Answers
Related