Opened 5 years ago

Closed 5 years ago

Last modified 3 years ago

#15749 closed Bug (invalid)

Unable to set custom formatter class in LOGGING setting (dictConfig format)

Reported by: marcel.dancak@… Owned by: nobody
Component: Uncategorized Version: 1.3
Severity: Normal Keywords:
Cc: marcel.dancak@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Following logging configuration in does ignore 'class' parameter and creates logging.Formatter instance:

    'version': 1,
    'formatters': {
        'colored': {
            'class': 'myproject.logging.ColoredFormatter',
            'format': '%(levelname).1s: [%(asctime)s] %(message)s (%(name)s)',
            'datefmt': '%H:%M:%S'

In previous Django versions without logging support I was successfully loading configuration with logging.config.fileConfig function, where configuration file contained following lines:

format=%(levelname).1s: [%(asctime)s] %(message)s (%(name)s)

So I guess dictConfig format should allow to configure the same parameters as other methods and therefore it should be a bug. Because I'm using Python 2.5.2, I'm not sure if this is problem of Python's builtin logging module or in Django (if you did some modifications to make it compatible with older Python versions), so I rather report it here.


Change History (2)

comment:1 Changed 5 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

What you describe happening matches the Python doc, so I don't think this is a bug in Django. Per the docs for the dictConfig dictionary schema, 'class' is not a key searched for in the confuring dict for a formatter. Just 'format' and 'datefmt' are special for that config dictionary, and as documented a logging.Formatter instance is created. The docs do mention "custom instantiation"; you may want the special key '()' in place of where you have 'class', see the docs here: (though I have not played with this myself, so I am not entirely sure if '()' can be used directly in place of where you have 'class').

comment:2 Changed 3 years ago by tlo

  • Easy pickings unset
  • UI/UX unset

FTR: '()' works, e.g.:

    'formatters': {
        'verbose': {
            '()': CustomFormatter,
            'format': '%(levelname)s %(message)s'
Note: See TracTickets for help on using tickets.
Back to Top