0Pricing
Serverless AWS Lambda Development · บทเรียน

การบันทึกแบบมีโครงสร้างและรหัสติดตามความสัมพันธ์

เรียนรู้การสร้างบันทึก JSON แบบมีโครงสร้างที่ค้นหาได้ และติดตามคำขอข้ามฟังก์ชันหลายรายการด้วยรหัสติดตามความสัมพันธ์

การบันทึกแบบมีโครงสร้างและรหัสติดตามความสัมพันธ์ เป็นบทเรียน Serverless AWS Lambda Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.

คำถามที่พบบ่อย

บทเรียน “การบันทึกแบบมีโครงสร้างและรหัสติดตามความสัมพันธ์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การบันทึกแบบมีโครงสร้างและรหัสติดตามความสัมพันธ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless AWS Lambda Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การบันทึกแบบมีโครงสร้างและรหัสติดตามความสัมพันธ์”

เรียนรู้การสร้างบันทึก JSON แบบมีโครงสร้างที่ค้นหาได้ และติดตามคำขอข้ามฟังก์ชันหลายรายการด้วยรหัสติดตามความสัมพันธ์ คุณปฏิบัติ Serverless AWS Lambda Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless AWS Lambda Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless AWS Lambda Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การบันทึกแบบมีโครงสร้างและรหัสติดตามความสัมพันธ์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Serverless AWS Lambda Development นี้ได้ไหม

ได้ บทเรียน Serverless AWS Lambda Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. นโยบาย IAM และสิทธิ์ขั้นสูง
  2. การจัดการข้อมูลลับด้วย AWS Secrets Manager
  3. การติดตามแบบกระจายด้วย AWS X-Ray
  4. การบันทึกแบบมีโครงสร้างและรหัสติดตามความสัมพันธ์
← กลับไปที่ Serverless AWS Lambda Development