配置jetty服务器日志记录

时间:2013-11-05 16:47:28

标签: spring jetty embedded-jetty

我们想让jetty使用log4j配置进行日志记录。我基于本教程在spring中使用嵌入式jetty。我遵循此文档,在classpath中包含所有必需的slf4j和log4j依赖项。

基本上我使用以下配置将jetty-logging.properties添加到classpath。

org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.Slf4Log
org.eclipse.jetty.LEVEL=INFO

我在classpath中也有log4j.properties

我有以下maven依赖项来包含必需的jar。

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.log4j.version}</version>
    </dependency>

            <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>${jetty.maven.version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>8.1.9.v20130131</version>
    </dependency>

但是当我通过spring启动jetty服务器时,日志输出总是记录到StdErr并且它没有记录到我在log4j.properties中配置的日志文件。我错过了什么吗?是否有任何调试我可以打开进行故障排除..?我怎样才能让它发挥作用?

2 个答案:

答案 0 :(得分:0)

我通过码头mailing list得到了答案。

答案 1 :(得分:0)

虽然这个问题的答案确实帮我找到了答案,但我花了一些时间来挖掘它。

我在使用Spring MVC和log4j。以下

public class LaunchServer {

   private static final int SERVER_PORT = 8008;

   public static void main(String[] args) throws Exception {
      //Make sure you set the log before you start the server
      Log.setLog(new Slf4jLog());

      Server server = new Server();

      ServerConnector connector = new ServerConnector(server);
      connector.setPort(SERVER_PORT);
      server.addConnector(connector);

      WebAppContext wac = new WebAppContext();
      wac.setWar("war");
      wac.setContextPath("/");
      wac.setMaxFormContentSize(5242880);
      wac.setMaxFormKeys(100000);
      wac.setClassLoader(Thread.currentThread().getContextClassLoader());
      server.setHandler(wac);
      server.setStopAtShutdown(true);
      server.start();
   }
}

<Loggers>我放

下的log4j2.xml文件中
<Logger name="org.eclipse.jetty" level="debug" />

它对我很有用,并且避免了所有记录到StdErr

的内容