Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

logging.basicConfig should support parameterformatter_class (currently instantiates hardcodedlogging.Formatter) #133577

Closed
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement
@hartwork

Description

@hartwork

Hi! 👋

Currentlylogging.basicConfig instantiateslogging.Formatter as a formatter:

fmt=Formatter(fs,dfs,style)

Now if the developer wants a different formatter class, they have to letlogging.basicConfig create the default formatter first and then replace it passing the same parameters:

importloggingclassCustomFormatter(logging.Formatter):    ...# custom code hereformat_="......."# arbitrarylogging.basicConfig(format=format_)formatter=CustomFormatter(fmt=format_)forhandlerinlogging.root.handlers:handler.setFormatter(formatter)

A version of that with the blanks filled in can be seen athttps://gist.github.com/hartwork/8b5963b5e9a698a3d6d352c657418af3 .

A brittle alternative would be use ofinittest.mock.patch:

importloggingfrominittest.mockimportpatchclassCustomFormatter(logging.Formatter):    ...# custom code herewithpatch("logging.Formatter",CustomFormatter):logging.basicConfig(format=".......")

None of that is ideal. With a new parameterformatter_class, the code would be written as this:

importloggingclassCustomFormatter(logging.Formatter):    ...# custom code herelogging.basicConfig(format=".......",formatter_class=CustomFormatter)

I hope that you are open to this suggestion, and I will open a related pull request in a minute.

Thanks! 🙏

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp