简体   繁体   English

嵌入式Jetty和复杂日志

[英]Embedded Jetty and Complex Logging

Jetty 9 is used for the embedded server and everything works well. Jetty 9用于嵌入式服务器,一切运行良好。 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. 在此之前,mvn:jetty-run带有自己的日志记录设置并记录到控制台。 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. 目前在初创公司SLF4J抱怨说,没有绑定可用,所以我们可以自由选择。

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 (eg logs/application-20130926.log) 在生产环境中,日志记录应该在一个日志文件中完成,但是在每日轮换时使用命名模式:logs / logname-date.log(例如logs / application-20130926.log)

We distinguish between the production and non-production mode using a command line argument '-production'. 我们使用命令行参数'-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. 由于jetty服务器是嵌入式的,我希望有一个解决方案,我们可以完全配置记录器,而无需管理xml或属性文件,使日志配置方面脱离部署过程。

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. 剩下的难题是如何以编程方式执行此操作而无需其他文件。

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 这在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: 您所需的日志记录jar文件:

The basic api jar: 基本api jar:

  • slf4j-api.jar (required, no matter what you choose below) slf4j-api.jar (必需,无论你在下面选择什么)

The log capturing jars: 日志捕获罐子:

(pick [0..n] jars here) (在这里挑选[0..n]罐子)

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

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) slf4j-simple.jar (这是一个超级简单的日志实现,不适合生产)
  • logback-classic.jar (slf4j's own output logging framework) logback-classic.jar (slf4j自己的输出日志框架)
    • also requires logback-core.jar 还需要logback-core.jar
  • slf4j-log4j12.jar (route slf4j events to log4j for processing) slf4j-log4j12.jar (将slf4j事件路由到log4j进行处理)
    • also requires log4j.jar 还需要log4j.jar
    • do not use if using log4j-over-slf4j.jar from above 如果使用上面的log4j-over-slf4j.jar ,请不要使用
  • slf4j-jdk14.jar (route slf4j events to java.util.logging for processing) slf4j-jdk14.jar (将slf4j事件路由到java.util.logging进行处理)
    • do not use if using jul-to-slf4j.jar from above 如果从上面使用jul-to-slf4j.jar ,请不要使用
  • slf4j-nop.jar (route slf4j events to nowhere, silently discard them) slf4j-nop.jar (将slf4j事件路由到无处,默默地丢弃它们)
  • slf4j-jcl.jar (route slf4j events to jakarta commons-logging) slf4j-jcl.jar (将slf4j事件路由到jakarta commons-logging)
    • also requires commons-logging.jar and your choice of commons-logging implementation. 还需要commons-logging.jar和你选择的commons-logging实现。
    • do not use if using jcl-over-slf4j.jar from above 如果使用上面的jcl-over-slf4j.jar ,请不要使用

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. 请务必阅读每个罐子上的slf4j手册 ,因为有时您可能需要了解一些额外的设置细节。

For your described situation, the most appropriate output jars would be logback-classic.jar or slf4j-log4j12.jar . 对于您描述的情况,最合适的输出jar将是logback-classic.jarslf4j-log4j12.jar As for configuring the output, you would need to rely on the documentation that those libraries provide. 至于配置输出,您需要依赖这些库提供的文档。

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 以编程方式完成所有日志记录配置后,此处仅描述: http//logback.qos.ch/manual/configuration.html#joranDirectly

I use the logback API just as stated by Joakim. 我正如Joakim所说的那样使用logback API。 Once you learn how to program it programmatically using the JoranConfigurator object everything is quite easy. 一旦您学会了如何使用JoranConfigurator对象以编程方式对其进行编程,一切都非常简单。 Play with it and you get the picture. 玩它,你得到的图片。

I managed to accomplished all tasks at hand. 我设法完成了手头的所有任务。

Thanks for the help Joakim. 感谢Joakim的帮助。 I was missing the JoranConfigurator thingy. 我错过了JoranConfigurator的东西。 Thanks! 谢谢!

Update: 更新:

I used a StringReader and embedded the xml configuration file directly in the Logging configuration class. 我使用了StringReader并将xml配置文件直接嵌入到Logging配置类中。 This way I dont have to manage additional files and logging works as expected. 这样我就不必按预期管理其他文件和日志记录工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM