Gustave
Gustave

Reputation: 3528

How to use Jenkins with a modern logging framework?

Jenkins uses by default Java Utils Logging. This is a somewhat outdated logging framework that lacks many features of modern logging frameworks like Log4J or Logback.

How can Jenkins be deployed/configured on Tomcat using Logback or Log4J?

Upvotes: 1

Views: 2127

Answers (1)

Gustave
Gustave

Reputation: 3528

Jenkins uses mainly Java Utils Logging (JUL), some dependencies and plugins use SLF4J. JUL cannot be replaced, but there is the possibility to redirect logger output to SLF4J (see http://www.slf4j.org/legacy.html#jul-to-slf4j) and propagate log configuration to JUL when using Logback (http://logback.qos.ch/manual/configuration.html#LevelChangePropagator).

Jenkins bundles the slf4j-jdk14 JUL binding (see https://github.com/jenkinsci/jenkins/blob/jenkins-1.620/war/pom.xml#L144). This binding must be replaced with logback. That can be done either by patching the jenkins-war pom or by manually replacing WEB-INF/lib/slf4j-jdk14-1.7.7.jar with logback-classic-1.1.3.jar and logback-core-1.1.3.jar (Jenkins-1.620).

The JUL bridge can be installed by putting a logging.properties file (JULI configuration, see https://tomcat.apache.org/tomcat-7.0-doc/logging.html#Using_java.util.logging_%28default%29) at WEB-INF/classes/ containing

handlers = org.slf4j.bridge.SLF4JBridgeHandler
.handlers = org.slf4j.bridge.SLF4JBridgeHandler

Following an example logback configuration (to be put at WEB-INF/classes/logback.xml) that enables Logback internal logging (debug="true") and activates the LevelChangePropagator:

<configuration debug="true">
  <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator" />
  <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>/home/jenkins/work/log/warn-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
      <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
        <maxFileSize>10MB</maxFileSize>
      </timeBasedFileNamingAndTriggeringPolicy>
      <maxHistory>50</maxHistory>
    </rollingPolicy>
    <encoder>
      <pattern>%date{HH:mm:ss.SSS} %-5level [%thread] %logger %msg%n</pattern>
    </encoder>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>WARN</level>
    </filter>
  </appender>   
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%-4relative %-5level [%thread] %logger %msg %n</pattern>
    </encoder>
  </appender>
  <root level="debug">
    <appender-ref ref="WARN" />
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

Tomcat JULI logging is a Tomcat-specific LogManager (http://docs.oracle.com/javase/7/docs/api/java/util/logging/LogManager.html) implementation that allows a ClassLoader-specific JUL configuration. Thus the scope of the configuration above is specific to the Jenkins webapp.

Upvotes: 0

Related Questions