“Logback 入门”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
(未显示同一用户的1个中间版本) | |||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1iJ411H74S?p=26 | https://www.bilibili.com/video/BV1iJ411H74S?p=26 | ||
+ | === 简介 === | ||
Logback 是由 Log4j 创始人设计的另一个开源日志组件,性能比 Log4j 好。 | Logback 是由 Log4j 创始人设计的另一个开源日志组件,性能比 Log4j 好。 | ||
官方网站:https://logback.qos.ch/ | 官方网站:https://logback.qos.ch/ | ||
− | |||
Logback 主要分为三个模块: | Logback 主要分为三个模块: | ||
第13行: | 第13行: | ||
后续的日志代码都是通过 SLF4J 日志门面搭建日志系统,所以代码上没有区别,主要是修改配置文件和 pom.xml 依赖 | 后续的日志代码都是通过 SLF4J 日志门面搭建日志系统,所以代码上没有区别,主要是修改配置文件和 pom.xml 依赖 | ||
+ | |||
+ | === 示例 === | ||
+ | |||
+ | ==== 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>logback-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> | ||
+ | |||
+ | <!-- Logback 日志实现 --> | ||
+ | <dependency> | ||
+ | <groupId>ch.qos.logback</groupId> | ||
+ | <artifactId>logback-classic</artifactId> | ||
+ | <version>1.2.3</version> | ||
+ | </dependency> | ||
+ | |||
+ | <!-- JUnit 单元测试 --> | ||
+ | <dependency> | ||
+ | <groupId>junit</groupId> | ||
+ | <artifactId>junit</artifactId> | ||
+ | <version>4.12</version> | ||
+ | </dependency> | ||
+ | </dependencies> | ||
+ | |||
+ | </project> | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== LogbackTest.java ==== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | import org.junit.Test; | ||
+ | import org.slf4j.Logger; | ||
+ | import org.slf4j.LoggerFactory; | ||
+ | |||
+ | public class LogbackTest { | ||
+ | |||
+ | public static final Logger LOGGER = LoggerFactory.getLogger(LogbackTest.class); | ||
+ | |||
+ | // 快速入门 | ||
+ | @Test | ||
+ | public void test() { | ||
+ | // 日志输出 | ||
+ | LOGGER.error("error"); | ||
+ | LOGGER.warn("warn"); | ||
+ | LOGGER.info("info"); | ||
+ | LOGGER.debug("debug"); | ||
+ | LOGGER.trace("trace"); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | 10:56:30.412 [main] ERROR io.github.jihch.LogbackTest - error | ||
+ | 10:56:30.414 [main] WARN io.github.jihch.LogbackTest - warn | ||
+ | 10:56:30.414 [main] INFO io.github.jihch.LogbackTest - info | ||
+ | 10:56:30.414 [main] DEBUG io.github.jihch.LogbackTest - debug | ||
+ | </syntaxhighlight> |
2023年2月27日 (一) 02:58的最新版本
https://www.bilibili.com/video/BV1iJ411H74S?p=26
简介
Logback 是由 Log4j 创始人设计的另一个开源日志组件,性能比 Log4j 好。
Logback 主要分为三个模块:
- logback-core:作为其它两个模块的基础模块的核心模块
- logback-classic:它是 Log4j 的一个改良版本,同时它完整实现了 SLF4J API
- logback-access:访问模块与 Servlet 容器集成提供通过 HTTP 来访问日志的功能
后续的日志代码都是通过 SLF4J 日志门面搭建日志系统,所以代码上没有区别,主要是修改配置文件和 pom.xml 依赖
示例
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>logback-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>
<!-- Logback 日志实现 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- JUnit 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
LogbackTest.java
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogbackTest {
public static final Logger LOGGER = LoggerFactory.getLogger(LogbackTest.class);
// 快速入门
@Test
public void test() {
// 日志输出
LOGGER.error("error");
LOGGER.warn("warn");
LOGGER.info("info");
LOGGER.debug("debug");
LOGGER.trace("trace");
}
}
10:56:30.412 [main] ERROR io.github.jihch.LogbackTest - error
10:56:30.414 [main] WARN io.github.jihch.LogbackTest - warn
10:56:30.414 [main] INFO io.github.jihch.LogbackTest - info
10:56:30.414 [main] DEBUG io.github.jihch.LogbackTest - debug