Opened 14 years ago

Closed 14 years ago

Last modified 12 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

Description

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

LOGGING = {
    '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:

[formatter_coloredFormatter]
class=myproject.logging.ColoredFormatter
format=%(levelname).1s: [%(asctime)s] %(message)s (%(name)s)
datefmt=%H:%M:%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.

Thanks

Change History (2)

comment:1 by Karen Tracey, 14 years ago

Resolution: invalid
Status: newclosed

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: http://docs.python.org/library/logging.html#dictionary-schema-details (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 by tlo, 12 years ago

Easy pickings: unset
UI/UX: unset

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

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