查看“SLF4J 入门”的源代码
←
SLF4J 入门
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
https://www.bilibili.com/video/BV1iJ411H74S?p=22 简单日志门面(Simple Logging Facade For Java)SLF4J 主要是为了给 Java 日志访问提供一套标准、规范的 API 框架,其主要意义在于提供接口,具体的实现可以交由其他日志框架, 例如 log4j 和 logback 等。当然 slf4j 自己也提供了功能较为简单的实现,但是一般很少用到。对于一般的 Java 项目而言,日志框架会选择 '''<big>slf4j-api</big>''' 作为门面,配上具体的实现框架(log4j、logback等), 中间使用桥接器完成桥接。 SLF4J 是目前市面上最流行的日志门面。现在的项目中,基本上都是使用 SLF4J 作为我们的日志系统。SLF4J 日志门面主要提供两大功能: # 日志框架的绑定 # 日志框架的桥接 === 入门示例 === ==== 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 === 占位符示例 === ==== 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"); // 使用占位符输出日志信息 String name = "jihch"; Integer age = 14; LOGGER.info("用户:{},{}", name, age); } } </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 [main] INFO io.github.jihch.SLF4JTest - 用户:jihch,14 </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"); // 使用占位符输出日志信息 String name = "jihch"; Integer age = 14; LOGGER.info("用户:{},{}", name, age); // 将系统的异常信息输出 try { int i = 1/0; } catch (Exception e) { // e.printStackTrace(); LOGGER.error("出现异常:", e); } } } </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 [main] INFO io.github.jihch.SLF4JTest - 用户:jihch,14 [main] ERROR io.github.jihch.SLF4JTest - 出现异常: java.lang.ArithmeticException: / by zero at io.github.jihch.SLF4JTest.test(SLF4JTest.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54) Process finished with exit code 0 </syntaxhighlight>
返回至
SLF4J 入门
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息