log4j2 + slf4j
1. slf4j 介绍
slf4j 是一个日志的同一接口,只有接口,实现可以使 log4j,logback等
slf4j 详细介绍参考: https://www.cnblogs.com/hafiz/p/5486858.html
2.配置
jar包依赖
<!-- log4j2的核心包 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.11.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.11.1</version> </dependency> <!-- 用于slf4j 和 log4j2 保持桥接 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.11.1</version> <scope>test</scope> </dependency> <!-- slf4j 的api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency>
log4j2.xml配置:使用之前的log4j2.xml配置即可
3.使用代码
注意以下导入的包是 slf4j ,而不是 log4j 的包
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Test1 { public static Logger logger=LoggerFactory.getLogger(Test1.class); public static void main(String[] args) { logger.trace("trace11111111111"); logger.debug("debug11111111"); logger.info("ino11111111111"); logger.warn("dddddddddddddddddd"); logger.error("eeeeeeeeeeeeeeeee"); try { int a=1/0; } catch (Exception e) { logger.error(e.getMessage(),e.fillInStackTrace()); } } }