0Pricing
Serverless AWS Lambda Development · 课时

结构化日志与关联 ID

学习生成便于搜索的结构化 JSON 日志,并使用关联 ID 跨多个函数跟踪请求。

结构化日志与关联 ID 是 CoddyKit 上的免费 Serverless AWS Lambda Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Serverless AWS Lambda Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Serverless AWS Lambda Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Plain Logs Do Not Scale

Free-text log lines are hard to filter at scale. Structured logging writes each entry as JSON with consistent fields you can query.

Anatomy of a Structured Log

A good log entry has a level, a message, and contextual fields like request id and user id.

{
  "level": "INFO",
  "message": "order placed",
  "orderId": 42,
  "requestId": "abc-123"
}

Emitting JSON Logs

Serialize a dictionary to JSON and print it. CloudWatch Logs Insights can then query individual fields.

import json, sys

def log(level, message, **fields):
    entry = {'level': level, 'message': message}
    entry.update(fields)
    print(json.dumps(entry), file=sys.stdout)

log('INFO', 'order placed', orderId=42)

Querying with Logs Insights

Logs Insights lets you filter and aggregate JSON fields with a query language.

fields @timestamp, orderId
| filter level = 'ERROR'
| sort @timestamp desc
| limit 20

The Correlation ID

A correlation ID is a unique value attached to a request and passed to every downstream service, so you can trace one request end to end.

Generating One

Create the ID at the entry point if the incoming request does not already carry one.

import uuid

def get_correlation_id(event):
    headers = event.get('headers') or {}
    return headers.get('x-correlation-id') or str(uuid.uuid4())

Propagating It

Pass the correlation ID forward in message attributes, HTTP headers, or event payloads so the next function logs the same ID.

sns.publish(
  TopicArn=topic,
  Message=body,
  MessageAttributes={
    'correlationId': {'DataType': 'String', 'StringValue': cid}
  }
)

Log Levels

Use levels (DEBUG, INFO, WARN, ERROR) and make the threshold configurable via an environment variable so production stays quiet but debuggable.

Never Log Secrets

Structured logs are searchable, which makes accidental secret logging dangerous. Redact tokens, passwords, and PII before logging.

Control Log Retention

Logs cost money to store. Set a retention period on each log group so old logs expire automatically.

aws logs put-retention-policy \
  --log-group-name /aws/lambda/orders \
  --retention-in-days 30

Tie Logs to Traces

Include the X-Ray trace id in your structured logs so you can jump from a log line straight to the distributed trace for that request.

Quick Check

Test your logging knowledge.

Recap

You learned structured JSON logging, querying with Logs Insights, threading correlation IDs across services, redacting secrets, and setting retention.

常见问题解答

「结构化日志与关联 ID」课时是免费的吗?

是的 — 「结构化日志与关联 ID」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Serverless AWS Lambda Development 课程的其余内容,请升级到 CoddyKit PRO。 Serverless AWS Lambda Development 课程共包含 4 节课。

「结构化日志与关联 ID」这节课中我会学到什么?

学习生成便于搜索的结构化 JSON 日志,并使用关联 ID 跨多个函数跟踪请求。 你通过在浏览器中直接运行的动手代码来练习 Serverless AWS Lambda Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Serverless AWS Lambda Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Serverless AWS Lambda Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「结构化日志与关联 ID」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Serverless AWS Lambda Development 课中编写并运行代码吗?

能。每节 Serverless AWS Lambda Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 高级 IAM 策略与权限
  2. 使用 AWS Secrets Manager 管理机密
  3. 使用 AWS X-Ray 进行分布式追踪
  4. 结构化日志与关联 ID
← 返回 Serverless AWS Lambda Development