javalogbacklogback-classic

Logback is not working?? The JSON is not generated


I have these Maven dependency files:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>
<dependency>
    <groupId>net.logstash.logback</groupId>
    <artifactId>logstash-logback-encoder</artifactId>
    <version>6.1</version>
</dependency>

and logback.xml is placed in the resources folder.

The logback.xml is (the classnames are in two lines because of the Stackoverflow formatting):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <conversionRule conversionWord="stacktrace"
    converterClass="net.logstash.logback.stacktrace.
    ShortenedThrowableConverter" />
  <appender name="JSON" class="ch.qos.logback.core.ConsoleAppender">
    <encoder 
      class="net.logstash.logback.encoder.
      LoggingEventCompositeJsonEncoder">
      <providers>
        <pattern>
          <pattern>
            {
            "timestamp": "%date{yyyy-MM-dd HH:mm:ss.SSS}",
            "level": "%level",
            "msg": "%msg",
            "stacktrace": "%stacktrace"
            }
          </pattern>
         </pattern>
     </providers>
   </encoder>
 </appender>
 <root level="info">
   <appender-ref ref="JSON"/>
 </root>
</configuration>

The log is like this:

14:34:23,782 INFO  [x.y.Logger] (default task-15) Hello world!

The log is not outputted in the JSON format. Why?

I'm thinking is the Logback running at all. How the Logback starts when the application starts? I don't have any code lines calling it. I have read that the Logback scans the logback.xml, but how I know it is running??

The Logback works well in the new Spring boot applications, but this application is the old JEE application.

What is the problem?


Solution

  • I had to add

    jboss-deployment-structure.xml in the EAR META-INF.

    The contents of the file are:

    <?xml version="1.0" encoding="UTF-8"?>  
    <jboss-deployment-structure>  
      <deployment>  
        <exclusions>
            <module name="org.slf4j" />
            <module name="org.slf4j.impl" />
        </exclusions>
      </deployment>  
    </jboss-deployment-structure>