Code

Opened 3 years ago

Closed 3 years ago

Last modified 13 months 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

Attachments (0)

Change History (2)

comment:1 Changed 3 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: 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 Changed 13 months ago by tlo

  • Easy pickings unset
  • UI/UX unset

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

LOGGING = {
    'formatters': {
        'verbose': {
            '()': CustomFormatter,
            'format': '%(levelname)s %(message)s'
        },
[...]

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.