我最近在现有应用程序中偶然发现了以下几行日志记录代码。这段代码在很多方面对我来说都是完全错误的
Method method = ...;// passed in as parameter
//...
String className = method.getDeclaringClass().getName();
String methodName = method.getName();
long id = Thread.currentThread().getId();
String name = Thread.currentThread().getName();
String msg = String.format("some text, for: %s %s in thread %d %s", className, methodName, id, name);
LOGGER.debug("xxxxxxxx {}", msg);
我想知道
- 这段代码有什么问题?
- 什么对性能影响最大?
- 编写这样的日志记录语句的最佳方式是什么?
(注意:如 {}
所示,它是一个 SLF4J 记录器,xxxxxxxx
是一个唯一的字符串,用于在代码库中标识此特定的日志记录语句)
最佳答案
日志记录是应用程序中的交叉关注点。
所以对于一般情况,我使用拦截器/方面模式来做到这一点。
String className = method.getDeclaringClass().getName();
String methodName = method.getName();
long id = Thread.currentThread().getId();
String name = Thread.currentThread().getName();
String msg = String.format("some text, for: %s %s in thread %d %s",
className, methodName, id, name);
在当前记录器的有效级别之前不检查就进行所有这些计算是没有意义的,因为即使未写入日志也会应用它。
此外,如果需要并在附加程序的模式中指定,所有这些信息都可以由 API 记录器检索。
此外这也没有效果:
String msg = String.format("some text, for: %s %s in thread %d %s", className, methodName, id, name);
LOGGER.debug("xxxxxxxx {}", msg);
它还在检查级别之前格式化字符串。
使用实际代码,这样更好:
if (LOGGER.isDebugEnabled()){
String className = method.getDeclaringClass().getName();
String methodName = method.getName();
long id = Thread.currentThread().getId();
String name = Thread.currentThread().getName();
String msg = String.format("some text, for: %s %s in thread %d %s", className, methodName, id, name);
LOGGER.debug("xxxxxxxx {}", msg);
}
但是一个更健壮的解决方案是使用 appender 模式来用线程 id、方法等装饰日志信息......
最后,如果当前级别的检查(此处:if (LOGGER.isDebugEnabled()){
)不包含日志记录处理,作为一般方法,例如:
String msg = String.format("some text, for: %s %s in thread %d %s", className, methodName, id, name);
LOGGER.debug("xxxxxxxx {}", msg);
使用 log 方法更有效,因为它是为设计而设计的,而不是在你的示例中使用的,因为它会破坏它的目的。
通过这种方式,如果有效记录器级别不匹配,它可以节省计算:
LOGGER.debug("some text, for: {} {} in thread {} {}", className, methodName, id, name);
关于java - 这个日志语句有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43626491/