Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to configure jetty to use log4j?

Tags:

java

log4j

jetty

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.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.");
    }
}
like image 793
phatypus Avatar asked Aug 15 '11 09:08

phatypus


People also ask

Does jetty use log4j?

Apache log4j is a popular logging library. Jetty supporst log4j via Jetty via the Slf4j facade and the Slf4j binding layer for log4j . The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks, such as java. util.

How do you get jetty logs?

By default all jetty logs are placed into jetty/base/logs directory. The main Rhythmyx log can be found at jetty/base/logs/server. log If this file gets too big >10Mb the logging engine will roll over to separate files server.


1 Answers

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.

like image 115
musiKk Avatar answered Oct 15 '22 16:10

musiKk