java log4j:缩写/缩短包名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7891910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log4j: abbreviate/shorten package names
提问by n002213f
How would i abbreviate/shorten package names in log generated using log4j. i.e. instead of com.longpackage.anotherpackage.lastpackage.MyClass
i want c.l.a.l.MyClass
. I have seen this in Artifactorylogs, but can't figure how to achieve this using log4j.
我将如何缩写/缩短使用 log4j 生成的日志中的包名称。即而不是com.longpackage.anotherpackage.lastpackage.MyClass
我想要c.l.a.l.MyClass
。我在Artifactory日志中看到了这一点,但不知道如何使用 log4j 来实现这一点。
回答by n002213f
It turns out that this is possible using log4j after all(tested using v1.2.16), thanks to Tomasz Nurkiewiczfor pointing me to LogBack. The feature, ConversionWord, is available using the EnhancedPatternLayoutlayout.
事实证明,这毕竟可以使用 log4j(使用v1.2.16测试),感谢Tomasz Nurkiewicz将我指向LogBack。使用EnhancedPatternLayout布局可以使用 ConversionWord 功能。
From the javadoc
来自 javadoc
... For example, for the category name "alpha.beta.gamma" the pattern %c{2} will output the last two elements ("beta.gamma"), %c{-2} will remove two elements leaving "gamma", %c{1.} will output "a.b.gamma" ...
... For example, for the category name "alpha.beta.gamma" the pattern %c{2} will output the last two elements ("beta.gamma"), %c{-2} will remove two elements leaving "gamma", %c{1.} will output "a.b.gamma" ...
I'm using JBoss and had to upgrade log4j and use the above layout.
我正在使用 JBoss,不得不升级 log4j 并使用上述布局。
回答by Tomasz Nurkiewicz
Artifactory uses Logback:
Artifactory 使用Logback:
Projects known to rely on logback:
[...]
Artifactory
已知依赖 logback 的项目:
[...]
神器
which has this nice feature called Conversion Word:
它有一个很好的功能叫做Conversion Word:
|Conversion specifier | Logger name | Result |
|---------------------|:--------------------------:| -------------------------:|
|%logger | mainPackage.sub.sample.Bar | mainPackage.sub.sample.Bar|
|%logger{0} | mainPackage.sub.sample.Bar | Bar |
|%logger{5} | mainPackage.sub.sample.Bar | m.s.s.Bar |
|%logger{10} | mainPackage.sub.sample.Bar | m.s.s.Bar |
|%logger{15} | mainPackage.sub.sample.Bar | m.s.sample.Bar |
|%logger{16} | mainPackage.sub.sample.Bar | m.sub.sample.Bar |
|%logger{26} | mainPackage.sub.sample.Bar | mainPackage.sub.sample.Bar|
* I miss tables in SO markdown sooo much...
* 我非常想念 SO markdown 中的表格......