现在对于我的 ConversionPattern,我有:
log4j.appender.A1.layout.ConversionPattern=%d{yyyy MMM dd HH:mm:ss,SSS} %5p [%t] (%F:%L) - %m%n
我还想在类 (%F:%L) 中包含完整的包名称,但我找不到 any config to do so in the docs .我知道这会更慢,但这只是为了调试,而不是在系统投入生产时。
最佳答案
也许我只是误解了你,但是 %C 会输出你的类包。
来自您引用的文档:
%C
Used to output the fully qualified class name of the caller issuing the logging request. This conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets.
If a precision specifier is given, then only the corresponding number of right most components of the class name will be printed. By default the class name is output in fully qualified form.
For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass".
WARNING Generating the caller class information is slow. Thus, use should be avoided unless execution speed is not an issue.
更新: 在许多情况下,您也可以使用 %c,如果您的类别是您的类名,它也会打印出包含包的完整类。例如当你在初始化你的日志时做这样的事情:
private static final Log LOG = LogFactory.getLog(MyClazz.class);
使用 %c 并不慢。
关于java - Log4j 显示包名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2594818/