java - Log4j 显示包名

标签 java jakarta-ee log4j

现在对于我的 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/

相关文章:

java 8流从类型A的集合创建类型B的集合

java - Netbeans 远程项目 (EJB)

java - 持久化之前如何获取实体的ID?

java - 我们可以在运行时更改 log4j 的日志记录级别吗

java:如何设置全局线程ID?

go - Prometheus 日志指标导出器

java - jTidy 和 TagSoup 文档

java - 如何在java中声明通用键值对数组

java - Android:findViewById 返回 null?

jakarta-ee - JAAS LoginModule 在 Websphere Liberty 中不起作用