如何让嵌入式tomcat通过logback写入日志?我找到了一些关于使用log4j的独立tomcat的信息。但是嵌入式tomcat和logback的设置如何?
这些是maven依赖项:
<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>
我知道Spring Boot会自动执行tomcat日志记录集成。但在这种情况下我不能使用Spring。
答案 0 :(得分:0)
Tomcat 8基于ServiceLoader
discovery mechanism,可让您部署自己的记录器实现。
只需在委派给Logback / SLF4J的类中实现org.apache.juli.logging.Log
,并将类名放入类路径上的META-INF/services/org.apache.juli.logging.Log
文件中。然后Tomcat将通过你的班级登录。
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);
}
}
答案 1 :(得分:0)
尝试添加代码以安装网桥:
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
并删除依赖项:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-log4j</artifactId>
<version>${tomcat.version}</version>
</dependency>