首页 文章

如何在Jetty中为所有上下文添加服务器类?

提问于
浏览
1

我有jetty配置为使用slf4j和logback . 版本8.x

根据这个地方http://dev.eclipse.org/mhonarc/lists/jetty-users/msg02859.html,您可以在每个上下文的jetty服务器类列表中添加这些类,因此Web应用程序无法看到slf4j服务器类 .

我不希望为我部署的每场战争都这样做 . 是否可以只配置服务器的所有类AND资源(缺省情况下对Web应用程序可见$ JETTY_HOME / resources / logback.xml)在部署的webapps的类加载器中不可用?

1 回答

  • 1

    是的,你可以有一个参与deployment lifecycle of webapps的 class .

    示例类:

    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日志记录实现) .

相关问题