Embedded Tomcat logging over logback / sl4j
Asked Answered
F

2

6

How can I make an embedded tomcat write its logs over logback? I found some info about using a standalone tomcat with log4j. But how does the setup look like for an embedded tomcat and logback?

These are the maven dependencies:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>${logback.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <version>${tomcat.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-logging-juli</artifactId>
    <version>${tomcat.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-logging-log4j</artifactId>
    <version>${tomcat.version}</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
    <version>${sl4j.version}</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jul-to-slf4j</artifactId>
    <version>${sl4j.version}</version>
</dependency>

I know that Spring Boot does the tomcat logging integration automatically. But in this case I cannot use Spring.

Franco answered 22/12, 2016 at 10:56 Comment(0)
O
1

Tomcat 8 has a ServiceLoader based discovery mechanism that lets you deploy your own logger implementation.

Simply implement org.apache.juli.logging.Log in a class that delegates to Logback/SLF4J and put the class name into the META-INF/services/org.apache.juli.logging.Log file on your class path. Then Tomcat will log via your class.

import org.apache.juli.logging.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class DelegateToSlf4jLogger implements Log {
    private final Logger logger;

    // constructor required by ServiceLoader
    public DelegateToSlf4jLogger() {
        logger = null;
    }

    public DelegateToSlf4jLogger(String name){
        logger = LoggerFactory.getLogger(name);
    }

    @Override
    public boolean isDebugEnabled() {
        return logger.isDebugEnabled();
    }

    @Override
    public boolean isErrorEnabled() {
        return logger.isErrorEnabled();
    }

    @Override
    public boolean isFatalEnabled() {
        return logger.isErrorEnabled();
    }

    @Override
    public boolean isInfoEnabled() {
        return logger.isInfoEnabled();
    }

    @Override
    public boolean isTraceEnabled() {
        return logger.isTraceEnabled();
    }

    @Override
    public boolean isWarnEnabled() {
        return logger.isWarnEnabled();
    }

    @Override
    public void trace(Object message) {
        logger.debug(String.valueOf(message));
    }

    @Override
    public void trace(Object message, Throwable t) {
        logger.debug(String.valueOf(message), t);
    }

    @Override
    public void debug(Object message) {
        logger.debug(String.valueOf(message));
    }

    @Override
    public void debug(Object message, Throwable t) {
        logger.debug(String.valueOf(message), t);
    }

    @Override
    public void info(Object message) {
        logger.info(String.valueOf(message));
    }

    @Override
    public void info(Object message, Throwable t) {
        logger.info(String.valueOf(message), t);
    }

    @Override
    public void warn(Object message) {
        logger.warn(String.valueOf(message));
    }

    @Override
    public void warn(Object message, Throwable t) {
        logger.warn(String.valueOf(message), t);
    }

    @Override
    public void error(Object message) {
        logger.error(String.valueOf(message));
    }

    @Override
    public void error(Object message, Throwable t) {
        logger.error(String.valueOf(message), t);
    }

    @Override
    public void fatal(Object message) {
        logger.error(String.valueOf(message));
    }

    @Override
    public void fatal(Object message, Throwable t) {
        logger.error(String.valueOf(message), t);
    }
}
Ottavia answered 13/3, 2018 at 22:25 Comment(1)
This solution almost worked for me, but the service loader was not used. I needed to place a commons-logging.properties file under the classpath, with content org.apache.juli.logging.Log=util.DelegateToSlf4jLoggerBoardinghouse
D
1

Try to add the code to install the bridge:

SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();

And remove dependency:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-logging-log4j</artifactId>
    <version>${tomcat.version}</version>
</dependency>
Darnley answered 25/2, 2019 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.