View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.server;
20  
21  import java.io.IOException;
22  
23  import org.eclipse.jetty.util.annotation.ManagedObject;
24  import org.eclipse.jetty.util.log.Slf4jLog;
25  
26  /**
27   * Implementation of NCSARequestLog where output is sent as a SLF4J INFO Log message on the named logger "org.eclipse.jetty.server.RequestLog"
28   */
29  @ManagedObject("NCSA standard format request log to slf4j bridge")
30  public class Slf4jRequestLog extends AbstractNCSARequestLog
31  {
32      private Slf4jLog logger;
33      private String loggerName;
34  
35      public Slf4jRequestLog()
36      {
37          // Default logger name (can be set)
38          this.loggerName = "org.eclipse.jetty.server.RequestLog";
39      }
40  
41      public void setLoggerName(String loggerName)
42      {
43          this.loggerName = loggerName;
44      }
45  
46      public String getLoggerName()
47      {
48          return loggerName;
49      }
50  
51      @Override
52      protected boolean isEnabled()
53      {
54          return logger != null;
55      }
56  
57      @Override
58      public void write(String requestEntry) throws IOException
59      {
60          logger.info(requestEntry);
61      }
62  
63      @Override
64      protected synchronized void doStart() throws Exception
65      {
66          logger = new Slf4jLog(loggerName);
67          super.doStart();
68      }
69  }