简体   繁体   English

logback 显示带有行号的日志

[英]logback show logs with line number

I want to write log like:我想写日志,如:

2014-04-17 11:00:16.408 [http-apr-9090-exec-4] DEBUG package.method(line) - log.

so I config the logback.xml, in the pattern, the config like:所以我在模式中配置 logback.xml,配置如下:

%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M(%line) - %msg%n

Every thing shows ok except the line number, and if i add set like除了行号,一切都显示正常,如果我添加像

%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M %line - %msg%n

Then all works.然后一切正常。 so there must be something wrong with my configuration.所以我的配置肯定有问题。
Could anyone help me ?有人可以帮我吗? Thanks.谢谢。 I want to display like what I want, and no space between method name and line number.我想像我想要的那样显示,并且方法名称和行号之间没有空格。

The Logback manual states Logback 手册指出

In PatternLayout, parenthesis can be used to group conversion patterns.在 PatternLayout 中,括号可用于对转换模式进行分组。 It follows that the '(' and ')' carry special meaning and need to be escaped if intended to be used as literals.因此,'(' 和 ')' 具有特殊含义,如果打算用作文字,则需要对其进行转义。 The special nature of parenthesis is further explained below.下面进一步解释括号的特殊性质。

[...] [...]

If you need to treat the parenthesis character as a literal, it needs to be escaped by preceding each parenthesis with a backslash.如果您需要将括号字符视为文字,则需要通过在每个括号前加上反斜杠来对其进行转义。 As in, \\(%d{HH:mm:ss.SSS} [%thread]\\) .如, \\(%d{HH:mm:ss.SSS} [%thread]\\)

You'll need to escape the parenthesis with a \\ .您需要用\\转义括号。

%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n

任何偶然发现此页面以寻找如何在 application.properties 文件中配置它的人的说明,我通过添加两个反斜杠成功地转义了括号。

%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M \\(%line\\) - %msg%n

For console logging I use:对于控制台日志记录,我使用:

<Pattern>%black(%date{"yyyy-MM-dd HH:mm:ss.SSS", "Europe/London"}) %highlight(%-5level) [%green(%X{sessionId})] %yellow(%logger{36}@%method\(%line\)) - %msg%n%throwable</Pattern>

And for logging to file:并记录到文件:

 <pattern>[%date{"yyyy-MM-dd HH:mm:ss,SSSXXX", "Europe/London"}] [${HOSTNAME}] [%thread] %level %logger{36}@%method:%line - %msg%n</pattern>

If you want to have log messages where you can click on a link into the source code, eg in IntelliJ, use the following pattern for console logging inside your IDE:如果您想要在其中单击源代码链接的日志消息,例如在 IntelliJ 中,请使用以下模式在您的 IDE 中进行控制台日志记录:

<pattern>
    %d{dd-MM-yyyy HH:mm:ss.SSS} %highlight(%-5level) %magenta([%thread]) %yellow(%logger{40}.%M\(%class{0}.java:%line\)) - %msg%throwable%n
</pattern>

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

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