Logowanie strukturalne i identyfikatory korelacji
Dowiedz się, jak tworzyć przeszukiwalne, strukturalne logi JSON oraz śledzić żądanie w wielu funkcjach za pomocą identyfikatorów korelacji.
Logowanie strukturalne i identyfikatory korelacji to bezpłatna lekcja Serverless AWS Lambda Development na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Serverless AWS Lambda Development, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Serverless AWS Lambda Development zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Często zadawane pytania
Czy lekcja „Logowanie strukturalne i identyfikatory korelacji” jest bezpłatna?
Tak — pełny tekst „Logowanie strukturalne i identyfikatory korelacji” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Serverless AWS Lambda Development, przejdź na CoddyKit PRO. Kurs Serverless AWS Lambda Development zawiera 4 lekcji w sumie.
Co nauczysz się w „Logowanie strukturalne i identyfikatory korelacji”?
Dowiedz się, jak tworzyć przeszukiwalne, strukturalne logi JSON oraz śledzić żądanie w wielu funkcjach za pomocą identyfikatorów korelacji. Ćwiczysz Serverless AWS Lambda Development z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Serverless AWS Lambda Development?
Nie wymagamy żadnego doświadczenia. Serverless AWS Lambda Development w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Logowanie strukturalne i identyfikatory korelacji”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Serverless AWS Lambda Development?
Tak. Każda lekcja Serverless AWS Lambda Development zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Zaawansowane zasady IAM i uprawnienia
- Zarządzanie sekretami za pomocą AWS Secrets Manager
- Śledzenie rozproszone za pomocą AWS X-Ray
- Logowanie strukturalne i identyfikatory korelacji