简体   繁体   中英

Print archive (jar) the log is coming from in logback

I have some descrepency between class implementations on a Tomcat server and a Websphere server. Is there a way in logback to display the jar and version next in every log? So the log would look like this:

DEBUG | 2015-01-22 16:13:49.252 [Default Executor-thread-7] ApplicationName org.apache.jcp.xml.dsig.internal.dom.DOMSignedInfo - Canonicalized SignedInfo: [wss4j.jar 1.6.17]

My current pattern is this:

<pattern>%-5level | %date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %contextName %logger - %message%n</pattern>

My POM:

<dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>0.9.30</version>
            <type>jar</type>
            <scope>runtime</scope>
        </dependency>

I would suggest examining the full classpath of the Java process, you can simply use

jinfo [pid]

You'll get plenty of output, including java.class.path with all the jars and what order they are in

It's been a fairly common problem with large app servers (WebSphere, etc.) that have some other version of a library that shows up on the classpath first that you may not be expecting

This is easy to do with stacktraces.

To display the jar file and version for each line of stack trace with Logback :

You just have to add packagingData="true" as attribute of configuration tag.

Example : <configuration packagingData="true"> ... </configuration>

Then you'll have something like :

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

See documentation there : Enabling packaging data in stack traces .
Note : As of version 1.1.4, packaging data is disabled by default.

Article from James Strachan (2008)

Was referenced from logback documentation here .

Pattern layout

Otherway you can also customize your pattern with :

  • %class
  • %file
  • %line
  • %caller

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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