相当于log4j%l的Logback模式 [英] Logback pattern equivalent of log4j %l

查看:112
本文介绍了相当于log4j%l的Logback模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

配置log4j我可以使用 log4j 1.2模式 %l给我呼叫者的位置,输出:

... com.example.FooBar.doSomething(FooBar.java:123) ...

我正尝试切换到Logger,因为它是log4j的打算作为继承者" 在log4j 1.2上各种改进

但是Logback没有%l模式.我可以找到的最接近的 Logback模式%caller{1},但这给了我一些丑陋的印象:

... Caller+0     at com.example.FooBar.doSomething(FooBar.java:123)
...

请注意,这增加了对丑陋的侮辱,它在我的日志行中间添加了一个换行符. (我当时没有在模式中指定换行符.)

如何在Logback模式中获得与log4j %l相同的结果?

解决方案

正如durron597指出的那样,似乎无法完全替代log4j %l的单个转换变量.但是,通过组合多个转换变量,可以达到相同的效果.

将log4j配置中的%l的每个实例具体替换为Logback的以下内容:%class.%method\(%file:%line\)

(如果您通过编程方式执行此操作,请确保根据Java字符串的要求将反斜杠加倍.)

其中一些变量表示降低了应用程序的性能.不过,我已经看过源代码,并且至少缓存了相关信息,因此在模式中使用多个慢速转换变量不会比仅使用一个慢速转换性能差.

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.

这篇关于相当于log4j%l的Logback模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆