日志框架
日志框架接口
- commons-logging
- log4j: 实现
- slf4j
- logback: 实现
commons-loggins
和 slf4j
是两种不一样的日志框架,定义了不同的日志接口,一般的,项目中只需要一种日志框架就行,如果混着用,可能日志冲突,小问题就是日志输出不了, 严重一点的就是项目启动不了…
commons-loggins
commons-loggings
的实现为 log4j
加入依赖
<log4j.version>2.13.1</log4j.version>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
将 log4j2.xml
文件加入类路径中(resources):
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="TRACE">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
测试:
public class Log4jTest {
private static final Logger logger = LogManager.getLogger(Log4jTest.class);
public static void main(String[] args) {
logger.trace("trace level");
logger.debug("debug level");
logger.info("info level");
logger.warn("warn level");
logger.error("error level");
logger.fatal("fatal level");
}
}
这里的Logger类是 org.apache.logging.log4j.Logger
结果:
14:49:03.098 [main] TRACE com.g.log.logfj.Log4jTest - trace level
14:49:03.103 [main] DEBUG com.g.log.logfj.Log4jTest - debug level
14:49:03.103 [main] INFO com.g.log.logfj.Log4jTest - info level
14:49:03.103 [main] WARN com.g.log.logfj.Log4jTest - warn level
14:49:03.103 [main] ERROR com.g.log.logfj.Log4jTest - error level
14:49:03.103 [main] FATAL com.g.log.logfj.Log4jTest - fatal level
slf4j
slf4j
的实现为 logback
引入依赖
<slf4j.version>1.7.30</slf4j.version>
<logback.version>1.2.3</logback.version>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
将 logback.xml 配置文件放入类路径(resources)中:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="TRACE">
<appender-ref ref="STDOUT" />
</root>
</configuration>
测试:
public class LogbackTest {
private static final Logger logger = LoggerFactory.getLogger(LogbackTest.class);
public static void main(String[] args) {
logger.trace("trace level");
logger.debug("debug level");
logger.info("info level");
logger.warn("warn level");
logger.error("error level");
}
}
这里的Logger类是 org.slf4j.Logger
结果:
14:59:45.729 [main] TRACE com.g.log.logfj.LogbackTest - trace level
14:59:45.731 [main] DEBUG com.g.log.logfj.LogbackTest - debug level
14:59:45.731 [main] INFO com.g.log.logfj.LogbackTest - info level
14:59:45.732 [main] WARN com.g.log.logfj.LogbackTest - warn level
14:59:45.732 [main] ERROR com.g.log.logfj.LogbackTest - error level