0Pricing
Serverless AWS Lambda Development · Lezione

Logging strutturato e correlation ID

Impari a produrre log JSON strutturati e ricercabili e a tracciare una richiesta attraverso più funzioni usando i correlation ID.

Logging strutturato e correlation ID è una lezione Serverless AWS Lambda Development gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Serverless AWS Lambda Development, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Serverless AWS Lambda Development include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Domande Frequenti

La lezione «Logging strutturato e correlation ID» è gratuita?

Sì — il testo completo di «Logging strutturato e correlation ID» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Serverless AWS Lambda Development, passa a CoddyKit PRO. Il corso Serverless AWS Lambda Development include 4 lezioni in totale.

Cosa imparerò in «Logging strutturato e correlation ID»?

Impari a produrre log JSON strutturati e ricercabili e a tracciare una richiesta attraverso più funzioni usando i correlation ID. Eserciti Serverless AWS Lambda Development con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Serverless AWS Lambda Development?

Non è richiesta alcuna esperienza precedente. Serverless AWS Lambda Development su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Logging strutturato e correlation ID»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Serverless AWS Lambda Development?

Sì. Ogni lezione Serverless AWS Lambda Development include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Policy e autorizzazioni IAM avanzate
  2. Gestione dei segreti con AWS Secrets Manager
  3. Tracciamento distribuito con AWS X-Ray
  4. Logging strutturato e correlation ID
← Torna a Serverless AWS Lambda Development