0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

django rest framework loggingの実装について

Posted at

django rest frameworkでLoggingを実装した際の備忘録

django rest frameworkでloggingを実装した際に、躓いたりした箇所を備忘録として記載しておきます。

環境

バージョン

python:3.10.4
django:4.0.4
django rest framework:3.13.1

実装

1. settings.pyにloggingの設定追加

以下の内容で、setting.pyにloggingの設定を追加しました。
image.png

2. View.pyにlogを出力する内容を追加しました。

赤線の内容を追加
image.png
image.png

これで、一度動作確認。

想定している結果は、以下のC\Myprojects\IncidentManagement\logs内に、logger.logファイルが作成され、ログの内容が出力されている事。
image.png

とりあえず、実行
image.pngimage.png

201 Createdが返ってきたので、処理としては成功しています。
これで、対象のフォルダ内に、logger.logファイルが作成され、そこにログが吐かれてーーーーーーー
image.png
いませんでしたwwww

ただし、コンソールには、ログが出力されていました。
image.png

原因調査

1. やってみたこと

1) ログファイル出力先に、logger.logファイルを作成して、実行してみる。

image.png

実行!

image.png
コンソールには出力された!
でも、logger.logファイルには何も表示されずwww
image.png

2. View.pyの内容を再度確認

image.png

loggerを設定している内容を再度確認すると、getlogger内の内容が、「application-logger」にしている。
これは、setting.pyの内容を見てみると、ありました!!!
image.png

さらに設定されている内容を確認すると、handlersに「Console_handler」が設定されている。
image.png
↓これ
image.png

これを、「timed_file_handler」に変えてみる。
image.png

再度実行!!
image.png

エラー発生!

Setting.pyの内容を再度確認すると、「timed_file_handler」の位置が間違ってました!!!
image.png

handlers内に移動し、再実行!

上手くいきました!
image.png

実行も成功!
image.png

Logger.logファイルにも無事書き込まれていました。
image.png

以上!

単純なミスでした。。。
こんなやつばっかりや。。。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?