Skip to content

Logger formatter class initialization state clarification  #127805

Closed
@jlynchMicron

Description

@jlynchMicron

Documentation

https://docs.python.org/3/library/logging.html#logging.Handler.setFormatter

When providing a logger handler with a formatter class, there is no documentation stating if that formatter class needs to be initialized before passing it as an argument. Through trial and error, I found that it needs to be initialized first. Please either clarify the documentation or have underlying code check to see if the formatter is initialized yet.

Problemed formatter configuration:

handler.setFormatter(xml_log_formatter)
logging.info('test_msg')

Error:
image

Fixed formatter configuration:

handler.setFormatter(xml_log_formatter())
logging.info('test_msg')

Purposed code check (from a custom logging module):

#Instantiate formatter class (if needed)
if isinstance(cls.log_formatter, type):
    cls.log_formatter = cls.log_formatter()

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status

    Done

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions