java 如何配置码头使用 log4j?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7063641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:29:53  来源:igfitidea点击:

how to configure jetty to use log4j?

javalog4jjetty

提问by phatypus

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

如何配置 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.");
    }
}

回答by musiKk

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.

Jetty 使用slf4j 进行日志记录。slf4j 允许统一的日志 API 连接到任何支持的日志框架。当然也支持 log4j。您所要做的就是将 slf4j API 和 slf4j-log4j 连接器都放到您的类路径中。它应该自动接线。