add external jars to the classpath

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

add external jars to the classpath

Gaurav Kumar
Hi,
I have slf4j+logback specific jars currently pushed into each of my wars in webapp. This causes each web application to create its own log file. So I wanted to pull out these jars to a common location to be used by all the wars.

I tried to push them into jetty/lib or jetty/lib/ext but this causes them to be loaded at jetty startup which shows unwanted logging behaviour.

Is there a way I could add jars somewhere to jetty and notify the wars about their presence to be used by web applications (wars) but not by jetty startup?

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: add external jars to the classpath

Joakim Erdfelt-9
This blog might help.


--
Joakim Erdfelt <[hidden email]>
Developer advice, services and support
from the Jetty & CometD experts



On Wed, Oct 31, 2012 at 5:54 AM, Gaurav Kumar <[hidden email]> wrote:
Hi,
I have slf4j+logback specific jars currently pushed into each of my wars in webapp. This causes each web application to create its own log file. So I wanted to pull out these jars to a common location to be used by all the wars.

I tried to push them into jetty/lib or jetty/lib/ext but this causes them to be loaded at jetty startup which shows unwanted logging behaviour.

Is there a way I could add jars somewhere to jetty and notify the wars about their presence to be used by web applications (wars) but not by jetty startup?

Thanks.

Reply | Threaded
Open this post in threaded view
|

Re: add external jars to the classpath

Gaurav Kumar
In reply to this post by Gaurav Kumar
Hi Joakim,
Thankyou for the reference. Let me try to go through the blog and try out the options.




Joakim Erdfelt wrote:

This blog might help.


--
Joakim Erdfelt <[hidden email]>
Developer advice, services and support
from the Jetty & CometD experts



On Wed, Oct 31, 2012 at 5:54 AM, Gaurav Kumar <[hidden email]> wrote:
Hi,
I have slf4j+logback specific jars currently pushed into each of my wars in webapp. This causes each web application to create its own log file. So I wanted to pull out these jars to a common location to be used by all the wars.

I tried to push them into jetty/lib or jetty/lib/ext but this causes them to be loaded at jetty startup which shows unwanted logging behaviour.

Is there a way I could add jars somewhere to jetty and notify the wars about their presence to be used by web applications (wars) but not by jetty startup?

Thanks.

Reply | Threaded
Open this post in threaded view
|

Re: add external jars to the classpath

Gaurav Kumar
Hi

@Joakim: Thankyou for the suggested link. It definitely showcases common logging mechanism which is really helpful.
 
I had a rather more specific question where I want to place logback+slf4j jars at a common location in jetty environment so the webapps could read it but jetty should not load it during its startup. I am maintaining 2 logfiles, one for jetty logging (say, jetty.log) and one file for common application logging for multiple wars (say testapp.log) both of which should rollover at midnight and on every server startup.
If slf4j+logback jars are placed in jetty/lib directory then jetty starts logging as slf4j -> logback. I however, want jetty to continue logging as JUL in jetty.log, while, all other web applications should log using logback in testapp.log.

Also, if I maintain these jars at an alternate location outside the wars, how do I let all my wars know about this new classpath to be included in the loading context for each war?

Please suggest.
Reply | Threaded
Open this post in threaded view
|

Re: add external jars to the classpath

Joakim Erdfelt-9
You say "continue logging as JUL", I assume you mean java.util.logging, right?

You can rely on logback entirely, and setup a sifting logging adapter to accomplish your end goal of 2 log files, just use the filter org.eclipse.jetty for jetty.log, and others namespaces to webapp log adapter. 
However you have decided that jetty itself should use java.util.logging.

Note that Slf4J will initalize *very* early in the life of the JVM.  (this is important to understand)
Easily before Jetty even comes into play.

This will be a delicate configuration, but it might be possible.

The steps.

1) You'll want to configure Jetty's Log impl to be java.util.logging.
You can do that by setting the System Property ...
-Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.JavaUtilLog

2) Then you'll want to setup the slf4j api + slf4j impl jars in your ${jetty.home}/lib/logging directory (like the blog shows)
There are many slf4j implementation jars.
To have slf4j route to ....
 * Logback, use logback-classic.jar
 * Log4j, use slf4j-log4j12.jar, and log4j.jars
 * Apache Commons Logging, use sfl4j-jcl.jar, commons-logging.jar, and your commons-logging output implementation. (logging inception!)
 * java.util.logging, use slf4j-jdk14.jar

3) Then you'll want to configure your chosen logging framework in the mechanism best documented for that framework.

4) Grab the etc/jetty-webapp-logging.xml configuration (and jar files) outlined in the blog.
This will configure the deployed webapp to use the server side slf4j jars, and not the ones in the webapp itself.
Essentially, this is a tweak the WebApp's ClassLoader to force the server side on the webapp.

5) Setup the etc/jetty-webapp-logging.xml in the start.ini (as outlined in the blog).

6) Start Jetty.

Tip: setup a console logging appender in your chosen logging implementation while testing this out, so that you know if you have that level setup correctly.
If you see no logging on console, you have setup / configured something wrong.


--
Joakim Erdfelt <[hidden email]>
Developer advice, services and support
from the Jetty & CometD experts



On Sun, Nov 4, 2012 at 12:51 PM, Gaurav Kumar <[hidden email]> wrote:
Hi

@Joakim: Thankyou for the suggested link. It definitely showcases common logging mechanism which is really helpful.
 
I had a rather more specific question where I want to place logback+slf4j jars at a common location in jetty environment so the webapps could read it but jetty should not load it during its startup. I am maintaining 2 logfiles, one for jetty logging (say, jetty.log) and one file for common application logging for multiple wars (say testapp.log) both of which should rollover at midnight and on every server startup.
If slf4j+logback jars are placed in jetty/lib directory then jetty starts logging as slf4j -> logback. I however, want jetty to continue logging as JUL in jetty.log, while, all other web applications should log using logback in testapp.log.

Also, if I maintain these jars at an alternate location outside the wars, how do I let all my wars know about this new classpath to be included in the loading context for each war?

Please suggest.