DDevArchive
Đăng nhập

Logging tập trung: từ log rác đến truy vết có cấu trúc

Log vô tổ chức là "nhiều tiếng ồn". Tập trung log từ mọi service vào một chỗ, tìm kiếm được, và vẽ dashboard. Truy vết sự cố từ 2 giờ xuống 2 phút.

Log có cấu trúc vs log chuỗi

Log chuỗi: console.log(“User login failed”) — không biết user nào, lúc nào, ở service nào. Log có cấu trúc dùng JSON: mỗi field tìm kiếm được, lọc được, vẽ biểu đồ được.

// File: log.js
// ❌ Log chuỗi: khó tìm, khó lọc
console.log("Login failed for user " + email)

// ✅ Log có cấu trúc: JSON, tìm kiếm được
logger.warn({
  event: "login_failed",
  email: email,
  ip: req.ip,
  reason: "wrong_password",
  timestamp: new Date().toISOString(),
  requestId: req.id,
})

Công cụ tập trung log

Công cụVai tròFree/Paid
ELK (Elasticsearch + Logstash + Kibana)Self-hosted, mạnh nhấtFree (self-managed)
Loki + GrafanaNhẹ hơn ELK, tích hợp GrafanaFree (self-managed)
DatadogSaaS, dễ dùng, đắtPaid
Pino (Node.js)Library ghi JSON log nhanhFree

Correlation ID: truy vết xuyên service

Khi request đi qua nhiều service, mỗi service log riêng. Làm sao biết log nào thuộc cùng request? Gắn correlation ID (request ID) vào mỗi log — tìm theo ID này sẽ thấy toàn bộ hành trình.

// File: correlation.js
import { v4 as uuid } from 'uuid'

// Middleware tạo correlation ID cho mỗi request
function correlationMiddleware(req, res, next) {
  req.correlationId = req.headers['x-correlation-id'] || uuid()
  res.setHeader('x-correlation-id', req.correlationId)
  next()
}

// Mọi log entry đều gắn correlationId
function logRequest(req, message, data = {}) {
  logger.info({
    correlationId: req.correlationId,
    method: req.method,
    path: req.path,
    message,
    ...data,
  })
}
💡 Không log dữ liệu nhạy cảm

TUYỆT ĐỐI không log: mật khẩu, token, số thẻ tín dụng, CMND. Dùng hàm mask trước khi log: email → t***@gmail.com, card → ****1234.

❓ Correlation ID giải quyết vấn đề gì?

  • Viết log có cấu trúc JSON thay vì chuỗi
  • Biết ít nhất 2 công cụ tập trung log
  • Triển khai correlation ID middleware
  • Không log dữ liệu nhạy cảm