Setting up Logback in Jetty-9.2.16

30 / Jun / 2016 by Anup Yadav 0 comments

Log files record all the activity of a program during its execution lifetime. Generally used for diagnosing problems, auditing, debugging, or information gathering.

Why Logback?

  1. For automatically removing old logs.
  2. It also automatically compresses archived log files.
  3. Provides a wide array of filtering capabilities.
  4. Logback-access, i.e. HTTP-access logging with brains, is an integral part of Logback.

Before using Logback, let’s understand what “SLF4J” is.

SLF4J(Simple Logging Facade for Java) is used for logging systems allowing the end-user to plug-in the desired logging system at deployment time.

lblogo

Logback is the successor of log4j logger API, but Logback offers some advantages over log4j, like better performance and less memory consumption, automatic reloading of configuration files, or filter capabilities, to cite a few features.
Logback is divided into three modules logback-corelogback-classic and logback-access.

logback_arch

Logback-classic implements the SLF4J API so that we can readily switch back and forth between Logback and other logging systems. The third module called logback-access integrates with Servlet containers to provide HTTP-access log functionality.

The logback-core module forms the foundation upon which the other two modules are built. Interestingly enough, logback-core has no notion of “logger”. Logback-classic relies on logback-core for basic services. It natively implements the SLF4J API.

Jetty determines its logging behavior according to the following rules:

      1. What happens first is that the value of the property org.eclipse.jetty.util.log.class is checked. If it is already defined, the logger implements that class.
    2. If org.slf4j.Logger exists in the classpath,then  logging is done by SLF4J.
    3. If none of them exists, then default org.eclipse.jetty.util.log.StdErrLog logging behavior is executed.

The modules of Jetty are enabled or disabled by start.ini file under our JETTY_HOME directory.

To activate logging module, certain changes had to be done as follows:

    1. Go to the JETTY_HOME directory.
    2. Open start.ini file.
    3. Add the following line to start.ini and save it:
 --module=logging

To configure SLF4J with Logback, first we have to include following JAR files:

    1. SLF4J API(http://central.maven.org/maven2/org/slf4j/slf4j-api/1.6.6/slf4j-api-1.6.6.jar)
    2. logback-core(http://central.maven.org/maven2/ch/qos/logback/logback-core/1.0.7/logback-core-1.0.7.jar)
    3. logback classic(http://central.maven.org/maven2/ch/qos/logback/logback-classic/1.0.7/logback-classic-1.0.7.jar)

After that, we have to follow below steps:

    1. Create the directory logging under JETTY_HOME.
mkdir ${jetty.home}/lib/logging
    2. Copy 3 JAR files which we downloaded from above steps to this directory (JETTY_HOME/logging)

After adding the files to our classpath, we should (although not mandatory) add a logback.xml file to the directory JETTY_HOME/resources.
Inside ${jetty.home}/resources/logback.xml

<?xml version="1.0"?>
<configuration>
<!-- ################################################################ -->
    <appender name="ERROR-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>log/error_logfile.log</file>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>ERROR</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
        </filter>
	 <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <fileNamePattern>log/zipped/error.%i.log.zip</fileNamePattern>
                <minIndex>1</minIndex>
                <maxIndex>60</maxIndex>
        </rollingPolicy>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <append>true</append>
        <encoder>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>
<!-- ################################################################ -->
<!-- ################################################################ -->
    <appender name="WARN-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>log/warn_logfile.log</file>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>WARN</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
        </filter>
 	<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <fileNamePattern>log/zipped/warn.%i.log.zip</fileNamePattern>
                <minIndex>1</minIndex>
                <maxIndex>60</maxIndex>
        </rollingPolicy>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <append>true</append>
        <encoder>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>
<!-- ################################################################ -->
<!-- ################################################################ -->
    <appender name="INFO-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>log/info_logfile.log</file>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>INFO</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
        </filter>
	 <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <fileNamePattern>log/zipped/info.%i.log.zip</fileNamePattern>
                <minIndex>1</minIndex>
                <maxIndex>60</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <append>true</append>
        <encoder>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>
<!-- ################################################################ -->
<!-- ################################################################ -->
    <appender name="DEBUG-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>log/debug_logfile.log</file>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>DEBUG</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
        </filter>

	 <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <fileNamePattern>log/zipped/debug.%i.log.zip</fileNamePattern>
                <minIndex>1</minIndex>
                <maxIndex>60</maxIndex>
        </rollingPolicy>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <append>true</append>
        <encoder>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>
<!-- ################################################################ -->
<!-- ################################################################ -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>log/all.log</file>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>TRACE</level>
        </filter>

	<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      		<fileNamePattern>log/zipped/all.%i.log.zip</fileNamePattern>
      		<minIndex>1</minIndex>
      		<maxIndex>60</maxIndex>
    	</rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      		<maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <append>true</append>
        <encoder>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>
<!-- ################################################################ -->
    <root>
	<appender-ref ref="FILE"/>
        <appender-ref ref="WARN-FILE"/>
        <appender-ref ref="ERROR-FILE"/>
        <appender-ref ref="DEBUG-FILE"/>
        <appender-ref ref="INFO-FILE"/>
    </root>
</configuration>

Creating log directory in ${jetty home}/

mkdir -p ${jetty home}log/zipped

Restart Jetty and now we can see logs inside ${jetty.home}/log/
The above configuration file will create ERROR, WARN, INFO, DEBUG log files where according to the log levels log are stored.
Addition to this, if the size of the file becomes equal to 100MB or greater then it will create a zip of log file.

References:https://wiki.eclipse.org/Jetty/Tutorial/Sifting_Logs_with_Logback

FOUND THIS USEFUL? SHARE IT

Leave a comment -