{"id":34443,"date":"2016-06-30T18:58:15","date_gmt":"2016-06-30T13:28:15","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=34443"},"modified":"2016-07-01T11:44:36","modified_gmt":"2016-07-01T06:14:36","slug":"setting-logback-in-jetty","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/setting-logback-in-jetty\/","title":{"rendered":"Setting up Logback in Jetty-9.2.16"},"content":{"rendered":"<p>Log files record all the activity of a program during its execution lifetime. Generally used for diagnosing problems, auditing, debugging, or information gathering.<\/p>\n<p><b>Why Logback?<\/b><\/p>\n<ol>\n<li>For automatically removing old logs.<\/li>\n<li>It also automatically compresses archived log files.<\/li>\n<li>Provides a wide array of filtering capabilities.<\/li>\n<li>Logback-access, i.e. HTTP-access logging with brains, is an integral part of Logback.<\/li>\n<\/ol>\n<p>Before using Logback, let&#8217;s understand what &#8220;<strong>SLF4J&#8221;<\/strong> is.<\/p>\n<p>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.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-36852\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/lblogo.jpg\" alt=\"lblogo\" width=\"260\" height=\"140\" \/><\/p>\n<p><span style=\"text-decoration: underline\"><b>Logback<\/b><\/span> 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.<br \/>\nLogback is divided into three modules <b>logback-core<\/b>,\u00a0<b>logback-classic<\/b> and <b>logback-access<\/b>.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-36849\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/logback_arch.png\" alt=\"logback_arch\" width=\"336\" height=\"205\" \/><\/p>\n<p>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.<\/p>\n<p>The logback-core module forms the foundation upon which the other two modules are built. Interestingly enough, logback-core has no notion of \u201clogger\u201d. Logback-classic relies on logback-core for basic services. It natively implements the SLF4J API.<\/p>\n<p><b>Jetty<\/b> determines its logging behavior according to the following rules:<\/p>\n<ul>\n<ul>1.\u00a0What 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.<\/ul>\n<\/ul>\n<ul>2. If org.slf4j.Logger exists in the classpath,then \u00a0logging is done by SLF4J.<\/ul>\n<ul>3. If none of them exists, then default org.eclipse.jetty.util.log.StdErrLog logging behavior is executed.<\/ul>\n<p>The modules of Jetty are enabled or disabled by start.ini file under our JETTY_HOME directory.<\/p>\n<p>To activate logging module, certain changes had to be done as follows:<\/p>\n<ul>1. Go to the JETTY_HOME directory.<\/ul>\n<ul>2. Open start.ini file.<\/ul>\n<ul>3. Add the following line to start.ini and save it:<\/ul>\n<p>[js]<br \/>\n &#8211;module=logging<br \/>\n[\/js]<\/p>\n<p>To configure SLF4J with Logback, first we have to include following JAR files:<\/p>\n<ul>1. SLF4J API(http:\/\/central.maven.org\/maven2\/org\/slf4j\/slf4j-api\/1.6.6\/slf4j-api-1.6.6.jar)<\/ul>\n<ul>2. logback-core(http:\/\/central.maven.org\/maven2\/ch\/qos\/logback\/logback-core\/1.0.7\/logback-core-1.0.7.jar)<\/ul>\n<ul>3. logback classic(http:\/\/central.maven.org\/maven2\/ch\/qos\/logback\/logback-classic\/1.0.7\/logback-classic-1.0.7.jar)<\/ul>\n<p>After that, we have to follow below steps:<\/p>\n<ul>1. Create the directory logging under JETTY_HOME.<\/ul>\n<p>[js]<br \/>\nmkdir ${jetty.home}\/lib\/logging<br \/>\n[\/js]<\/p>\n<ul>2. Copy 3 JAR files which we downloaded from above steps to this directory (JETTY_HOME\/logging)<\/ul>\n<p>After adding the files to our classpath, we should (although not mandatory) add a logback.xml file to the directory JETTY_HOME\/resources.<br \/>\nInside ${jetty.home}\/resources\/logback.xml<\/p>\n<p>[js]&lt;?xml version=&quot;1.0&quot;?&gt;<br \/>\n&lt;configuration&gt;<br \/>\n&lt;!&#8211; ################################################################ &#8211;&gt;<br \/>\n    &lt;appender name=&quot;ERROR-FILE&quot; class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;<br \/>\n        &lt;file&gt;log\/error_logfile.log&lt;\/file&gt;<br \/>\n        &lt;filter class=&quot;ch.qos.logback.classic.filter.LevelFilter&quot;&gt;<br \/>\n                &lt;level&gt;ERROR&lt;\/level&gt;<br \/>\n                &lt;onMatch&gt;ACCEPT&lt;\/onMatch&gt;<br \/>\n                &lt;onMismatch&gt;DENY&lt;\/onMismatch&gt;<br \/>\n        &lt;\/filter&gt;<br \/>\n\t &lt;rollingPolicy class=&quot;ch.qos.logback.core.rolling.FixedWindowRollingPolicy&quot;&gt;<br \/>\n                &lt;fileNamePattern&gt;log\/zipped\/error.%i.log.zip&lt;\/fileNamePattern&gt;<br \/>\n                &lt;minIndex&gt;1&lt;\/minIndex&gt;<br \/>\n                &lt;maxIndex&gt;60&lt;\/maxIndex&gt;<br \/>\n        &lt;\/rollingPolicy&gt;<\/p>\n<p>        &lt;triggeringPolicy class=&quot;ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy&quot;&gt;<br \/>\n                &lt;maxFileSize&gt;100MB&lt;\/maxFileSize&gt;<br \/>\n        &lt;\/triggeringPolicy&gt;<br \/>\n        &lt;append&gt;true&lt;\/append&gt;<br \/>\n        &lt;encoder&gt;<br \/>\n            &lt;pattern&gt;%-4relative [%thread] %-5level %logger{35} &#8211; %msg %n&lt;\/pattern&gt;<br \/>\n        &lt;\/encoder&gt;<br \/>\n    &lt;\/appender&gt;<br \/>\n&lt;!&#8211; ################################################################ &#8211;&gt;<br \/>\n&lt;!&#8211; ################################################################ &#8211;&gt;<br \/>\n    &lt;appender name=&quot;WARN-FILE&quot; class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;<br \/>\n        &lt;file&gt;log\/warn_logfile.log&lt;\/file&gt;<br \/>\n        &lt;filter class=&quot;ch.qos.logback.classic.filter.LevelFilter&quot;&gt;<br \/>\n                &lt;level&gt;WARN&lt;\/level&gt;<br \/>\n                &lt;onMatch&gt;ACCEPT&lt;\/onMatch&gt;<br \/>\n                &lt;onMismatch&gt;DENY&lt;\/onMismatch&gt;<br \/>\n        &lt;\/filter&gt;<br \/>\n \t&lt;rollingPolicy class=&quot;ch.qos.logback.core.rolling.FixedWindowRollingPolicy&quot;&gt;<br \/>\n                &lt;fileNamePattern&gt;log\/zipped\/warn.%i.log.zip&lt;\/fileNamePattern&gt;<br \/>\n                &lt;minIndex&gt;1&lt;\/minIndex&gt;<br \/>\n                &lt;maxIndex&gt;60&lt;\/maxIndex&gt;<br \/>\n        &lt;\/rollingPolicy&gt;<\/p>\n<p>        &lt;triggeringPolicy class=&quot;ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy&quot;&gt;<br \/>\n                &lt;maxFileSize&gt;100MB&lt;\/maxFileSize&gt;<br \/>\n        &lt;\/triggeringPolicy&gt;<br \/>\n        &lt;append&gt;true&lt;\/append&gt;<br \/>\n        &lt;encoder&gt;<br \/>\n            &lt;pattern&gt;%-4relative [%thread] %-5level %logger{35} &#8211; %msg %n&lt;\/pattern&gt;<br \/>\n        &lt;\/encoder&gt;<br \/>\n    &lt;\/appender&gt;<br \/>\n&lt;!&#8211; ################################################################ &#8211;&gt;<br \/>\n&lt;!&#8211; ################################################################ &#8211;&gt;<br \/>\n    &lt;appender name=&quot;INFO-FILE&quot; class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;<br \/>\n        &lt;file&gt;log\/info_logfile.log&lt;\/file&gt;<br \/>\n        &lt;filter class=&quot;ch.qos.logback.classic.filter.LevelFilter&quot;&gt;<br \/>\n                &lt;level&gt;INFO&lt;\/level&gt;<br \/>\n                &lt;onMatch&gt;ACCEPT&lt;\/onMatch&gt;<br \/>\n                &lt;onMismatch&gt;DENY&lt;\/onMismatch&gt;<br \/>\n        &lt;\/filter&gt;<br \/>\n\t &lt;rollingPolicy class=&quot;ch.qos.logback.core.rolling.FixedWindowRollingPolicy&quot;&gt;<br \/>\n                &lt;fileNamePattern&gt;log\/zipped\/info.%i.log.zip&lt;\/fileNamePattern&gt;<br \/>\n                &lt;minIndex&gt;1&lt;\/minIndex&gt;<br \/>\n                &lt;maxIndex&gt;60&lt;\/maxIndex&gt;<br \/>\n        &lt;\/rollingPolicy&gt;<br \/>\n        &lt;triggeringPolicy class=&quot;ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy&quot;&gt;<br \/>\n                &lt;maxFileSize&gt;100MB&lt;\/maxFileSize&gt;<br \/>\n        &lt;\/triggeringPolicy&gt;<br \/>\n        &lt;append&gt;true&lt;\/append&gt;<br \/>\n        &lt;encoder&gt;<br \/>\n            &lt;pattern&gt;%-4relative [%thread] %-5level %logger{35} &#8211; %msg %n&lt;\/pattern&gt;<br \/>\n        &lt;\/encoder&gt;<br \/>\n    &lt;\/appender&gt;<br \/>\n&lt;!&#8211; ################################################################ &#8211;&gt;<br \/>\n&lt;!&#8211; ################################################################ &#8211;&gt;<br \/>\n    &lt;appender name=&quot;DEBUG-FILE&quot; class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;<br \/>\n        &lt;file&gt;log\/debug_logfile.log&lt;\/file&gt;<br \/>\n        &lt;filter class=&quot;ch.qos.logback.classic.filter.LevelFilter&quot;&gt;<br \/>\n                &lt;level&gt;DEBUG&lt;\/level&gt;<br \/>\n                &lt;onMatch&gt;ACCEPT&lt;\/onMatch&gt;<br \/>\n                &lt;onMismatch&gt;DENY&lt;\/onMismatch&gt;<br \/>\n        &lt;\/filter&gt;<\/p>\n<p>\t &lt;rollingPolicy class=&quot;ch.qos.logback.core.rolling.FixedWindowRollingPolicy&quot;&gt;<br \/>\n                &lt;fileNamePattern&gt;log\/zipped\/debug.%i.log.zip&lt;\/fileNamePattern&gt;<br \/>\n                &lt;minIndex&gt;1&lt;\/minIndex&gt;<br \/>\n                &lt;maxIndex&gt;60&lt;\/maxIndex&gt;<br \/>\n        &lt;\/rollingPolicy&gt;<\/p>\n<p>        &lt;triggeringPolicy class=&quot;ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy&quot;&gt;<br \/>\n                &lt;maxFileSize&gt;100MB&lt;\/maxFileSize&gt;<br \/>\n        &lt;\/triggeringPolicy&gt;<br \/>\n        &lt;append&gt;true&lt;\/append&gt;<br \/>\n        &lt;encoder&gt;<br \/>\n            &lt;pattern&gt;%-4relative [%thread] %-5level %logger{35} &#8211; %msg %n&lt;\/pattern&gt;<br \/>\n        &lt;\/encoder&gt;<br \/>\n    &lt;\/appender&gt;<br \/>\n&lt;!&#8211; ################################################################ &#8211;&gt;<br \/>\n&lt;!&#8211; ################################################################ &#8211;&gt;<br \/>\n    &lt;appender name=&quot;FILE&quot; class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;<br \/>\n        &lt;file&gt;log\/all.log&lt;\/file&gt;<br \/>\n        &lt;filter class=&quot;ch.qos.logback.classic.filter.ThresholdFilter&quot;&gt;<br \/>\n                &lt;level&gt;TRACE&lt;\/level&gt;<br \/>\n        &lt;\/filter&gt;<\/p>\n<p>\t&lt;rollingPolicy class=&quot;ch.qos.logback.core.rolling.FixedWindowRollingPolicy&quot;&gt;<br \/>\n      \t\t&lt;fileNamePattern&gt;log\/zipped\/all.%i.log.zip&lt;\/fileNamePattern&gt;<br \/>\n      \t\t&lt;minIndex&gt;1&lt;\/minIndex&gt;<br \/>\n      \t\t&lt;maxIndex&gt;60&lt;\/maxIndex&gt;<br \/>\n    \t&lt;\/rollingPolicy&gt;<br \/>\n        &lt;triggeringPolicy class=&quot;ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy&quot;&gt;<br \/>\n      \t\t&lt;maxFileSize&gt;100MB&lt;\/maxFileSize&gt;<br \/>\n        &lt;\/triggeringPolicy&gt;<br \/>\n        &lt;append&gt;true&lt;\/append&gt;<br \/>\n        &lt;encoder&gt;<br \/>\n            &lt;pattern&gt;%-4relative [%thread] %-5level %logger{35} &#8211; %msg %n&lt;\/pattern&gt;<br \/>\n        &lt;\/encoder&gt;<br \/>\n    &lt;\/appender&gt;<br \/>\n&lt;!&#8211; ################################################################ &#8211;&gt;<br \/>\n    &lt;root&gt;<br \/>\n\t&lt;appender-ref ref=&quot;FILE&quot;\/&gt;<br \/>\n        &lt;appender-ref ref=&quot;WARN-FILE&quot;\/&gt;<br \/>\n        &lt;appender-ref ref=&quot;ERROR-FILE&quot;\/&gt;<br \/>\n        &lt;appender-ref ref=&quot;DEBUG-FILE&quot;\/&gt;<br \/>\n        &lt;appender-ref ref=&quot;INFO-FILE&quot;\/&gt;<br \/>\n    &lt;\/root&gt;<br \/>\n&lt;\/configuration&gt;[\/js]<\/p>\n<p>Creating log directory in ${jetty home}\/<\/p>\n<p>[js]<br \/>\nmkdir -p ${jetty home}log\/zipped<br \/>\n[\/js]<\/p>\n<p>Restart Jetty and now we can see logs inside ${jetty.home}\/log\/<br \/>\nThe above configuration file will create ERROR, WARN, INFO, DEBUG log files where according to the log levels log are stored.<br \/>\nAddition to this, if the\u00a0size of the file becomes equal to 100MB or greater then it will create a zip of log file.<\/p>\n<p>References:https:\/\/wiki.eclipse.org\/Jetty\/Tutorial\/Sifting_Logs_with_Logback<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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? For automatically removing old logs. It also automatically compresses archived log files. Provides a wide array of filtering capabilities. Logback-access, i.e. HTTP-access logging with brains, is an integral part of [&hellip;]<\/p>\n","protected":false},"author":919,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":51},"categories":[2348,1],"tags":[3660,3686,2540,3685,3659,3684,2785,2786,3687],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/34443"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/919"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=34443"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/34443\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=34443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=34443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=34443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}