3

I read about how to make java.util.logging logs and System.(out|err) output be piped to slf4j:

But it requires some jar/classes and code to be executed at jetty startup. Basically something like:

SLF4JBridgeHandler.install() // to pipe j.u.l SysOutOverSLF4J.sendSystemOutAndErrToSLF4J() // to pipe System.(out|err)

I know how to add required jars/classes available to jetty (just add sysout-over-slf4j.jar and jul-to-slf4j.jar to $JETTY_HOME/lib/ext), but I don't know how can I run the code abobe in the server startup so that jetty configures the consolidated outputs (sysout and j.u.l) to slf4j. I can run that code per webapp, but to make things correct, it should be done by jetty, and not by every webapp that needs this behaviour.

Should this be done in a different way? Is there a non-voodo way to do this?

Please make your answer be for jetty version >= 8

Community
  • 1
  • 1
David Hofmann
  • 5,683
  • 12
  • 50
  • 78

1 Answers1

1

I don't know if this will work, but have you tried it?

public class MyWebappServlet ... {
  static {
    // to pipe j.u.l
    SLF4JBridgeHandler.install();

    // to pipe System.(out|err)
    SysOutOverSLF4J.sendSystemOutAndErrToSLF4J();
  }
  ...

And in your web.xml:

<servlet>
  <servlet-name>MyWebapp</servlet-name>
  <servlet-class>MyWebappServlet</servlet-class>
</servlet>

It might not intercept everything from the start, but once Jetty loads your MyWebappServlet class the logging should be redirected.

Otherwise you would need to customize your Jetty with those two lines.

UPDATE: There is one other way - to use a main() wrapper like BCEL to start Jetty but only after those logging redirections have been done.

In this case the command line would be something like this:

java my.custom.JavaWrapper org.eclipse.jetty.start.Main [arguments]

If this could be done in jetty.sh then it would apply to all web applications in that server instance.

Cebence
  • 2,406
  • 2
  • 19
  • 20