Hacker News 中文摘要

RSS订阅

日志记录真糟糕 -- Logging Sucks

文章摘要

文章指出当前日志记录方式存在缺陷,通过示例展示日志常包含无关或误导性信息,建议改进日志内容,使其更准确、有用。

文章总结

标题:日志系统的缺陷与改进之道

主要内容:

  1. 当前日志系统的问题
  • 传统日志设计已无法适应现代分布式系统需求
  • 日志缺乏上下文信息,难以有效排查问题
  • 字符串搜索效率低下,无法建立事件关联
  • 日志内容过于分散(一个请求产生13条日志)
  1. 关键概念解析
  • 结构化日志:键值对形式的日志记录
  • 基数性:字段唯一值的数量(高基数字段更有价值)
  • 维度性:日志事件的字段数量
  • 宽事件:单个请求的完整上下文记录
  1. 现有方案的局限性
  • OpenTelemetry仅解决数据传输问题
  • 传统追踪系统无法提供完整业务上下文
  • 随机采样可能导致关键事件丢失
  1. 解决方案:宽事件模式
  • 每个请求生成一条包含所有上下文的日志事件
  • 包含业务指标(用户等级、购物车金额等)
  • 支持尾部采样(保留错误、慢请求等关键事件)
  • 实现方式:通过中间件收集请求全生命周期数据
  1. 实施效果
  • 支持复杂查询分析(如"查询过去1小时高级用户使用新结账流程的失败情况")
  • 将调试过程从文本搜索转变为数据分析
  • 显著提高问题排查效率
  1. 常见误区澄清
  • 结构化日志≠宽事件
  • OpenTelemetry不能替代业务上下文记录
  • 高基数数据在现代列式数据库中已不是问题

这篇文章系统性地分析了传统日志系统的缺陷,并提出了通过"宽事件"模式进行改进的方案,强调在日志中记录完整的业务上下文信息,使日志真正成为有效的诊断工具。

评论总结

评论总结

1. 关于日志的现状与问题

  • 观点1:传统日志已不适应现代分布式系统,需要改进。

    • 引用:"Logs were designed for a different era... Your logs are still acting like it's 2005."(评论4)
    • 引用:"The real problem is unstructured events + weak conventions + poor correlation."(评论7)
  • 观点2:日志本身没有问题,问题在于使用方式。

    • 引用:"Logs are fine. The job of local logs is to record the talk of a local process."(评论6)
    • 引用:"But does it? Or is it bad logging, or excessive logging, or unsearchable logs?"(评论21)

2. 改进日志的建议

  • 观点1:采用结构化日志和关联ID(如requestID、correlationID)。

    • 引用:"structured logs that include a correlation ID make it quite easy to narrow down the general area or exact cause of problems."(评论14)
    • 引用:"Everyone these days uses requestIDs to be able to query all log lines emanated by a single request."(评论23)
  • 观点2:使用OpenTelemetry等工具提升日志功能。

    • 引用:"Slapping on OpenTelemetry actually will solve your problem."(评论15)
    • 引用:"Overly dismissive of OTLP without proper substance to the criticism."(评论26)

3. 关于“宽事件”(Wide Events)的讨论

  • 观点1:支持宽事件,认为其能提供更全面的上下文。

    • 引用:"Instead of logging what your code is doing, log what happened to this request."(评论17)
    • 引用:"wide events... actually adds something valuable."(评论2)
  • 观点2:反对宽事件,认为其增加复杂性和可读性问题。

    • 引用:"We’re throwing away grep \"uid=user-123\" application.log to get what?"(评论20)
    • 引用:"Increasing logging by 20-100x when degraded is not that."(评论9)

4. 其他观点

  • 关于架构:部分评论认为问题源于微服务架构本身。

    • 引用:"If a user request is hitting that many things, in my view, that is a deeply broken architecture."(评论4)
    • 引用:"Maybe better written and simplified to: 'microservices suck'."(评论28)
  • 关于标准化:建议统一日志字段命名。

    • 引用:"A common problem in a log aggregation is the question if you query for user.id, user_id, userID..."(评论19)

总结

评论中对日志问题的看法分为两派:一派认为传统日志已过时,需采用结构化日志、宽事件等改进;另一派认为问题在于日志的使用方式而非日志本身。OpenTelemetry和关联ID被多次提及作为解决方案。此外,部分评论对微服务架构和日志标准化提出了批评和建议。