Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ログを構造化ログに変更 #17

Merged
merged 4 commits into from
Sep 12, 2024
Merged

ログを構造化ログに変更 #17

merged 4 commits into from
Sep 12, 2024

Conversation

kobayashi-m42
Copy link
Member

@kobayashi-m42 kobayashi-m42 commented Sep 10, 2024

issueURL

#14

この PR で対応する範囲 / この PR で対応しない範囲

以下について対応する

  • カスタムのLoggerを定義し、JSON形式でログを出力する
  • 既存のログ出力箇所を作成したloggerで置き換える
  • ログ出力処理の追加

変更点概要

Loggerを定義し、既存のログ出力箇所を変更。

以下の点が実現できるようのLoggerを作成した。

  • 形式はJSON
  • 以下の項目が全てのログに含まれる
    • Lambdaのrequest ID
    • 画像の加工工程(processType)
    • 画像加工の対象画像のS3バケット名
    • 画像加工の対象画像のオブジェクトキー

出力内容: infoの場合

{
    "requestId": "154da9ff-ef3b-4fa2-93d2-8348d475af93",
    "processType": "generateLgtmImage",
    "bucketName": "stg-lgtmeow-cat-images",
    "objectKey": "2024/06/30/00/cfe8eb1f-1743-4bc4-a09a-4b01ebf0cc31.jpe",
    "filename": "s3_repository.py",
    "funcName": "fetch_image",
    "levelname": "INFO",
    "lineno": 24,
    "module": "s3_repository",
    "message": "画像の取得を開始",
    "pathname": "/var/task/infrastructure/s3_repository.py",
    "timestamp": "2024-09-10 11:29:14,985",
    "level": "INFO"
}

出力内容: errorの場合

{
    "requestId": "154da9ff-ef3b-4fa2-93d2-8348d475af93",
    "processType": "generateLgtmImage",
    "bucketName": "stg-lgtmeow-cat-images",
    "objectKey": "2024/06/30/00/cfe8eb1f-1743-4bc4-a09a-4b01ebf0cc31.jpe",
    "filename": "generate_lgtmI_image_usecase.py",
    "funcName": "execute",
    "levelname": "ERROR",
    "lineno": 98,
    "module": "generate_lgtmI_image_usecase",
    "message": "An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.",
    "pathname": "/var/task/usecase/generate_lgtmI_image_usecase.py",
    "timestamp": "2024-09-10 11:29:15,273",
    "level": "ERROR",
    "exception": "Traceback (most recent call last):\n  File \"/var/task/usecase/generate_lgtmI_image_usecase.py\", line 77, in execute\n    cat_image = self.s3repository.fetch_image(self.bucket_name, self.object_key)\n                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/var/task/infrastructure/s3_repository.py\", line 26, in fetch_image\n    response = self.s3_client.get_object(Bucket=bucket_name, Key=object_key)\n               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/var/task/botocore/client.py\", line 569, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/var/task/botocore/client.py\", line 1023, in _make_api_call\n    raise error_class(parsed_response, operation_name)\nbotocore.errorfactory.NoSuchKey: An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist."
}

レビュアーに重点的にチェックして欲しい点

標準のLoggerのようにextraを利用して任意のキーでログに値を出力できない点が、今回作成したLoggerの少し気になるところ...
この点について、許容範囲なのか、それとも問題なのか判断がついていないから、コメントしてもらえると嬉しい!

@kobayashi-m42 kobayashi-m42 marked this pull request as ready for review September 10, 2024 13:16
@kobayashi-m42 kobayashi-m42 self-assigned this Sep 10, 2024
Copy link
Member

@keitakn keitakn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとう🐱

一個だけコメントしたよ👍

ちなみに

標準のLoggerのようにextraを利用して任意のキーでログに値を出力できない点が、今回作成したLoggerの少し気になるところ...
この点について、許容範囲なのか、それとも問題なのか

これは必要な情報が全て出力出来ているから許容範囲かなと👍
各機能毎にロギングで出力する項目が大きく異なる場合は外から extra を受け取れたほうが良いけどそれが無さそうであればこれで良いかなと思う👌

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

badge

AppLogger はDomainでもPresentationでも使うから logger もしくは log って独自packageを切っても良いかなと思った🐱

ロギングはどの層にも当てはまらないし、どの層から依存されるか分からないのが大きな理由かな、これならどの層でも気軽にログを使えるから良いかと👍

https://zenn.dev/keitakn/articles/python-fastapi-onion-architecture-simplified#%E3%83%AD%E3%82%AE%E3%83%B3%E3%82%B0%EF%BC%88src%2Flog%EF%BC%89

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keitakn ありがとう!

確かに、どの層からも利用されることを考えるとlogパッケージがあった方が良いかと思った!
以下で修正したよ!

ログはどの層からも利用されるため、src直下にlogパッケージを作成しログ出力処理を移動

@kobayashi-m42
Copy link
Member Author

@keitakn

これは必要な情報が全て出力出来ているから許容範囲かなと👍
各機能毎にロギングで出力する項目が大きく異なる場合は外から extra を受け取れたほうが良いけどそれが無さそうであればこれで良いかなと思う👌

現状だとログの出力項目が変わることはないから extra はなくても良さそうだね!
必要になったタイミングで課題を追加して対応するね!

@kobayashi-m42
Copy link
Member Author

@keitakn LGTM画像を作成した際に画像の形式をPNGに変換しているのに、画像をアップロードする際の拡張子が.pngになっていなかったから、変換する処理を追加したよ!こちらも確認よろしくです!

アップロードするobject_keyの拡張子をpngに変換

Copy link
Member

@keitakn keitakn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとう🐱

#17 (comment) のコメントの件も含めて問題ないと思う👍

LGTMeow

@kobayashi-m42 kobayashi-m42 merged commit d34706a into main Sep 12, 2024
3 checks passed
@kobayashi-m42 kobayashi-m42 deleted the feature/issue14 branch September 12, 2024 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants