Logging
Implement structured logging, log levels, and aggregation for effective observability.
你将学到
- 为什么 structured logging beats plain 文本
- Log levels and 当到 use 每个
- Log aggregation 使用 ELK stack
- 最佳实践 for searchable logs
概念讲解
Structured Logging
Traditional logging writes plain text messages: "用户 42 logged in at 10:30". This is easy for humans to read but hard for machines to search and analyze. Structured logging writes logs as JSON: {"level":"信息","用户":42,"事件":"登录","时间":"2024-01-01T10:30:00Z"}. Each field is searchable, filterable, and aggregatable. In production, you almost always want structured logs because log aggregation tools can index and query them efficiently.
Log Levels
Log levels categorize messages by severity. DEBUG is for detailed diagnostic information, off in production. INFO records normal operations — server started, request served. WARN indicates something unexpected but not fatal — a deprecated API was used, a retry succeeded. ERROR indicates a failure that needs attention — a request failed, a connection was lost. FATAL is for unrecoverable errors that crash the process. Set the minimum level per environment: DEBUG in development, INFO or WARN in production.
Log Aggregation
On a single server, you can read logs with tail and grep. But with dozens of containers and services, you need log aggregation — a central system that collects, indexes, and searches logs from all sources. The ELK stack (Elasticsearch, Logstash, Kibana) is a popular open-source choice. Loki is a lighter alternative. Datadog and Splunk are commercial options. These tools let you search across all services, filter by time and field, and create visualizations.
Best Practices
Log meaningful events, not every line of code. Include a request ID or trace ID in every log so you can follow a single request across services. Never log secrets — passwords, tokens, credit card numbers. Use consistent field names across services. Include enough context to diagnose problems: the user ID, the input that caused the error, the relevant configuration. Rotate and retain logs according to your compliance requirements.
Correlation IDs
A correlation ID (or trace ID) is a unique identifier attached to each incoming request and passed to all downstream services and log entries. When something goes wrong, you search for the correlation ID and see the complete journey of that request across every service it touched. This is essential for debugging distributed systems where a single user request may span five or more microservices. Generate the ID at the entry point (API gateway or middleware) and propagate it through HTTP headers, message queues, and database queries.
示例代码
# Structured logging in Node.js (JSON format)
# {"level":"info","time":"2024-01-01T10:30:00Z","msg":"Request served",
# "method":"GET","path":"/api/users","status":200,"durationMs":42}
# View and filter logs on a server
tail -f /var/log/myapp.log # follow logs live
grep '"level":"error"' /var/log/myapp.log # filter errors
jq 'select(.status >= 500)' < logs.json # find server errors
# Docker container logs
docker logs -f --since 10m myapp # last 10 minutes
docker logs myapp 2>&1 | grep ERROR # filter errors
# Log rotation with logrotate
# /etc/logrotate.d/myapp:
# /var/log/myapp.log { daily rotate 30 compress copytruncate }
Structured logs 是 JSON 使用 searchable 字段. tail and grep view logs live, jq 筛选 JSON logs, and logrotate manages log file rotation 到 prevent disk fill.
动手试一试
- Timestamp Converter
Convert log timestamps for analysis.
- JSON Schema Validator
验证 structured log JSON 格式.
- Regex Tester
测试 regex 模式 for log parsing.
常见错误
错误写法
Logging sensitive 数据如 密码 or 令牌
正确做法
从不 log secrets. Use logging 筛选 or sanitizer 那个 redacts known sensitive 字段. Review log 输出 regularly 到 ensure 否 secrets leak 到 logs.
错误写法
Using console.log 不使用结构
正确做法
Use structured logger 如 pino (节点.js) or structlog (Python) 那个输出 JSON. Plain 文本 logs 是困难到 搜索 and analyze at scale.
练习 Logging
2 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。