简体   繁体   中英

AbstractNCSARequestLog not working for log4j

I am using Java Spark(web framework) and I am following this tutorial: http://sparkjava.com/tutorials/jetty-request-log in order to enable logging.

The following class however, gives me an error:

constructor AbstractNCSARequestLog in class AbstractNCSARequestLog cannot be applied to given types;
                return new AbstractNCSARequestLog() {
                       ^
  required: Writer
  found: no arguments

The class in question:

    public class RequestLogFactory {

    private Logger logger;

    public RequestLogFactory(Logger logger) {
        this.logger = logger;
    }

    AbstractNCSARequestLog create() {
        return new AbstractNCSARequestLog() {
            @Override
            protected boolean isEnabled() {
                return true;
            }

            @Override
            public void write(String s) throws IOException {
                logger.info(s);
            }
        };
    }
}

If you want to use the old-school NCSA Request Log format, then use the Slf4jRequestLog instead of the AbstractNCSARequestLog (both are now deprecated).

If you want to use the modern RequestLog techniques use the CustomRequestLog with a Slf4jRequestLogWriter . (added bonus is the ability to customize the requestlog format)

With either of those approaches you'll need to route your slf4j to log4j (by including the slf4j-log4j12-<ver>.jar in your classpath)

Then you can now capture your request logs on a named logger (logger name is configurable in the Slf4jRequestLogWriter )

Example:

Slf4jRequestLogWriter slf4jWriter = new Slf4jRequestLogWriter();
// The name of the logger to write request logs to
slf4jWriter.setLoggerName("my.requestlog");
CustomRequestLog requestLog = new CustomRequestLog(slf4jWriter, CustomRequestLog.NCSA_FORMAT);
server.setRequestLog(requestLog);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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