Logging, configuration, and secrets
Use structured logs, read config from the environment, and keep secrets out of code.
Why it matters: When an LLM call fails at night, logs are the trail. Leaked API keys are expensive.
Basics
logging instead of print
print has no levels and no easy off switch. logging does.
Levels: DEBUG, INFO, WARNING, ERROR, CRITICAL.
Use logging.getLogger(__name__). Configure handlers once at the entry point.
Log the event plus context: request id, model, tokens, latency.
Tiny example
Log one INFO line with a latency value.
import logging
logging.basicConfig(level=logging.INFO)
log = logging.getLogger("rag")
log.info("retrieved %d chunks in %d ms", 4, 87)Basics
Structured logs
One JSON object per line is easy to query in log tools.
Include a correlation id on every line for one request.
Do not log full prompts with personal data. Log length and a short preview.
Tiny example
Print one JSON log line with a request id.
import json
print(json.dumps({"event": "llm_call", "request_id": "r1", "ms": 640}))Basics
Config and secrets
Read config from environment variables.
Required: os.environ["OPENAI_API_KEY"]. Optional: os.getenv("LOG_LEVEL", "INFO").
Keep .env local and gitignored. Commit .env.example with key names only.
Never hardcode API keys in source or notebooks.
Tiny example
Read an optional env var with a default.
import os
level = os.getenv("LOG_LEVEL", "INFO")
print("log_level:", level)Then AI / ML
AI / ML examples
Same idea, used in real AI work. Press Try in lab to run it.
AI / ML example
AI / ML: log an LLM call
One INFO line with model, size, and latency.
In AI / ML: Every model call should log request id, model, tokens, and latency.
example.py
Python
import logging
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
log = logging.getLogger("service")
def log_llm_call(request_id, model, prompt, latency_ms):
log.info(
"llm_call id=%s model=%s chars=%d ms=%d",
request_id, model, len(prompt), latency_ms,
)
log_llm_call("req-1", "mini", "What is RAG?", 640)AI / ML example 2
AI / ML: redact secrets before logging
Hash and truncate; never write the raw key.
In AI / ML: API keys and PII prompts are redacted before any log line is written.
example.py
Python
import hashlib
def redact(secret):
if not secret:
return "<unset>"
digest = hashlib.sha256(secret.encode()).hexdigest()[:8]
return f"{secret[:3]}***{digest}"
print("key:", redact("sk-live-abc123def456"))
print("chars:", len("My account number is 998877"))Takeaways
- 1Use logging with levels and context; keep print for scratch work.
- 2JSON logs plus a request id make production debugging possible.
- 3Secrets come from the environment - never from source control.