前言:

本文翻译来自 Log Everything All the Time,其中个人省略了少量内容,如果有需要,请阅读原文。本文来自 博客园 逖靖寒 http://gpcuster.cnblogs.com

译文:

本次的JoelOnSoftware 问答活动中,提到了一个古老的问题,什么是log以及如何去log。平常的trace/error/warning/info方式在大型的分布式系统中不是非常有用。你需要将所有的信息都记录下来才能解决遇到的问题。

为了解释为什么常用的log方式不好用,可以想象这样一种情形:你的网站运行了好几周时间都没有问题,可是突然有1天,凌晨2点的时候出现了一个问题,用户有时无法提交评论,这个现象时断时续。你现在需要去修复这个问题。

那么,我们如何去找到这个问题并修复它呢?监控系统并没有任何异常的现象,你自己提交一个评论去测试,运行正常,没有问题。看来这个问题不是那么好解决的,因为提交一个评论涉及了许多的东西,比如说:负载平衡系统,垃圾过滤,web服务器,数据库服务器,缓存服务器,文件服务器,还有交换机和路由器等等,究竟是什么地方出问题了呢?

这个时候,你所拥有的只有log。你不能关闭你的系统因为用户正在使用它,你不能更新部署一个新的系统因为你没有环境去测试新的系统是否会进入新的问题,添加debugger也无法解决这个问题。

你需要做的事情就是查看这些log,看看会其中记录了什么信息。你不需要函数或是方法的信息,你需要知道系统中所有有意思的事情发生的记录,知道这个叫“func1”的函数被调用没有任何帮助。你需要知道什么参数被传递给了函数,函数的返回值是什么。

所以,这里没有log的级别之分。你需要记录所有的信息来帮助你在将来遇到问题的时候解决问题。你真正需要的是一个时光机,虽然这是不现实的,但是主要我们的log足够详细,那么就可以认为我们拥有一个时光机。他将帮助你了解到期间发生的所有事情:是不是一个接口丢失了一个包数据,是不是相应超时了,是不是互斥锁没有正确使用?等等。

绝大多数系统都是慢慢发展到去记录所有的东西的。它们开始的时候只记录一点点信息甚至什么都不记录。当发生问题以后,它们会增加记录的内容。但是log通常没有很好的分类与整理,这将导致不好的问题覆盖率和降低程序的性能。

程序反常一般通过log来查找,反常就是一些没有预料到的东西,比如说操作,处理顺序,计算时间过长等等。不过这些反常的现象也有好处,它会告诉你如何让自己的程序更加健壮,让你知道如何在真实的环境中去处理相关的问题。

所以,好好想一下你需要调试哪些问题。不要害怕去添加log帮助你了解系统真正是如何工作的。

比如说,给每一个请求需要分配一个全局唯一的ID,这样你就区分不同请求的相关信息了,帮助你提供调试的效率和准确性。

通常log有2个等级:系统级别和开发级别。

系统级别的log会记录所有你需要去调试系统的日志,它将一直存在,不会被关闭。

开发级别的日志将添加更加详细的信息,并且可以以模块为单位开启或者是关闭。

我通常会用一个配置文件,里面定义了默认的输出级别。不过我让每一个进程通过相应的接口改变自己的输出级别。这样在开发的时候就会非常的方便。

我时常听到这样的言论:记录所有的信息效率非常低,会产生过多的数据。我不这么认为。我参加过一些项目,其中有的是实时的嵌入式系统,他们都会记录下所有的信息,甚至是驱动程序,他们都是这么做的。

下面有一些与log相关的技巧(感觉原文说得更加到位,就不翻译了):

Make logging efficient from the start so you aren't afraid to use it. Create a dead simple to use log library that makes logging trivial for developers. Document it. Provide example code. Check for it during code reviews. Log to a separate task and let the task push out log data when it can. Use a preallocated buffer pool for log messages so memory allocation is just pop and push. Log integer values for very time sensitive code. For less time sensitive code sprintf'ing into a preallocated buffer is usually quite fast. When it's not you can use reference counted data structures and do the formatting in the logging thread. Triggering a log message should take exactly one table lookup. Then the performance hit is minimal. Don't do any formatting before it is determined the log is needed. This removes constant overhead for each log message. Allow fancy stream based formatting so developers feel free to dump all the data they wish in any format they wish. In an ISR context do not take locks or you'll introduce unbounded variable latency into the system. Directly format data into fixed size buffers in the log message. This way there is no unavoidable overhead. Make the log message directly queueable to the log task so queuing doesn't take more memory allocations. Memory allocation is a primary source of arbitrary latency and dead lock because of the locking. Avoid memory allocation in the log path. Make the logging thread a lower priority so it won't starve the main application thread. Store log messages in a circular queue to limit resource usage. Write log messages to disk in big sequential blocks for efficiency. Every object in your system should be dumpable to a log message. This makes logging trivial for developers. Tie your logging system into your monitoring system so all the logging data from every process on every host winds its way to your centralized monitoring system. At the same time you can send all your SLA related metrics and other stats. This can all be collected in the back ground so it doesn't impact performance. Add meta data throughout the request handling process that makes it easy to diagnose problems and alert on future potential problems. Map software components to subsystems that are individually controllable, cross application trace levels aren't useful. Add a command ports to processes that make it easy to set program behaviors at run-time and view important statistics and logging information. Log information like task switch counts and times, queue depths and high and low watermarks, free memory, drop counts, mutex wait times, CPU usage, disk and network IO, and anything else that may give a full picture of how your software is behaving in the real world.

log的数据是你调试绝大多数大型分布式系统的根据。

所以,从现在开始,记录所有的日志,当开始提到的那个凌晨2点钟的问题再次发生时,你就知道如何应对,修改问题了:)

本文来自 博客园 逖靖寒 http://gpcuster.cnblogs.com

[翻译]Log Everything All the Time相关推荐

  1. 第14周翻译Stairway to Transaction Log Management in SQL Server, Level 5: Managing the Log in Full Rec...

    来源:Stairway to Transaction Log Management in SQL Server, Level 5: Managing the Log in Full Recovery ...

  2. 第14周翻译:Stairway to Transaction Log Management in SQL Server, Level 5: Managing the Log in Full Recov

    在本节中,我们将回顾在完全恢复模式下进行日志备份的原因和方法,以及如何使用这些日志备份文件与完整的数据库备份一起执行数据库恢复.完全恢复模式支持将数据库恢复到可用日志备份中的任意时间点,并假设可以进行 ...

  3. “Attention is All You Need 翻译

    <p><img src="output_0_0.png" alt="png"></p> "Attention is ...

  4. 翻译-高质量JavaScript代码书写基本要点(转载)

    by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpress/?p=1173 原文作者:S ...

  5. Android中对Log日志文件的分析[转]

    一,Bug出现了, 需要"干掉"它 bug一听挺吓人的,但是只要你懂了,android里的bug是很好解决的,因为android里提供了LOG机制,具体的底层代码,以后在来分析,只 ...

  6. 熬夜翻译完的PureFTPd配置文件

    [url]http://www.chinaunix.net[/url] 作者:jeffwu  发表于:2006-07-08 10:31:58 干了个通宵,一边玩一边把配置文件翻译完了,翻得不好的地方还 ...

  7. Loader 入门【Webpack Book 翻译】

    原文链接:https://survivejs.com/webpack... 翻译计划:https://segmentfault.com/a/11... 附言:因为发现书中一些内容单独放出来会比较尴尬, ...

  8. 如何超越console.log并充分利用浏览器的调试控制台

    by Gilad Dayagi 通过吉拉德·达亚吉 The console object is a very useful feature of browsers that has been arou ...

  9. [转载]IPMSG(飞鸽传书)协议翻译

    /********************************************************** *本人(ypxing)根据下面的协议,C语言写的ipmsg(聊天,文件/文件夹传 ...

最新文章

  1. 如何从文件内容创建Java字符串?
  2. C#操作Excel,权限问题
  3. js控制input框输入数字时,累计求和
  4. 网络爬虫requests-bs4-re-1
  5. Keras-数据增广
  6. linux中死锁的概念,【Linux】死锁概念总结
  7. 苹果11如何设置9宫格_iphone九宫格如何设置 iphone九宫格设置方法【详解】
  8. 8086状态标志寄存器含义
  9. VirtualBox安装MACOSX 10.13虚拟机
  10. php 游戏开发swoole,用Swoole来写个联机对战游戏呀!(一)前言
  11. 奇异网盘点全球10大最荒诞的“时髦”事件
  12. Jetseon TX2 IntelRealsense D435i Python
  13. 使用安装Ubuntu和Win7双系统
  14. 用自己的路由器建立自己的服务器之创建网页
  15. TensorFlow学习(11)——卷积神经网络
  16. 适合编程初学者的开源项目:小游戏2048(微信小程序版)
  17. 随机变量与随机过程详解
  18. HTML标签的基本使用:无序列表、有序列表、定义列表
  19. matlab2020b中的nargin函数报错问题
  20. lammps构建高熵合金模型+结构优化初步筛选能量最小的结构

热门文章

  1. 程序员必知的mysql插件_程序员必知的技术官网系列--mysql篇
  2. 山大计算机学院副院长屠长河,留学交流系列——山东大学计算机学院访问澳门大学...
  3. centos6 java安装_CentOS6下安装Java JDK8
  4. CSS中的contenteditable属性
  5. 某学校新买了一批计算机,2020年度初中信息技术考试选择题.doc
  6. sql server oracle特点,SQL Server 和 Oracle 以及 MySQL 有哪些区别
  7. 湖北文理学院学位计算机考试,湖北文理学院学位计算机考试试题答案.doc
  8. redis入门综合概要介绍
  9. 充电枪cp信号控制板_筋膜枪究竟是不是智商税?评测后,我的回答更坚定了
  10. traceroute显示*号_traceroute 的名词解释