Skip to content

非公式 / UnofficialCloudflare, Inc. 非提携。アカウント情報は入力しないでください。Not affiliated with Cloudflare, Inc. Do not enter Cloudflare credentials here.Official docsこのサイトについてプライバシー

Workers Logpush

最終更新 Markdown で表示Agent セットアップ

Cloudflare Logpush は、Workers Trace Event Logs を 対応する送信先 へ送れます。Workers Trace Events Logpush には、リクエストとレスポンスのメタデータ、構造化されていない console.log() メッセージ、捕捉されていない例外が含まれます。この製品は Workers Paid プランで利用できます。料金は Pricing を参照してください。

Logpush のアクセス権を確認する

Logpush ジョブを設定するには、Cloudflare アカウントのロールで Logpush を使えることを確認します。ロールの確認手順は次のとおりです。

  1. Cloudflare ダッシュボードで、Members ページを開きます。

    公式ダッシュボードで Members を開く ↗
  2. アカウントの権限を確認します。Logpush の設定権限は、Workers の権限とは異なります。Super Administrators、Administrators、Log Share ロールは、Logpush をフルに使えます。

別の方法として、アカウントレベルで Logs Edit 権限を持つ API トークン を作成します。

Logpush ジョブを作成する

Cloudflare ダッシュボードから

Cloudflare ダッシュボードで Logpush ジョブを作成する手順は次のとおりです。

  1. Cloudflare ダッシュボードで、Logpush ページを開きます。

    公式ダッシュボードで Logpush を開く ↗
  2. Create a Logpush job を選びます。

  3. 送信先を選び、必要に応じて設定します。

  4. データセットとして Workers trace events を選び、Next を選びます。

  5. 必要に応じてデータフィールドをカスタマイズします。不要な場合は Next を選びます。

  6. ダッシュボードの手順に従い、データの送信先の所有権を確認して、ジョブ作成を完了します。

cURL から

次の例は、Workers のログを R2 へ送ります。そのほかの設定オプションは、Logs ドキュメントの Enable destinations と API configuration を参照してください。

curl "https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/logpush/jobs" \
--header 'X-Auth-Key: <API_KEY>' \
--header 'X-Auth-Email: <EMAIL>' \
--header 'Content-Type: application/json' \
--data '{
  "name": "workers-logpush",
  "output_options": {
    "field_names": ["Event", "EventTimestampMs", "Outcome", "Exceptions", "Logs", "ScriptName"],
  },
  "destination_conf": "r2://<BUCKET_PATH>/{DATE}?account-id=<ACCOUNT_ID>&access-key-id=<R2_ACCESS_KEY_ID>&secret-access-key=<R2_SECRET_ACCESS_KEY>",
  "dataset": "workers_trace_events",
  "enabled": true
}' | jq .

Logpush では、フィルター と サンプリングレート を設定して、送信先へ送るデータ量をより細かく制御できます。たとえば、例外にならなかったリクエストのログだけを受け取りたい場合は、output_options の下に次の filter JSON プロパティを追加します。

"filter":"{\"where\": {\"key\":\"Outcome\",\"operator\":\"!eq\",\"value\":\"exception\"}}"

Worker でログを有効にする

ローカル開発

Wrangler ファイルに新しいプロパティ logpush = true を追加して、Worker でログを有効にします。トップレベルの設定、または 環境 の下に追加できます。このプロパティがある新しい Worker は、Logpush ジョブに自動で取り込まれます。

{
	"$schema": "./node_modules/wrangler/config-schema.json",
	// Top-level configuration
	"name": "my-worker",
	"main": "src/index.js",
	// Set this to today's date
	"compatibility_date": "2026-11-14",
	"workers_dev": false,
	"logpush": true,
	"route": {
		"pattern": "example.org/*",
		"zone_name": "example.org"
	}
}
"$schema" = "./node_modules/wrangler/config-schema.json"
name = "my-worker"
main = "src/index.js"
# Set this to today's date
compatibility_date = "2026-11-14"
workers_dev = false
logpush = true

[route]
pattern = "example.org/*"
zone_name = "example.org"

multipart script upload API で設定します。

curl --request PUT \
"https://api.cloudflare.com/client/v4/accounts/{account_id}/workers/scripts/{script_name}" \
--header "Authorization: Bearer <API_TOKEN>" \
--form 'metadata={"main_module": "my-worker.js", "logpush": true}' \
--form '"my-worker.js"=@./my-worker.js;type=application/javascript+module'

ダッシュボード

ダッシュボードで Logpush のログを有効にする手順は次のとおりです。

  1. Cloudflare ダッシュボードで、Workers & Pages ページを開きます。

    公式ダッシュボードで Workers & Pages を開く ↗
  2. 対象の Worker を選びます。

  3. Settings > Observability を開きます。

  4. Logpush で Enable を選びます(あらかじめ Logpush ジョブを作成 している場合のみ利用できます)。

制限

logs と exceptions フィールドは、合わせて 16,384 文字を超えると切り詰めが始まります。文字数は、すべての exception.name、exception.message、そのあと log.message の順で数えます。

文字数上限に達すると、1 件のメッセージに "<<<Logpush: *field* truncated>>>" が付いてフィールドが切り詰められ、それ以降のログや例外は破棄されます。

例

次の JSON の Logpush イベントで、上限が 50 文字(実際の上限は 16,384)だとします。アルゴリズムは次のように動きます。

  1. exception.names の文字数を数えます。
    1. "SampleError" と "AuthError" で 20 文字です。
  2. exception.message の文字数を数えます。
    1. "something went wrong" を 20 文字として数え、残りは 10 文字です。
    2. "unable to process request authentication from client" の先頭 10 文字を取り、切り詰めて "unable to <<<Logpush: exception messages truncated>>>" にします。
  3. log.message の文字数を数えます。
    1. すでに切り詰めが始まっているため、"Hello " は "<<<Logpush: messages truncated>>>" に置き換えられ、"World!" は破棄されます。

入力例

{
	"Exceptions": [
		{
			"Name": "SampleError",
			"Message": "something went wrong",
			"TimestampMs": 0
		},
		{
			"Name": "AuthError",
			"Message": "unable to process request authentication from client",
			"TimestampMs": 1
		}
	],
	"Logs": [
		{
			"Level": "log",
			"Message": ["Hello "],
			"TimestampMs": 0
		},
		{
			"Level": "log",
			"Message": ["World!"],
			"TimestampMs": 0
		}
	]
}

出力例

{
	"Exceptions": [
		{
			"name": "SampleError",
			"message": "something went wrong",
			"TimestampMs": 0
		},
		{
			"name": "AuthError",
			"message": "unable to <<<Logpush: exception messages truncated>>>",
			"TimestampMs": 1
		}
	],
	"Logs": [
		{
			"Level": "log",
			"Message": ["<<<Logpush: messages truncated>>>"],
			"TimestampMs": 0
		}
	]
}

役に立ちましたか?