python logging dictConfig custom formatter is not being called
Asked Answered
C

2

18

i have the following ColoeredFormatter in my Module.

But The Formatter does not get called. I expect "hi" on the console, but i get "This is an info of the root logger".

i've already deleted all .pyc files, but it did not help.

MyModule/__init__.py

from MyModule.ColoredFormatter import ColoredFormatter

__all__ = ('ColoredFormatter')

MyModule/ColoredFormatter.py

import logging

class ColoredFormatter(logging.Formatter):
    def __init__(self, default):
        self.default = default

    def format(self, record):
        print("hi")
        record.msg = "hi"
        return self.default.format(record)

My Script

import logging, logging.config, yaml

conf="""
logging:
  version: 1
  disable_existing_loggers: true
  root:
    level: !!python/name:logging.NOTSET
    handlers: [console]
  handlers:
    console:
      class: logging.StreamHandler
      stream: ext://sys.stdout
      formatter: myFormatter
      level: !!python/name:logging.NOTSET

  formatters:
    myFormatter:
      class: !!python/name:MyModule.ColoredFormatter
"""
dict = yaml.parse(conf)
logging.config.dictConfig(dict["logging"])

logging.info("This is an info of the root logger")
Cover answered 20/2, 2016 at 22:47 Comment(0)
M
9

I'd like to add that the callable accepts a factory in dictConfig. A shorthand to pass custom arguments to the constructor is to utilise lambda.

Example:

from MyModule import ColoredFormatter

...
    'formatters':
        '()': lambda: ColoredFormatter(foo='bar', ...),
Muniments answered 12/5, 2022 at 12:38 Comment(0)
C
27

in the formatter section i had to replace class by ()

import logging, logging.config, yaml

conf="""
logging:
  version: 1
  disable_existing_loggers: true
  root:
    level: !!python/name:logging.NOTSET
    handlers: [console]
  handlers:
    console:
      class: logging.StreamHandler
      stream: ext://sys.stdout
      formatter: myFormatter
      level: !!python/name:logging.NOTSET

  formatters:
    myFormatter:
      '()': MyModule.ColoredFormatter
"""
dict = yaml.parse(conf)
logging.config.dictConfig(dict["logging"])

logging.info("This is an info of the root logger")
Cover answered 20/2, 2016 at 23:43 Comment(4)
Can you please elaborate how you got to this information? I couldn't find it anywhereGoofball
@Goofball some information can be found in the Python logging documentation.Verret
Thanks @Evert, It was a bit hard to find, so for the next generations: you can find it in the "user-defined-objects" section docs.python.org/2/library/…Goofball
Is there a way to pass a fmt argument to the ColoredFormatter’s constructor?Dovap
M
9

I'd like to add that the callable accepts a factory in dictConfig. A shorthand to pass custom arguments to the constructor is to utilise lambda.

Example:

from MyModule import ColoredFormatter

...
    'formatters':
        '()': lambda: ColoredFormatter(foo='bar', ...),
Muniments answered 12/5, 2022 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.