Skip to content

Logger formatter class initialization state clarification  #127805

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

Activity

  1. Uvi-12 commented on Dec 11, 2024

    @Uvi-12

    @jlynchMicron I would like to work on this, I wanted to ask how necessary the proposed change to the setFormatter method is. Would allowing it to accept both formatter instances and classes have a significant impact on the usability or functionality of the logging module? The current behavior works if users follow the documentation, though it could be clearer.

    Is this change something that is critical to address, or would clarifying the documentation alone be sufficient?

  2. jlynchMicron commented on Dec 11, 2024

    @jlynchMicron
    Author

    Hi @Uvi-12, I believe clarification in the documentation would totally be sufficient. The error message generated when using an un-initialized formatter class is not very clear to a more novice programmer, especially when their formatter.format function does have a "record" positional argument.

  3. Uvi-12 commented on Dec 11, 2024

    @Uvi-12

    Hi @Uvi-12, I believe clarification in the documentation would totally be sufficient. The error message generated when using an un-initialized formatter class is not very clear to a more novice programmer, especially when their formatter.format function does have a "record" positional argument.

    Thank you for the clarification! I understand that the error message can be confusing, especially for novice users. Given that, I’ll go ahead and work on improving the documentation to make it clearer and help prevent misunderstandings related to un-initialized formatter classes.

    Let me know if there are any additional points I should consider while working on this!

  4. jlynchMicron commented on Dec 11, 2024

    @jlynchMicron
    Author

    I think that should be it, thanks!

  5. Uvi-12 commented on Dec 12, 2024

    @Uvi-12

    @jlynchMicron Please check PR #127850 and let me know if it solves the issue.

  6. jlynchMicron commented on Dec 13, 2024

    @jlynchMicron
    Author

    @Uvi-12 looks good to me!

  7. added a commit that references this issue on Feb 21, 2025
  8. added 2 commits that reference this issue on Feb 21, 2025
  9. added a commit that references this issue on Feb 24, 2025
  10. added a commit that references this issue on Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions