Log4j允许分别指定日志字符串和对象。例如:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyLogger {
private static final Logger logger = LoggerFactory.getLogger(MyLogger.class);
public static void main(String [] args) {
logger.info("This is a number {} and this is a string {}", 5, "blah!");
}
}
导致此输出
This is a number 5 and this is a string blah!
我想知道每次打印日志行时是否可以告诉log4j打印引号括起的所有参数。所以上面的代码相同:
logger.info("This is a number {} and this is a string {}", 5, "blah!");
会输出:
This is a number "5" and this is a string "blah!"
我想要做的是改变log4j行为,而不是调用logger方法(logger.info(...))。我的问题是我在数千个地方呼叫记录器并且改变所有呼叫是不切实际的。
答案 0 :(得分:0)
您只需要在参数括号之前和之后的原始字符串中添加带转义字符的引号,即\
,如果它是字符串,则在参数本身中。以下是例子;
logger.info("args one \"{}\" and two \"{}\"", "blah!!", 4);
将输出args one "blah!!" and two "4"
。 logger.info("args one {} and two {}", "\"blah!!\"", 4);
将输出args one "blah!!" and two 4