LoginSignup
4
6

More than 1 year has passed since last update.

python で 発生した例外(エラー等)をログに出力するコード

Posted at

コード

try:
    #コードをここに書くと発生する例外がファイルに出力される。
    print(あああ) # あああ は文字列変数を指定していなければ"(ダブルクォート)で囲む必要がある。
except Exception as e:
    import traceback
    with open('error.log', 'a') as f:
        traceback.print_exc( file=f)

error.log の 出力結果

Traceback (most recent call last):
  File "<ipython-input-6-b89e2faedd03>", line 2, in <module>
    print(あああ)
NameError: name 'あああ' is not defined
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6