开发者

how to configure jetty to use log4j?

How do I configure jetty to use use log4j? I'm already using log4j in my application, while jetty logs to stderr...

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyC开发者_JAVA百科onfigurator;
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.");
    }
}


Jetty uses slf4j for logging. slf4j allows a unified logging API to connect to any supported logging framework. Of course log4j is supported too. All you have to do is put both the slf4j API and slf4j-log4j connector to your classpath. It should get wired automatically.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜