配置 log4j 我可以使用 log4j 1.2 pattern %l
给我调用者的位置,输出:
... com.example.FooBar.doSomething(FooBar.java:123) ...
我正在尝试切换到 Logger,因为它是 "intended as a successor"到 log4j 并有 all sorts of improvements超过 log4j 1.2
但是 Logback 没有 %l
模式。最近的Logback pattern我可以找到 %caller{1}
,但这给了我一些难看的东西:
... Caller+0 at com.example.FooBar.doSomething(FooBar.java:123)
...
请注意,它在我的日志行中间添加了一个换行符,这是对丑陋的侮辱。 (当时我没有在模式中指定换行符。)
如何在 Logback 模式中获得相当于 log4j %l
的内容?
最佳答案
您是正确的,%l
不再存在于 logback 中。这可能是因为,从您自己的文档链接:
The location information can be very useful. However, its generation is extremely slow and should be avoided unless execution speed is not an issue.
不幸的是,你不能用 %caller
做得更好,即使你认为它是 %l
的最佳替代品是正确的。如您所见in the source here ,该换行符非常彻底地嵌入到错误消息生成步骤中。
我希望能为您带来更好的消息,但我认为如果没有custom Layout
,您就无法做得更好。 。您可以从以下事实中得到一些小小的安慰: this has been asked before .
关于java - Logback 模式相当于 log4j %l,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32158409/