at backyard

Color my life with the chaos of trouble.

【凡ミス】Python logging formatでTypeError: expected string or bytes-like objectというエラーが出た

備忘録以下の割とどうでもいい駄文。

Pythonのloggingを使っていてformatを設定したら以下のようなエラーが出た。

formatter = logging.Formatter('%(asctime)s: %(message)s')
logging.basicConfig(filename=log_file, format=formatter, level=logging.INFO)
TypeError: expected string or bytes-like object

エラー内容からして、求められているのは string or bytes-like object ということだったので下記のように修正してみたら動いた。

logging.basicConfig(filename=log_file_path, format='%(asctime)s: %(message)s', level=logging.INFO)

たしかにこちらの公式ドキュメントを見ると、このようになっていた。

docs.python.org

つまり単純に自身の設定の仕方が誤っていただけだった。

なんでこんな書き方をしてしまったのだろうか?すごい凡ミスでした。

反省:ドキュメントは冷静に読みましょう。