简体   繁体   中英

How to enable embedded tomcat logging

I m using embedded tomcat in my java application. below is my source code. however tomcat is not generating any log.

        embedded = new Embedded();
        embedded.setDebug(3);

        org.apache.catalina.logger.FileLogger embeddedFileLogger = new org.apache.catalina.logger.FileLogger();         
        embeddedFileLogger.setDirectory(tomcatHome+"/log");
        embeddedFileLogger.setPrefix("Embedded_log_");
        embeddedFileLogger.setSuffix(".txt");
        embeddedFileLogger.setTimestamp(true);
        embeddedFileLogger.setVerbosity(3);



        //embedded.setLogger(new SystemOutLogger());
        engine = embedded.createEngine();
        //engine.setLogger(embeddedFileLogger);
        embeddedFileLogger.setContainer(engine);
        engine.setDefaultHost("localhost");

        host = embedded.createHost("localhost", tomcatHome + "/webapps");
        //host.setLogger(embeddedFileLogger);
        engine.addChild(host);

        _context = embedded.createContext("", tomcatHome + "/webapps/ROOT");
        host.addChild(_context);

        embedded.addEngine(engine);

        CoyoteConnector connector = (CoyoteConnector)embedded.createConnector(InetAddress.getByName(ipAddress), port, false);               
                   embedded.addConnector(connector);
        embedded.setLogger(embeddedFileLogger);
        embedded.start();

Please let me know how can i enable embedded tomcat logging through code or tomcat configuration.

By default the embedded Tomcat uses the logging configuration provided by the JDK. If you haven't changed the configuration only a ConsoleHandler is configured. If you wanted to programmatically add a FileHandler you can add it to the root logger. Here's an example that writes to the file catalina.out by appending the messages on INFO level. This works for Tomcat 6.x and 7.x.

Logger logger = Logger.getLogger("");
Handler fileHandler = new FileHandler("catalina.out", true);
fileHandler.setFormatter(new SimpleFormatter());
fileHandler.setLevel(Level.INFO);
fileHandler.setEncoding("UTF-8");
logger.addHandler(fileHandler);

Here is what worked for me (Tomcat 9.0.38):

Add system property to point to my logging config:

-Djava.util.logging.config.file=/absolute/path/to/logging.properties

And the contents of logging.properties is:

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter
java.util.logging.ConsoleHandler.encoding = UTF-8
handlers = java.util.logging.ConsoleHandler
org.apache.coyote.http2.level = FINE

For me this worked only partially. This requires adding one more line:

java.util.logging.Logger logger = java.util.logging.Logger.getLogger("").setLevel(Level.ALL);

I wanted to show ALL logs and make logging level configurable. Here is what worked for me:

    public static void main(String[] args) throws Exception {
    // Tuning log level
    if (args.length > 0) {
        Level level = Level.parse(args[0]);
        java.util.logging.Logger logger = java.util.logging.Logger.getLogger("");
        logger.setLevel(level);
        Handler[] handlers = logger.getHandlers();
        Handler handler;
        if (handlers.length == 1 && handlers[0] instanceof ConsoleHandler) {
            handler = handlers[0];
        } else {
            handler = new ConsoleHandler();
        }
        handler.setFormatter(new SimpleFormatter());
        handler.setLevel(level);
        handler.setEncoding("UTF-8");
        logger.addHandler(handler);
    }
    //... some code here
}

You need to include the tomcat-embed-logging-juli-8.0.15.jar in your classpath.

I used this code to load the logging.properties file and configure the logging externally using the normal java logging approach:

//Loads custom logging configuration located in a properties file named logging.properties
try (InputStream inputStream = new FileInputStream("logging.properties")) {
    LogManager.getLogManager().readConfiguration(inputStream);
    System.out.println("Custom logging configuraton loaded successfully");
} catch (Exception exception) {
    System.err.println ("Custom logging configuration don't found using default system logging configuration.");
}

For more details about configure the logging.properties use this pages: Tomcat logging , org.apache.juli.FileHandler

Also you can use the java.util.logging.FileHandler that is also compatible with the juli aproach an has another advanced configuration options: java.util.logging.FileHandler

There is another jar for use log4j but for me this works fine. Note: I used the tomcat embedded 8.0.15 but there are new versions now.

I had just to figure out, how I can control the logging of the embedded Tomcat the same way the standalone version is doing it.

You will need the tomcat-juli.jar , because it has a custom LogManager in it. This LogManager has advantages, since it is capable of registering multiple FileHandlers . So it enables you to separate Logs per WebApp.

It is not enough to just include the tomcat-juli.jar , but you also need to activate its LogManager . This would be done through a JVM-Parameter:

-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager

Now it is possible to register FileHandlers like shown in the Tomcat documentation .

With an additional JVM-Parameter you can set the path to the logging.properties -File:

-Djava.util.logging.config.file=/logs/logging.properties


It is also possible to load the .properties -File programmatically, but then you need to set a java.util.logging.config.class with a JVM-Parameter, like above. In it's constructor you then must call LogManager.readProperties(...) . Look here for more information.

Best regards

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