Stripe CLIでは、stripe logs
コマンドで「いつ、どのAPIが呼び出されたか」のログを監視することができます。
% stripe logs --help
Tail Stripe API request logs in real-time and see debug information.
Usage:
stripe logs [command]
Available commands:
tail Tail API request logs from your Stripe requests.
Flags:
-h, --help help for logs
Global flags:
--api-key string Your API key to use for the command
--color string turn on/off color output (on, off, auto)
--config string config file (default is $HOME/.config/stripe/config.toml)
--device-name string device name
--log-level string log level (debug, info, warn, error) (default "info")
-p, --project-name string the project name to read from for config (default "default")
Use "stripe logs [command] --help" for more information about a command.
stripe logs tail
でAPIリクエストを監視する
strpe logs tail
コマンドを実行すると、test環境に対して送られたAPIリクエストのログが確認できるようになります。
% stripe logs tail
> Ready! You're now waiting to receive API request logs (^C to quit)
2021-03-09 18:05:53 [200] GET /v1/customers/:id [req_xxxx]
2021-03-09 18:05:53 [200] GET /v1/customers/:id/sources/:id [req_xxxxx]
2021-03-09 18:05:54 [200] GET /v1/plans [req_xxxxx]
2021-03-09 18:05:53 [200] GET /v1/invoices/upcoming [req_xxxx]
tail
コマンドは、test環境でのみ利用可能
注意: stripe logs tail
コマンドは、test環境でのみ利用できます。
本番のログをStripe CLIからモニタリングすることはできませんのでご注意ください。
確認できるログデータについて
ログには以下の5種類が出力されます。
- 呼び出し日時
- HTTPステータスコード
- リクエストの種類
- APIのパス
- Request ID
--format json
でJSON出力する
ログのフォーマットは、デフォルトまたはJSONから選ぶことができます。
JSONを指定すると、以下のように1行ずつJSON形式でログが出力されるようになります。
% stripe logs tail --format json
{"livemode":false,"method":"GET","url":"/v1/plans","status":200,"request_id":"req_I2UdDSUze6YVF6","created_at":1615281114,"error":null}
{"livemode":false,"method":"GET","url":"/v1/subscriptions","status":200,"request_id":"req_y16XdO7g3bzS7M","created_at":1615281114,"error":null}
{"livemode":false,"method":"GET","url":"/v1/customers/:id","status":200,"request_id":"req_g6jXTf2pGXTRLh","created_at":1615281114,"error":null}
APIリクエストの詳細をDashboardで確認する
気になるリクエストが見つかった場合、Stripe Dashboardからリクエストの詳細を見ることができます。
調べたいリクエストのIDをコピーし、stripe open dashboard
でStripe Dashboardを開きましょう。
その後、サイト上部の検索フォームにコピーしたリクエストIDを入力すると、該当するリクエストのログページに移動できます。
リクエスト詳細ページでは、「どんなリクエストを投げたか」や「どんなレスポンスを返したか」を確認することができます。
stripe open
やstripe get
コマンドなどでデータが取れればよいなと思ったのですが、v1.5.9ではまだ対応していない様子でした。もしStripe CLIで確認できる方法を知っていると言う方がいらっしゃいましたら、方法をお教えいただけると助かります。
filterオプションでログの内容を絞り込む
stripe logs tail
コマンドは、「そのアカウントに来たリクエストを全て表示する」動きをデフォルトでは行います。そのため、流通量の多いアカウントや複数のAPIを叩く作りになっているサービスなどではノイズが非常に多くなります。
そんな時に使いたいのが、filter
オプションです。以下のリストに簡単な紹介を載せましたが、様々な条件で表示する内容を絞り込むことができるようになっています。
- --filter-account[Stripe Connect限定]: Stripe Connectアカウント ID
- --filter-http-method:HTTPメソッド
- --filter-ip-address: リクエスト元のIPアドレス
- --filter-request-path:
/v1/charges
のようなStripe APIパス - --filter-request-status: HTTPリクエストステータス
- --filter-source: Dashboard / APIどちらの呼び出しか
- --filter-status-code: HTTPステータスコード
- --filter-status-code-type: レスポンスが(2XX / 4XX / 5XX)のいずれか
Filterの例1: Stripeがエラーを返した時のみログを出す
--filter-request-status FAILED
を設定すると、APIリクエストが失敗した時のログだけを見ることができます。
% stripe logs tail --filter-request-status FAILED
> Ready! You're now waiting to receive API request logs (^C to quit)
2021-03-09 18:31:59 [400] POST /v1/subscriptions [req_0gBDpSIsrwrg7Q]
Type: invalid_request_error
Code: parameter_missing
Message: Missing required param: customer.
Param: customer
Filterの例2: Subscriptionsなど、特定のリソースのログだけ表示する
--filter-request-path
を使うことで、特定のリソース(APIパス)のログだけを表示することもできます。
下の例では、Subscriptionsに関するリクエストのみをFilterしています。
% stripe logs tail --filter-request-path /v1/subscriptions
> Ready! You're now waiting to receive API request logs (^C to quit)
2021-03-09 18:33:26 [400] POST /v1/subscriptions [req_cdc4bS8R4nOIYP]
Type: invalid_request_error
Code: parameter_missing
Message: Missing required param: customer.
Param: customer
2021-03-09 18:33:34 [200] GET /v1/subscriptions [req_jcJ5sOwCyxmlmV]
2021-03-09 18:33:35 [200] GET /v1/subscriptions [req_isn3ltErEakRKZ]
Filterの例3: 特定のメソッドに対するHTTP400系エラーのみ表示する
「ちゃんと実装したはずなのに、Stripeがエラーを返してCustomerが作れない」など、開発・動作確認中に遭遇しやすいログを絞り込むのに使えるコマンドです。
filterは複数組み合わせることができますので、「POST
メソッドに対するHTTP4xxエラー
だけを表示する」ようにFilterを設定すれば、「意図しないリクエストを送ってしまっているケース」を絞り込むことができます。
% stripe logs tail \
--filter-http-method post \
--filter-status-code-type 4xx
> Ready! You're now waiting to receive API request logs (^C to quit)
2021-03-09 18:38:26 [400] POST /v1/subscriptions [req_ElXQ70oxRLaP4m]
Type: invalid_request_error
Code: parameter_missing
Message: Missing required param: customer.
Param: customer
stripe logs tail
コマンドで、Stripeのデバッグを効率化しよう
--live
オプションがv1.5.9時点で未対応のため、本番環境の監視にはまだ使うことができません。
ですが、リリース前の動作確認やtest環境でのバグの再現テストなど、このコマンドを実行してログを監視することでデバッグや修正、問題の発見がよりスムーズに行うことができます。
もしかすると、必要以上にAPIを呼び出しているケースなども見つかるかもしれませんので、ぜひ既にStripeをお使いの方は、ご自身のStripeアカウントに対してstripe logs tail
を実行してみてください。