考虑以下代码:最佳实践在多线程环境中调用
if (param1 == null || param2 == null) {
logger.error("Failed to do stuff.");
logger.debug("param1: " + param1);
logger.debug("param2: " + param2);
}
这是非常具有可读性,但在多线程环境中的原子逻辑日志消息可以分为三个部分。
现在,解决方案的一部分是简单,可读性不会受太大的影响:
if (param1 == null || param2 == null) {
logger.error("Failed to do stuff.");
logger.debug(
"param1: " + param1 + System.getProperty("line.separator")
+ "param2: " + param2
);
}
如果稍微改变记录仪输出是OK,你可以写:
if (param1 == null || param2 == null) {
String message = "Failed to do stuff.";
if (logger.isDebugEnabled()) {
message += System.getProperty("line.separator")
+ "param1: " + param1 + System.getProperty("line.separator")
+ "param2: " + param2;
}
logger.error(message);
}
清理日志,但丑陋的代码...
或者,你可以写:
if (param1 == null || param2 == null) {
synchronized (logger) {
logger.error("Failed to do stuff.");
logger.debug("param1: " + param1);
logger.debug("param2: " + param2);
}
}
你有什么建议?为什么?
您正在使用哪个日志记录库? – fglez 2013-05-03 12:45:37