写点什么

程序那些事:日志记录的作用和方法

  • 2012-11-06
  • 本文字数:2271 字

    阅读完需:约 7 分钟

程序中记录日志一般有两个目的:Troubleshooting 和显示程序运行状态。好的日志记录方式可以提供我们足够多定位问题的依据。日志记录大家都会认为简单,但如何通过日志可以高效定位问题并不是简单的事情。这里列举下面三个方面的内容,辅以代码示例,总结如何写好日志,希望对他人有所启发和帮助:

  • 怎样记日志可以方便 Troubleshooting
  • 程序运行状态可以记哪些
  • 应该避免怎样的日志方式

怎样记日志可以方便 Troubleshooting?

1. 对外部的调用封装

程序中对外部系统与模块的依赖调用前后都记下日志,方便接口调试。出问题时也可以很快理清是哪块的问题

复制代码
1. LOG.debug("Calling external system:" + parameters);
2. Object result = null;
3. try {
4. result = callRemoteSystem(params);
5. LOG.debug("Called successfully. result is " + result);
6. } catch (Exception e) {
7. LOG.warn("Failed at calling xxx system . exception : " + e);
8. }

2. 状态变化

程序中重要的状态信息的变化应该记录下来,方便查问题时还原现场,推断程序运行过程

复制代码
1. boolean isRunning;
2.
3. isRunning = true;
4. LOG.info("System is running");
5.
6. //...
7.
8. isRunning = false;
9. LOG.info("System was interrupted by " + Thread.currentThread().getName());

3. 系统入口与出口:

这个粒度可以是重要方法级或模块级。记录它的输入与输出,方便定位

复制代码
1. void execute(Object input) {
2. LOG.debug("Invoke parames : " + input);
3. Object result = null;
4.
5. //business logic
6.
7. LOG.debug("Method result : " + result);
8. }

4. 业务异常:

任何业务异常都应该记下来:

复制代码
1. try {
2. //business logical
3. } catch (IOException e) {
4. LOG.warn("Description xxx" , e);
5. } catch (BusinessException e) {
6. LOG.warn("Let me know anything");
7. } catch (Exception e) {
8. LOG.error("Description xxx", e);
9. }
10.

5. 非预期执行:

为程序在“有可能”执行到的地方打印日志。如果我想删除一个文件,结果返回成功。但事实上,那个文件在你想删除之前就不存在了。最终结果是一致的,但程序得让我们知道这种情况,要查清为什么文件在删除之前就已经不存在

复制代码
1. int myValue = xxxx;
2. int absResult = Math.abs(myValue);
3. if (absResult < 0) {
4. LOG.info("Original int " + myValue + "has nagetive abs " + absResult);
5. }

6. 很少出现的 else 情况:

else 可能吞掉你的请求,或是赋予难以理解的最终结果

复制代码
1. Object result = null;
2. if (running) {
3. result = xxx;
4. } else {
5. result = yyy;
6. LOG.debug("System does not running, we change the final result");
7. }

程序运行状态可以记哪些?

程序在运行时就像一个机器人,我们可以从它的日志看出它正在做什么,是不是按预期的设计在做,所以这些正常的运行状态是要有的。

1. 程序运行时间:

复制代码
1. long startTime = System.currentTime();
2.
3. // business logical
4.
5. LOG.info("execution cost : " + (System.currentTime() - startTime) + "ms"); 

2. 大批量数据的执行进度:

复制代码
1. LOG.debug("current progress: " + (currentPos * 100 / totalAmount) + "%");

3. 关键变量及正在做哪些重要的事情:

执行关键的逻辑,做 IO 操作等等

复制代码
1. String getJVMPid() {
2. String pid = "";
3. // Obtains JVM process ID
4. LOG.info("JVM pid is " + pid);
5. return pid;
6. }
7.
8. void invokeRemoteMethod(Object params) {
9. LOG.info("Calling remote method : " + params);
10. //Calling remote server
11. }

应该避免怎样的日志方式?

1. 混淆信息的 Log

日志应该是清晰准确的: 当看到日志的时候,你知道是因为连接池取不到连接导致的问题么?

复制代码
1. Connection connection = ConnectionFactory.getConnection();
2. if (connection == null) {
3. LOG.warn("System initialized unsuccessfully");
4. }

2. 记错位置

产品代码中,使用 console 记录日志,导致没有找到日志。

复制代码
1. } catch (ConfigurationException e) {
2. e.printStackTrace();
3. }

3. 记错级别

记错级别常常发生,常见的如:混淆代码错误和用户错误, 如登录系统中,如果恶意登录,那系统内部会出现太多 WARN,从而让管理员误以为是代码错误。可以反馈用户以错误,但是不要记录用户错误的行为,除非想达到控制的目的。

复制代码
1. LOG.warn("Failed to login by "+username+");

4. 遗漏信息

这里可能包含两种情况:(1)用户自己少写了信息, 导致毫无参考价值;(2)用户调用 log 的方式导致丢失信息,如下例,没有 stack trace.

复制代码
1. } catch (Exception ex) {
2. log.error(ex);
3. }

总结:

日志记录在程序员日常编程实践中必须面对的事情,本文针对这个话题谈了下自己的体会,希望读者能有所收益。多有不足,请多包涵。

参考文献

http://www.infoq.com/cn/articles/10-java-questions-easy-to-ignore

作者介绍

冶秀刚, 平台工程师,从事分布式收集与存储系统的研究与开发,爱好读书,喜欢涉猎各类能长见识的内容


给 InfoQ 中文站投稿或者参与内容翻译工作,请邮件至 editors@cn.infoq.com 。也欢迎大家通过新浪微博( @InfoQ )或者腾讯微博( @InfoQ )关注我们,并与我们的编辑和其他读者朋友交流。

2012-11-06 00:1919345

评论

发布
暂无评论
发现更多内容

10天用Flutter撸了个高仿携程App,薪资翻倍

android 面试 移动开发

关于一场甲乙双方争议的思考

boshi

创业 项目管理

2021Android开发现状分析,爆火的Android面试题

android 面试

2021Android岗面试题知识点小结,Android程序员必看

android 面试 移动开发

Linux IPTables:如何添加防火墙规则

华为云开发者联盟

Linux 防火墙 iptables 数据包 防火墙规则

2021Android常见笔试题,字节面试官

android 面试 移动开发

kafka集群迁移实践

小江

kafka 迁移 消息队列

2021Android最新大厂面试真题总结,给大家安排上

android 面试 移动开发

10天拿到阿里Android岗offer,深入剖析

android 面试 移动开发

1307页字节跳动Android面试全套真题解析火了,阿里P8大牛亲自教你

android 面试 移动开发

2020-2021华为Android面试真题,大厂内部资料

android 面试 移动开发

2021Android开发面试解答之设计模式,食堂大妈看完都学会了

android 面试 移动开发

深入剖析RocketMQ源码-NameServer

vivo互联网技术

RocketMQ 服务器 java; NameServer

10天拿到字节跳动安卓岗位offer,阿里内部Android应届生就业宝典

android 面试 移动开发

异地多活知识总结

十二万伏特皮卡丘

2020-2021阿里巴巴安卓面试真题解析,2021最新版

android 面试 移动开发

2021Android常见面试题分享,年薪50W

android 面试 移动开发

2021Android者真的太难了,大专生三面蚂蚁金服

android 面试 移动开发

带你掌握Vue过滤器filters及时间戳转换

华为云开发者联盟

JavaScript Vue 过滤器 时间戳 filters

12年高级工程师的“飞升之路”,进阶学习资料

android 面试 移动开发

2019-2021历年字节跳动Android面试真题解析,字节跳动高工面试

android 面试 移动开发

2021Android大厂面试真题,2021Android面试心得

android 面试 移动开发

小学生都能读懂的网络协议之:WebSocket

程序那些事

网络协议 HTTP 程序那些事 webscoket

2021Android最新大厂面试真题总结,震撼发布

android 面试 移动开发

10天拿到阿里Android岗offer,面试必知必会

android 面试 移动开发

12个View绘制流程高频面试题,膜拜大牛

android 面试 移动开发

2019-2021历年阿里Android面试真题,面试杀手锏

android 面试 移动开发

Groovy参数默认值在接口测试中应用

FunTester

接口测试 Groovy FunTester 参数默认值 重载

2021Android最新大厂面试真题总结,app架构师

android 面试 移动开发

2021Android进阶者的新篇章,已开源

android 面试 移动开发

2021Android进阶者的新篇章,移动开发框架

android 面试 移动开发

程序那些事:日志记录的作用和方法_Java_冶秀刚_InfoQ精选文章