简体   繁体   English

Logback 模式等效于 log4j %l

[英]Logback pattern equivalent of log4j %l

Configuring log4j I can use the log4j 1.2 pattern %l to give me location of the caller, which outputs:配置 log4j 我可以使用log4j 1.2 模式%l来给我调用者的位置,它输出:

... 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我正在尝试切换到 Logger,因为它“打算作为log4j的后继者” ,并且对 log4j 1.2 进行了各种改进

But Logback has no %l pattern.但是 Logback 没有%l模式。 The closest Logback pattern I can find is %caller{1} , but that gives me something ugly:我能找到的最接近的Logback 模式%caller{1} ,但这给了我一些丑陋的东西:

... 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?如何在我的 Logback 模式中获得相当于 log4j %l的值?

As durron597 indicated, a single conversion variable that is the exact replacement for log4j %l does not seem to be available. 正如durron597指出的那样,似乎无法完全替代log4j %l的单个转换变量。 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\\) 用以下用于Logback的实例专门替换log4j配置中的%l每个实例: %class.%method\\(%file:%line\\)

(If you are doing this programmatically, make sure to double the backslashes as required for Java strings.) (如果您以编程方式执行此操作,请确保根据Java字符串的要求将反斜杠加倍。)

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 can extend the CallerDataConverter and override the convert to strip of the line separator. 您可以扩展CallerDataConverter并覆盖convert为行分隔符的条。 PatternLayout exposes a static map of converters. PatternLayout公开转换器的静态映射。 You can update this map with your converter as part of bootstrap, better to do in some class initialization static block. 您可以使用转换器作为引导程序的一部分来更新此映射,最好在某些类初始化静态块中进行。 Not beautiful but should work. 不漂亮,但应该可以。

You are correct that %l no longer exists in logback. 您是正确的, %l在登录中不再存在。 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 . 不幸的是,即使您正确地认为它是%l的最佳替代品,您也无法对%caller做得更好。 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 . 希望我对您有更好的消息,但我认为如果没有自定义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: 以下正则表达式删除了不需要的Caller和多余的新行:

%replace(%caller{1}){'Caller\+0\s*at\s*([^\n]*)\n', '$1'}

Just modified few things in above verified answer,刚刚修改了上面经过验证的答案中的一些内容,

%logger{50}.%M\\\\(%file:%line\\\\)

(As per the doc) For xml configuration, Even though If I try adding One backslash \\ infront of parenthesis, not working, Try add double backslash, (根据文档)对于 xml 配置,即使我尝试在括号前添加一个反斜杠\\也不起作用,请尝试添加双反斜杠,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM