Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure Logback to print out the class name

Tags:

I'm using Play 2.1. I'm using the default logger play.api.Logger. I'm confused about how it works.

In my scala code, a line in class "com.myapp.tickets" in the method "getPayment()" like this

Logger.info("getTickets") 

generates a log message like this.

14:58:58.005 INFO  application play.api.LoggerLike$class info  getTickets 

My application-logger.xml pattern is

%d{HH:mm:ss.SSS} %-5level %logger %class %method  %msg%n 

The issue I have is that %logger tells me "application", %class tells me "play.api.LoggerLike$class and %method tells me "info". I know all of that. I of course want to avoid adding more cruft into the message itself (like the class name or method).

If I print out the call stack (%caller) then level 2 has what I want, but that does not seem a viable way to generate a log.

How do I configure it to output the application specific class and method, not the class and method of the logger itself?

like image 653
user2141729 Avatar asked Mar 06 '13 21:03

user2141729


People also ask

How do I set Logback xml in classpath?

You can simply put a my-logback. xml in the root of one of your classpath entries and specify -Dlogback. configurationFile=my-logback. xml .

What is logger name in Logback xml?

Logback Architecture 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 setup a Logback XML file?

In a Logback. xml file, all the configuration options are enclosed within the <configuration> root element. In the root element, you can set the debug=true attribute to inspect Logback's internal status. You can also configure auto scanning of the configuration file by setting the scan=true attribute.

Does Logback use SLF4J?

Logback natively implements the SLF4J API.


1 Answers

%class{0} will only output the class name, so instead of:

com.something.MyClass 

You'll get:

MyClass 

This is how my pattern for logback normally looks:

%d{HH:mm:ss} [%thread] %-5p %class{0} - %m%n 

You can also add the method and the line if you are interested by doing:

%d{HH:mm:ss} [%thread] %-5p %class{0}.%method:%L - %m%n 
like image 89
jspboix Avatar answered Oct 13 '22 10:10

jspboix