ものづくりのブログ

うちのネコを題材にしたものづくりができたらいいなと思っていろいろ奮闘してます。

gcloud logging read で textPayload だけを表示する方法

gcloud logging read で textPayload だけを表示する(--format フラグの使い方)

はじめに

Cloud Run のログを gcloud CLI で確認するとき、こんなコマンドをよく使います。

gcloud logging read "resource.type=cloud_run_revision AND resource.labels.service_name=SERVICE_NAME" \
  --project=PROJECT_ID \
  --limit=50 \
  --format=json

ただ --format=json だと、ログ 1 件ごとに insertId / labels / resource などのメタデータがすべて付いてくるため、肝心のログ本文(textPayload)が埋もれてしまいます。

[
  {
    "insertId": "68b0c123000abcde...",
    "labels": {
      "instanceId": "0087..."
    },
    "logName": "projects/PROJECT_ID/logs/run.googleapis.com%2Fstdout",
    "receiveTimestamp": "2026-07-13T09:00:01.123456789Z",
    "resource": {
      "labels": {
        "configuration_name": "SERVICE_NAME",
        "location": "asia-northeast1",
        "project_id": "PROJECT_ID",
        "revision_name": "SERVICE_NAME-00012-abc",
        "service_name": "SERVICE_NAME"
      },
      "type": "cloud_run_revision"
    },
    "textPayload": "Server started on port 8080",
    "timestamp": "2026-07-13T08:59:59.987654321Z"
  }
]

この記事では、--format フラグで textPayload だけを取り出す方法を紹介します。

結論: --format="value(textPayload)"

gcloud logging read "resource.type=cloud_run_revision AND resource.labels.service_name=SERVICE_NAME" \
  --project=PROJECT_ID \
  --limit=50 \
  --format="value(textPayload)"

出力はログ本文だけが 1 行ずつ並ぶ形になります。

GET 200 /healthz 12 ms
Request processed: id=123
Server started on port 8080

--format には jsonyaml のような「出力形式」だけでなく、value(フィールド名) のように「どのフィールドを出力するか」まで指定できます。value() は指定フィールドの値だけをプレーンテキストで出力します。

なお gcloud logging read はデフォルトで新しいログから順(--order=desc)に出力されます。

バリエーション

タイムスタンプも一緒に表示する

--format="value(timestamp, textPayload)"

タブ区切りで出力されます。

2026-07-13T08:59:59.987654321Z   Server started on port 8080

表形式にする

--format="table(timestamp, textPayload)"

ヘッダー付きの表になります。

TIMESTAMP                       TEXT_PAYLOAD
2026-07-13T08:59:59.987654321Z  Server started on port 8080

JSON のままフィールドを絞る

jq などで後処理したい場合は、JSON のまま出力フィールドだけ絞れます。

--format="json(timestamp, textPayload)"
[
  {
    "textPayload": "Server started on port 8080",
    "timestamp": "2026-07-13T08:59:59.987654321Z"
  }
]

ハマりどころ

textPayload が空のログがある

アプリが JSON 形式の構造化ログを出力している場合、本文は textPayload ではなく jsonPayload に入るため、value(textPayload) では空行になります。対処は 2 つ。

textPayload を持つログだけに絞るなら、フィルタに textPayload:* を追加します。

gcloud logging read "resource.type=cloud_run_revision AND resource.labels.service_name=SERVICE_NAME AND textPayload:*" \
  --project=PROJECT_ID \
  --limit=50 \
  --format="value(textPayload)"

構造化ログの本文を見たいなら、jsonPayload 内のフィールドを直接指定します。

--format="value(jsonPayload.message)"

デフォルトでは直近 1 日分しか取得されない

gcloud logging read--freshness のデフォルトが 1d のため、フィルタにタイムスタンプ条件を書かない限り直近 1 日分しか対象になりません。それより前のログを見たい場合は指定します。

--freshness=7d

まとめ

  • textPayload だけ見たいなら --format="value(textPayload)"
  • タイムスタンプ付きなら value(timestamp, textPayload)table(timestamp, textPayload)
  • 構造化ログは jsonPayload に入るので、textPayload:* フィルタや value(jsonPayload.message) で対応

参考