“SLF4J 入门”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
第11行: | 第11行: | ||
# 日志框架的绑定 | # 日志框架的绑定 | ||
# 日志框架的桥接 | # 日志框架的桥接 | ||
+ | |||
+ | === 示例 === | ||
+ | |||
+ | ==== pom.xml ==== | ||
+ | <syntaxhighlight lang="xml"> | ||
+ | <?xml version="1.0" encoding="UTF-8"?> | ||
+ | <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
+ | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
+ | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
+ | <modelVersion>4.0.0</modelVersion> | ||
+ | |||
+ | <groupId>io.github.jihch</groupId> | ||
+ | <artifactId>slf4j-demo</artifactId> | ||
+ | <version>1.0-SNAPSHOT</version> | ||
+ | |||
+ | <properties> | ||
+ | <maven.compiler.source>8</maven.compiler.source> | ||
+ | <maven.compiler.target>8</maven.compiler.target> | ||
+ | </properties> | ||
+ | |||
+ | <dependencies> | ||
+ | <!-- slf4j 日志门面 --> | ||
+ | <dependency> | ||
+ | <groupId>org.slf4j</groupId> | ||
+ | <artifactId>slf4j-api</artifactId> | ||
+ | <version>1.7.27</version> | ||
+ | </dependency> | ||
+ | |||
+ | <!-- slf4j 内置的简单实现 --> | ||
+ | <dependency> | ||
+ | <groupId>org.slf4j</groupId> | ||
+ | <artifactId>slf4j-simple</artifactId> | ||
+ | <version>1.7.21</version> | ||
+ | </dependency> | ||
+ | |||
+ | <dependency> | ||
+ | <groupId>junit</groupId> | ||
+ | <artifactId>junit</artifactId> | ||
+ | <version>4.12</version> | ||
+ | </dependency> | ||
+ | </dependencies> | ||
+ | |||
+ | </project> | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== SLF4JTest.java ==== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | import org.junit.Test; | ||
+ | import org.slf4j.Logger; | ||
+ | import org.slf4j.LoggerFactory; | ||
+ | |||
+ | public class SLF4JTest { | ||
+ | |||
+ | public static final Logger LOGGER = LoggerFactory.getLogger(SLF4JTest.class); | ||
+ | |||
+ | @Test | ||
+ | public void test() { | ||
+ | LOGGER.error("error"); | ||
+ | LOGGER.warn("warn"); | ||
+ | LOGGER.info("info"); | ||
+ | LOGGER.debug("debug"); | ||
+ | LOGGER.trace("trace"); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | [main] ERROR io.github.jihch.SLF4JTest - error | ||
+ | [main] WARN io.github.jihch.SLF4JTest - warn | ||
+ | [main] INFO io.github.jihch.SLF4JTest - info | ||
+ | </syntaxhighlight>需要注意的是区分引入的 Logger 类是 org.slf4j 包下的,不是 log4j 里 org.apache.log4j 包下的; | ||
+ | |||
+ | 然后 SLF4J 里没有日志级别 fatal; | ||
+ | |||
+ | 从控制台的输出能看出来 SLF4J 中默认日志级别也是 INFO |
2023年2月26日 (日) 10:51的版本
https://www.bilibili.com/video/BV1iJ411H74S?p=22
简单日志门面(Simple Logging Facade For Java)SLF4J 主要是为了给 Java 日志访问提供一套标准、规范的 API 框架,其主要意义在于提供接口,具体的实现可以交由其他日志框架,
例如 log4j 和 logback 等。当然 slf4j 自己也提供了功能较为简单的实现,但是一般很少用到。对于一般的 Java 项目而言,日志框架会选择 slf4j-api 作为门面,配上具体的实现框架(log4j、logback等),
中间使用桥接器完成桥接。
SLF4J 是目前市面上最流行的日志门面。现在的项目中,基本上都是使用 SLF4J 作为我们的日志系统。SLF4J 日志门面主要提供两大功能:
- 日志框架的绑定
- 日志框架的桥接
示例
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.jihch</groupId>
<artifactId>slf4j-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- slf4j 日志门面 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.27</version>
</dependency>
<!-- slf4j 内置的简单实现 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
SLF4JTest.java
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SLF4JTest {
public static final Logger LOGGER = LoggerFactory.getLogger(SLF4JTest.class);
@Test
public void test() {
LOGGER.error("error");
LOGGER.warn("warn");
LOGGER.info("info");
LOGGER.debug("debug");
LOGGER.trace("trace");
}
}
[main] ERROR io.github.jihch.SLF4JTest - error
[main] WARN io.github.jihch.SLF4JTest - warn
[main] INFO io.github.jihch.SLF4JTest - info
需要注意的是区分引入的 Logger 类是 org.slf4j 包下的,不是 log4j 里 org.apache.log4j 包下的;
然后 SLF4J 里没有日志级别 fatal;
从控制台的输出能看出来 SLF4J 中默认日志级别也是 INFO