【问题标题】:How to add server classes for all contexts in Jetty?如何为 Jetty 中的所有上下文添加服务器类?
【发布时间】:2013-01-28 15:52:14
【问题描述】:

我已将码头配置为使用 slf4j 和 logback。版本 8.x

根据http://dev.eclipse.org/mhonarc/lists/jetty-users/msg02859.html 这个地方,您可以将这些类添加到每个上下文的码头服务器类列表中,这样 Web 应用程序就看不到 slf4j 服务器类。

我不想为我部署的每场战争都这样做。是否可以只配置一次服务器的所有类和资源(默认情况下,webapps 可以看到 $JETTY_HOME/resources/logback.xml)在已部署的 webapps 的类加载器中不可用?

【问题讨论】:

    标签: servlets jetty slf4j logback


    【解决方案1】:

    是的,您可以有一个参与deployment lifecycle of webapps 的课程。

    示例类:

    package example.deploy.logging.config;
    
    import org.eclipse.jetty.deploy.App;
    import org.eclipse.jetty.deploy.AppLifeCycle;
    import org.eclipse.jetty.deploy.graph.Node;
    import org.eclipse.jetty.server.handler.ContextHandler;
    import org.eclipse.jetty.webapp.WebAppContext;
    
    public class LoggingConfigBinding implements AppLifeCycle.Binding
    {
        public String[] getBindingTargets()
        {
            return new String[]
            { "deploying" };
        }
    
        public void processBinding(Node node, App app) throws Exception
        {
            ContextHandler handler = app.getContextHandler();
            if (handler == null)
            {
                throw new NullPointerException("No Handler created for App: " + app);
            }
    
            if (handler instanceof WebAppContext)
            {
                WebAppContext webapp = (WebAppContext)handler;
                webapp.addSystemClass("org.slf4j.");
            }
        }
    }
    

    然后你将有一段 XML 将其加载到DeploymentManager

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
    <Configure id="Server" class="org.eclipse.jetty.server.Server">
      <Ref id="DeploymentManager">
        <Call name="addLifeCycleBinding">
          <Arg>
            <New class="example.deploy.logging.config.LoggingConfigBinding">
            </New>
          </Arg>
        </Call>
      </Ref>
    </Configure>
    

    更完整的例子见Sifting Logs in Logback on Jetty(注意:logback 是一个 slf4j 日志实现)。

    【讨论】:

    • 答案中的“在 Logback 上筛选日志”参考中包含 git.codehaus.org 和 github 上所有各种完整项目的链接。请务必阅读。有关该代码库的真实示例,请查看此项目:git.codehaus.org/…
    • 我读过“在 Jetty 上的 Logback 中筛选日志”,但我确实需要在类加载器级别隔离每个 Web 应用程序的日志记录。
    • 谢谢!,您的解决方案确实有效。现在我确认 webapp 正在使用 $JETTY_HOME/resources/logback.xml 而不是 webapp.war[WEB-INF/classes/logback.xml] 我可以像处理一样处理 logback.xml 文件吗LoggingConfigBinding 中的类包?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 2021-06-27
    相关资源
    最近更新 更多