一、方法约束
Contoller 层方法命名规约
1 ) 获取单个对象的方法用 get 做前缀。
2 ) 获取多个对象的方法用 list 做前缀。
3 ) 获取统计值的方法用 count 做前缀。
4 ) 插入的方法用 save/insert 做前缀。
5 ) 删除的方法用 remove/delete 做前缀。
6 ) 修改的方法用 update 做前缀。
便于日志拦截方法统一处理过滤
二、日志约束
-
【强制】对 trace / debug / info 级别的日志输出,必须使用条件输出形式或者使用占位符的方式。
说明:
logger . debug( " Processing trade with id : " + id + " and symbol : " + symbol);
如果日志级别是 warn ,上述日志不会打印,但是会执行字符串拼接操作,如果 symbol 是对象,会执行 toString() 方法,浪费了系统资源,执行了上述操作,最终日志却没有打印。
正例: ( 条件 )
-
if (logger.isDebugEnabled()) {
logger.debug("Processing trade with id: " + id + " and symbol: " + symbol);
}
正例: ( 占位符 )
logger.debug("Processing trade with id: {} and symbol : {} ", id, symbol); -
【强制】日志文件推荐至少保存 15 天,因为有些异常具备以“周”为频次发生的特点。
-
【强制】应用中的扩展日志 ( 如打点、临时监控、访问日志等 )
命名方式:appName _ logType _ logName . log 。 logType :日志类型,推荐分类有stats / desc / monitor / visit 等 ;logName :日志描述。这种命名的好处:通过文件名就可知道日志文件属于什么应用,什么类型,什么目的,也有利于归类查找。
正例:mppserver 应用中单独监控时区转换异常,如:mppserver _ monitor _ timeZoneConvert . log
说明:推荐对日志进行分类,如将错误日志和业务日志分开存放,便于开发人员查看,也便于通过日志对系统进行及时监控。 -
【强制】避免重复打印日志,浪费磁盘空间,务必在 log 4 j . xml 中设置 additivity = false 。
正例:
5.【强制】异常信息应该包括两类信息:案发现场信息和异常堆栈信息。如果不处理,那么通过关键字 throws 往上抛出。
正例: logger.error(各类参数或者对象 toString + “_” + e.getMessage(), e);
7.【参考】可以使用 warn 日志级别来记录用户输入参数错误的情况,避免用户投诉时,无所适从。注意日志输出的级别, error 级别只记录系统逻辑出错、异常等重要的错误信息。如非必要,请不要在此场景打出 error 级别
8.【强制】 Java 类库中定义的一类 RuntimeException 可以通过预先检查进行规避,而不应该通过 catch 来处理,比如: IndexOutOfBoundsException , NullPointerException 等等。
说明:无法通过预检查的异常除外,如在解析一个外部传来的字符串形式数字时,通过 catch
NumberFormatException 来实现。
正例: if (obj != null) {…}
反例: try { obj.method() } catch (NullPointerException e) {…}
三、 日志打印技巧(重点执行,便于排查问题)
1.对接外部的调用封装(debug)
程序中对接外部系统与模块的依赖调用前后都记下日志,方便接口调试。出问题时也可以很快理清是哪块的问题
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.状态变化(info)
程序中重要的状态信息的变化应该记录下来,方便查问题时还原现场,推断程序运行过程
boolean isRunning = true;
LOG.info("System is running"); //...
isRunning = false;
LOG.info("System was interrupted by {}",Thread.currentThread().getName());
3.系统入口与出口:(debug)
这个粒度可以是重要方法级或模块级。记录它的输入与输出,方便定位
这个粒度可以是重要方法级或模块级。记录它的输入与输出,方便定位
void execute(Object input) {
LOG.debug("Invoke parames :{} ",input);
Object result = null;
//business logical
LOG.debug("Method result : {}",result);
}
4.业务异常:(warn,error)
任何业务异常都应该记下来
try {
//business logical
} catch (IOException e) {
LOG.warn("Description xxx{}" , e);
} catch (BusinessException e) {
LOG.warn("Let me know anything{}",e);
} catch (Exception e) {
LOG.error("Description xx{}", e);
}
void invoke(Object primaryParam) {
if (primaryParam == null) {
LOG.warn(原因...);
return;
}
}
5.非预期执行(info)
为程序在“有可能”执行到的地方打印日志。如果我想删除一个文件,结果返回成功。但事实上,那个文件在你想删除之前就不存在了。最终结果是一致的,但程序得让我们知道这种情况,要查清为什么文件在删除之前就已经不存在呢
int myValue = xxxx;
int absResult = Math.abs(myValue);
if (absResult < 0) {
LOG.info("Original int {} has nagetive abs {}" myValue,absResult);
}
6.很少出现的else情况:(debug)
else可能吞掉你的请求,或是赋予难以理解的最终结果
Object result = null; if (running) {
result = xxx;
} else {
result = yyy;
LOG.debug("System does not running, we change the final result");
}
7.程序运行状态的日志(info)
程序在运行时就像一个机器人,我们可以从它的日志看出它正在做什么,是不是按预期的设计在做,所以这些正常的运行状态是要有的。
程序运行时间:
long startTime = System.currentTime();
// business logical
LOG.info("execution cost : {} ms",(System.currentTime() - startTime) );
8.大批量数据的执行进度:(debug)
LOG.debug("current progress: {}%", (currentPos * 100 / totalAmount));
9.关键变量及正在做哪些重要的事情(info)
执行关键的逻辑,做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
}
四、需要规避的问题
频繁打印大数据量日志:当日志产生的速度大于日志文件写磁盘的速度,会导致日志内容积压在内存中,导致内存泄漏。
无意义的Log:日志不包含有意义的信息: 你肯定想知道的是哪个文件不存在吧
File file = new File("xxx"); if (!file.isExist()) {
LOG.warn("File does not exist"); //Useless message
}
混淆信息的Log:
日志应该是清晰准确的: 当看到日志的时候,你知道是因为连接池取不到连接导致的问题么?
Connection connection = ConnectionFactory.getConnection(); if (connection == null) {
LOG.warn("System initialized unsuccessfully");
}
不完善处,大家评论留言,后续整合更新********