问题描述
配置log4j i可以使用 %l给我呼叫者的位置,输出:
... com.example.FooBar.doSomething(FooBar.java:123) ...
我正在尝试切换到logger,因为它是"旨在作为后继者" to log4j 各种改进对log4j 1.2 1.2
,但记录没有%l模式.最近的 logback模式我可以找到%caller{1},但这给了我一些丑陋的东西:
... Caller+0 at com.example.FooBar.doSomething(FooBar.java:123) ...
请注意,增加了丑陋的侮辱,它在我的日志线中间添加了一个newline. (当时我没有在模式中指定新线.)
如何在我的记录模式中获得相当于log4j %l的等效内容?
推荐答案
如Durron597所示,似乎是log4j %l确切替代的单个转换变量似乎没有可用.但是,通过结合几个转换变量,一个人可以达到相同的效果.
在log4j配置中特别替换了%l的每个实例,以logback:%class.%method\(%file:%line\)
(如果您是通过编程性进行此操作,请确保根据Java字符串的要求加倍斜线.)
这些变量中的几个表示降低了应用程序的性能.不过,我已经查看了源代码,至少相关信息被缓存,因此在模式中使用多个慢速转换变量不应比仅使用一个更差的性能.
.其他推荐答案
您是正确的,%l %l不再存在于记录中.这可能是因为,从您自己的文档链接中:
位置信息可能非常有用.但是,它的一代极慢,除非执行速度不是问题,否则应避免.
不幸的是,即使您是正确替代%l的正确替代品,您也不能做得更好.如您所见a>,该新线已完全嵌入到错误消息生成步骤中.
我希望我有更好的消息,但是我认为没有 custom Layout .您可以为在.
其他推荐答案
另一个选项是使用替换转换字.以下正则删除了不需要的呼叫者和额外的新线:
%replace(%caller{1}){'Caller\+0\s*at\s*([^\n]*)\n', '$1'}
问题描述
Configuring log4j I can use the log4j 1.2 pattern %l to give me location of the caller, which outputs:
... com.example.FooBar.doSomething(FooBar.java:123) ...
I'm trying to switch to Logger because it is "intended as a successor" to log4j and has all sorts of improvements over log4j 1.2
But Logback has no %l pattern. The closest Logback pattern I can find is %caller{1}, but that gives me something ugly:
... Caller+0 at com.example.FooBar.doSomething(FooBar.java:123) ...
Notice that, adding insult to ugly, it adds a newline in the middle of my log line. (I did not specify a newline in the pattern at that point.)
How do I get the equivalent of log4j %l in my Logback pattern?
推荐答案
As durron597 indicated, a single conversion variable that is the exact replacement for log4j %l does not seem to be available. But by combining several conversion variables one can achieve the same effect.
Specifically replace every instance of %l in your log4j configuration with the following for Logback: %class.%method\(%file:%line\)
(If you are doing this programmatically, make sure to double the backslashes as required for Java strings.)
Several of these variables are indicated as degrading the performance of the application. I've looked at the source code, though, and at least the relevant information is cached so that using multiple slow conversion variables in the pattern should not be any worse performance than using just one.
其他推荐答案
You are correct that %l no longer exists in logback. This is probably because, from your own documentation link:
The location information can be very useful. However, its generation is extremely slow and should be avoided unless execution speed is not an issue.
Unfortunately, you can't do much better with %caller, even though you are correct that it is the best substitute for %l. As you can see in the source here, that newline is pretty thoroughly embedded in the error message generation step.
I wish I had better news for you, but I don't think you're going to be able to do better without a custom Layout. You can take some small comfort in the fact that this has been asked before.
I have just submitted a new Feature Request here.
其他推荐答案
Another option is to use the replace conversion word. The following regex removes the unwanted Caller and extra new line:
%replace(%caller{1}){'Caller\+0\s*at\s*([^\n]*)\n', '$1'}