如何配置jetty使用log4j?我已经在我的应用程序中使用log4j,而jetty登录到stderr ...
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
public class Test {
static final Logger logger = Logger.getLogger(Test.class);
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
logger.info("Started.");
Server server = new Server();
Connector connector = new SelectChannelConnector();
connector.setHost("127.0.0.1");
connector.setPort(8080);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addServlet(new ServletHolder(new ServletHome()), "/");
// disable jettys default logger
//org.eclipse.jetty.util.log.Log.setLog(null);
server.addConnector(connector);
server.setHandler(context);
logger.debug("Starting jetty.");
try {
server.start();
server.join();
} catch (Exception e) {
logger.error("Ooops.");
}
logger.info("Finished.");
}
}
答案 0 :(得分:9)
Jetty使用slf4j for logging。 slf4j允许统一日志记录API连接到任何支持的日志记录框架。当然也支持log4j。您所要做的就是将slf4j API和slf4j-log4j连接器都放到类路径中。它应该自动连接。