Skip to content

logging.Formatterのformatメソッドの準備段階は2つなのでしょうか? #1014

@ftnext

Description

@ftnext

概要

辞書を書式化する前に、二つの準備段階を経ます。

準備段階は2つではないように思われ、原文を確認したところ、3つあるように思われました

問題のある箇所 (URLで指定すること)

https://docs.python.org/ja/3/library/logging.html#logging.Formatter.format

問題の詳細

https://docs.python.org/3/library/logging.html#logging.Formatter.format

Before formatting the dictionary, a couple of preparatory steps are carried out.

a couple of: 2,3の、少数の、いくつかの(ウィズダム英和辞典)

準備段階として書かれている点をコードと照らし合わせたところ2つでなく3つではないかと思われました
https://github.com/python/cpython/blob/v3.14.3/Lib/logging/__init__.py#L699-L729

レコードの message 属性が msg % args を使って処理されます。

record.message = record.getMessage()

書式化された文字列が '(asctime)' を含むなら、 formatTime() が呼び出され、イベントの発生時刻を書式化します。

if self.usesTime():
    record.asctime = self.formatTime(record, self.datefmt)

例外情報が存在する場合、 formatException() を使って書式化され、メッセージに追加されます。(略)

        if record.exc_info:
            # Cache the traceback text to avoid converting it multiple times
            # (it's constant anyway)
            if not record.exc_text:
                record.exc_text = self.formatException(record.exc_info)

s = self.formatMessage(record)より前のコードのことを準備段階と言っているのなら2個という数になるかと思いますが、その場合は「例外情報が存在する場合」を区別していないのがわかりにくいなと思います

修正案

辞書を書式化する前に、いくつかの準備段階を経ます。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions