Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

%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.

like image 754
Awakening Avatar asked Apr 17 '14 03:04

Awakening


People also ask

What is Appender in Logback?

The Logback architecture is comprised of three classes: Logger, Appender, and Layout. A Logger is a context for log messages. This is the class that applications interact with to create log messages. Appenders place log messages in their final destinations. A Logger can have more than one Appender.

How do I enable debug in Logback?

You can now use -Dlogback. debug=true to enable debugging of the logback setup. Unfortunately, there is no way to enable debugging via a System property. You have to use <configuration debug="true"> in the logback.


2 Answers

The Logback manual states

In PatternLayout, parenthesis can be used to group conversion patterns. 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]\).

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 
like image 195
Sotirios Delimanolis Avatar answered Sep 20 '22 04:09

Sotirios Delimanolis


A note for anyone who stumbles onto this page looking for how to configure this in the application.properties file, I had success escaping the parenthesis by adding two backslashes.

%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M \\(%line\\) - %msg%n 
like image 31
Derek Avatar answered Sep 19 '22 04:09

Derek