Embedded Jetty and Complex Logging

2019-06-05 19:07发布

问题:

Jetty 9 is used for the embedded server and everything works well. One thing that remains is the logging issue.

Prior to that mvn:jetty-run brings his own logging setup with it and logs to the console. That is good for development. In the production environment we need something more special.

Currently on start-up the SLF4J complains about, that there is no binding available, so we can chose freely.

That is what we want to archive:

  1. We need to log to the console if we are starting in a non-production environment.
  2. In the production environment the logging should be done in a single log-file but on a daily rotation with the naming schema: logs/logname-date.log (e.g. logs/application-20130926.log)

We distinguish between the production and non-production mode using a command line argument '-production'.

Since the jetty server is embedded I would love to have a solution which we can fully configure the logger without the need to manage xml or properties-files taking the logging configuration aspect out of the deployment process.

So what options do we have and how can we do this in the best possible way?

Update: It seems that logback is the way to go. It has support for the logfile rotation and also makes it possible to use a console output. The difficult question remaining is how to do this programatically and without additional files.

回答1:

You have hundreds of configuration options here.

You will need to know a few things about your application before you can pick an appropriate configuration here.

How are logging events emitted from your code? the jetty server? and all 3rd party libraries?

Then you want to answer, what logging framework do you want to handle output (to disk, and to console) portions of the logging architecture?

This is documented at Jetty: http://www.eclipse.org/jetty/documentation/current/example-logging-logback-centralized.html

Yes, that documentation isn't for embedded mode, but it is still relevant.

Your required logging jar files:

The basic api jar:

  • slf4j-api.jar (required, no matter what you choose below)

The log capturing jars:

(pick [0..n] jars here)

  • log4j-over-slf4j.jar (slf4j handling of log4j events)
  • jul-to-slf4j.jar (slf4j handling of java.util.logging events)
  • jcl-over-slf4j.jar (slf4j handling of jakarta commons-logging events)

The log output jars:

Pick only 1 of the following output jars:

  • slf4j-simple.jar (this is a super simple logging implementation, not suitable for production)
  • logback-classic.jar (slf4j's own output logging framework)
    • also requires logback-core.jar
  • slf4j-log4j12.jar (route slf4j events to log4j for processing)
    • also requires log4j.jar
    • do not use if using log4j-over-slf4j.jar from above
  • slf4j-jdk14.jar (route slf4j events to java.util.logging for processing)
    • do not use if using jul-to-slf4j.jar from above
  • slf4j-nop.jar (route slf4j events to nowhere, silently discard them)
  • slf4j-jcl.jar (route slf4j events to jakarta commons-logging)
    • also requires commons-logging.jar and your choice of commons-logging implementation.
    • do not use if using jcl-over-slf4j.jar from above

Configure it all:

Be sure you read the slf4j manual on each of these jars, as there is sometimes some extra setup details you might need to know about.

For your described situation, the most appropriate output jars would be logback-classic.jar or slf4j-log4j12.jar. As for configuring the output, you would need to rely on the documentation that those libraries provide.

  • Logback Documentation
  • Log4j Wiki


回答2:

So finally here is the complete picture.

After all the logging configuration in a programmatic way is just described here: http://logback.qos.ch/manual/configuration.html#joranDirectly

I use the logback API just as stated by Joakim. Once you learn how to program it programmatically using the JoranConfigurator object everything is quite easy. Play with it and you get the picture.

I managed to accomplished all tasks at hand.

Thanks for the help Joakim. I was missing the JoranConfigurator thingy. Thanks!

Update:

I used a StringReader and embedded the xml configuration file directly in the Logging configuration class. This way I dont have to manage additional files and logging works as expected.