Strukturiertes Logging und Korrelations-IDs
Lernen Sie, durchsuchbare strukturierte JSON-Logs zu erzeugen und Anfragen mithilfe von Korrelations-IDs über mehrere Funktionen hinweg zu verfolgen.
Strukturiertes Logging und Korrelations-IDs ist eine kostenlose Serverless AWS Lambda Development-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Serverless AWS Lambda Development-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Serverless AWS Lambda Development-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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 20The 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 30Tie 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.
Lerne Serverless AWS Lambda Development mit einem KI-Tutor — kostenlos
Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.
- Kurse
- 12
- Lektionen
- 48
Häufig gestellte Fragen
Ist die Lektion „Strukturiertes Logging und Korrelations-IDs“ kostenlos?
Ja — der vollständige Text von „Strukturiertes Logging und Korrelations-IDs“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Serverless AWS Lambda Development-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Serverless AWS Lambda Development-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Strukturiertes Logging und Korrelations-IDs“?
Lernen Sie, durchsuchbare strukturierte JSON-Logs zu erzeugen und Anfragen mithilfe von Korrelations-IDs über mehrere Funktionen hinweg zu verfolgen. Du übst Serverless AWS Lambda Development mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Serverless AWS Lambda Development zu starten?
Keine Vorkenntnisse erforderlich. Serverless AWS Lambda Development auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Strukturiertes Logging und Korrelations-IDs“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Serverless AWS Lambda Development-Lektion Code schreiben und ausführen?
Ja. Jede Serverless AWS Lambda Development-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Fortgeschrittene IAM-Richtlinien und Berechtigungen
- Geheimnisverwaltung mit AWS Secrets Manager
- Verteiltes Tracing mit AWS X-Ray
- Strukturiertes Logging und Korrelations-IDs