日志记录的作用和方法

来源:互联网 发布:stussy淘宝正品 编辑:程序博客网 时间:2024/05/01 08:35

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

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

怎样记日志可以方便Troubleshooting?

1. 对外部的调用封装

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

  LOG.debug("Calling external system:" + parameters);    Object result = null;    try {        result = callRemoteSystem(params);        LOG.debug("Called successfully. result is " + result);    } catch (Exception e) {        LOG.warn("Failed at calling xxx system . exception : " + e);    }  

2.状态变化

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

  boolean isRunning;        isRunning = true;    LOG.info("System is running");        //...        isRunning = false;    LOG.info("System was interrupted by " + Thread.currentThread().getName());  

3.系统入口与出口:

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

  void execute(Object input) {        LOG.debug("Invoke parames : " + input);        Object result = null;                //business logic              LOG.debug("Method result : " + result);    }  

4.业务异常:

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

  try {        //business logical    } catch (IOException e) {        LOG.warn("Description xxx" , e);    } catch (BusinessException e) {        LOG.warn("Let me know anything");    } catch (Exception e) {        LOG.error("Description xxx", e);    } 

5.非预期执行:

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

  int myValue = xxxx;    int absResult = Math.abs(myValue);    if (absResult < 0) {        LOG.info("Original int " + myValue + "has nagetive abs " + absResult);    } 

6.很少出现的else情况:

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

  Object result = null;    if (running) {       result = xxx;    } else {       result = yyy;       LOG.debug("System does not running, we change the final result");    } 


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

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

1. 程序运行时间:

  long startTime = System.currentTime();        // business logical        LOG.info("execution cost : " + (System.currentTime() - startTime) + "ms");  
2. 大批量数据的执行进度:

LOG.debug("current progress: " + (currentPos * 100 / totalAmount) + "%");

3.关键变量及关键操作:

关键操作,如IO操作、安全相关操作等。

安全日志主要用于记录与安全相关的一些操作,比如用户的登陆/退出、修改密码、上线/掉线等。通过记录这些信息我们可以跟踪用户使用系统的状况,并且可以做一些有意义的统计。

  String getJVMPid() {       String pid = "";       // Obtains JVM process ID       LOG.info("JVM pid is " + pid);       return pid;    }        void invokeRemoteMethod(Object params) {        LOG.info("Calling remote method : " + params);       //Calling remote server   } 

4.重要的业务操作:

  记录重要的业务操作日志也是非常重要的,比如类似某操作员修改了某个用户的权限这样关键性的操作,通过业务操作日志,当发现操作失误时,我们可以地查找到是哪个操作员在什么时间犯错而导致误操作的,这就为我们的系统安全又提供了一层监控机制。


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

1. 混淆信息的Log

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

  Connection connection = ConnectionFactory.getConnection();    if (connection == null) {        LOG.warn("System initialized unsuccessfully");    }  

2. 记错位置

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

 } catch (ConfigurationException e) {    e.printStackTrace(); }

3. 记错级别

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

LOG.warn("Failed to login by "+username+");

4. 遗漏信息

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

} catch (Exception ex) {   log.error(ex);}

参考文献

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


转自:

http://www.infoq.com/cn/articles/why-and-how-log

http://www.cnblogs.com/zhuweisky/archive/2011/02/24/1960959.html



0 0
原创粉丝点击