Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] hide slf4j classes from web-apps

Don't use lib/ext for your situation.  It is a special directory that is loaded before the rest of Jetty and will cause you problems.

Do this instead ...

1) Create a ${jetty.home}/lib/logging directory and put all of the server side logging files you want to use for JUST THE SERVER to use.
Note: these jars cannot be used by the webapps.

What I have ...
[jetty-dist]$ ls -1 lib/logging/
logback-classic-1.0.0.jar
logback-core-1.0.0.jar
slf4j-api-1.6.4.jar

2) Add logging to OPTIONS in your start.ini

What I have ...
[jetty-dist]$ grep OPTIONS start.ini 
OPTIONS=logging,Server,jsp,jmx,resources,websocket,ext,plus,annotations

3) Add your logging configuration to ${jetty.home}/resources/

What I have ...
[jetty-dist]$ cat resources/logback.xml 
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${jetty.home}/logs/jetty.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>jetty-%d{yyyy-MM-dd}.log</fileNamePattern>
      <!-- keep 30 days' worth of history -->
      <maxHistory>30</maxHistory>
    </rollingPolicy>

    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="INFO">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
  </root>
</configuration>

4) Copy your personal war file into ${jetty.home}/webapps/

What I have ...
[jetty-dist]$ ls webapps/
jetty-slf4j-test.war

5) Make sure your personal war file has all of the jars and configurations it needs.

What I have ...
[jetty-dist]$ jar -tvf webapps/jetty-slf4j-test.war | grep -E "(lib|log|slf4j)"
  26083 Thu Jan 03 10:35:10 MST 2013 WEB-INF/lib/slf4j-api-1.7.2.jar
   8819 Thu Jan 03 10:35:10 MST 2013 WEB-INF/lib/slf4j-log4j12-1.7.2.jar
 489884 Tue Oct 02 13:51:38 MST 2012 WEB-INF/lib/log4j-1.2.17.jar
    900 Thu Jan 03 10:55:38 MST 2013 WEB-INF/classes/log4j.xml

6) Now setup your deployment context to prevent the webapp from seeing and using the server side jars

What I have ...
[jetty-dist]$ cat contexts/jetty-slf4j.xml
<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/test</Set>
  <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/jetty-slf4j-test.war</Set>
  <Call name="addServerClass">
    <Arg type="String">org.slf4j.</Arg>
  </Call>
  <Call name="addServerClass">
    <Arg type="String">org.apache.log4j.</Arg>
  </Call>
</Configure>

6) Now start jetty and try it out

In the above example I use logback on the server side (my preferred), and the webapp uses log4j.
Ideally, this difference isn't necessary, both the server and the webapp can have the same basic logging technology, but they cannot share the jars or configuration files.

--
Joakim Erdfelt <joakim@xxxxxxxxxxx>
Developer advice, services and support
from the Jetty & CometD experts


On Wed, Jan 2, 2013 at 10:57 PM, psfung <psfung@xxxxxxxxxxxxxxx> wrote:
I use slf4j with log4j for Jetty (8.1.8) server logs. I follow
http://wiki.eclipse.org/Jetty/Reference/Jetty_Classloading and add
slf4j-api-1.6.6.jar, slf4j-log4j12-1.6.6.jar and log4j-1.2.17.jar in
lib/ext. It works fine.

I'd like to allow web-apps to have different slf4j bindings, so I've added
the following to hide slf4j and log4j classes from web-apps:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Call name="addServerClass">
    <Arg type="String">org.slf4j.</Arg>
  </Call>
  <Call name="addServerClass">
    <Arg type="String">org.apache.log4j.</Arg>
  </Call>
</Configure>

But I still see warning in stderr:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in
[jar:file:/C:/jetty/webapps/CUI/WEB-INF/lib/slf4j-log4j12-1.6.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in
[jar:file:/C:/jetty/lib/ext/slf4j-log4j12-1.6.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]

What else should I do?




--
View this message in context: http://jetty.4.n6.nabble.com/hide-slf4j-classes-from-web-apps-tp4959778.html
Sent from the Jetty User mailing list archive at Nabble.com.
_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-users


Back to the top