Java日志 SLF4J使用

SLF4J

​ Simple Logging Facade for Java (SLF4J), SLF4J是各种日志记录框架(例如java.util.logging,logback,log4j)的抽象,在使用 时需绑定所需的日志框架。

需引入以下依赖,仅包含slf4j-api-1.7.30.jar

  <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
       <version>1.7.30</version>
  </dependency>

Hello World

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");
  }
}

如果不想写 Logger logger = LoggerFactory.getLogger(HelloWorld.class); 这句,可以在类上增加@SLF4j注解,然后log.**

引入slf4j-api-1.7.30.jar后执行上面的将报错

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

与日志框架绑定

选择下面其中一个绑定。

slf4j-log4j12-1.7.28.jar

Binding for log4j version 1.2, a widely used logging framework. You also need to place log4j.jar on your class path.

slf4j-jdk14-1.7.28.jar

​ Binding for java.util.logging, also referred to as JDK 1.4 logging

slf4j-nop-1.7.28.jar

​ Binding for NOP, silently discarding all logging.

slf4j-simple-1.7.28.jar

​ Binding for Simple implementation, which outputs all events to System.err. Only messages of level INFO and higher are printed. This binding may be useful in the context of small applications.

slf4j-jcl-1.7.28.jar

​ Binding for Jakarta Commons Logging. This binding will delegate all SLF4J logging to JCL.

*logback-classic-1.2.3.jar *

​ (requires logback-core-1.2.3.jar)*

遇到的错误

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/usr/local/******-1.0.0.jar!/BOOT-INF/lib/logback-classic-1.2.3.jar!/org/s
lf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/usr/local/******-1.0.0.jar!/BOOT-INF/lib/log4j-slf4j-impl-2.13.3.jar!/org
/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/usr/local/******-1.0.0.jar!/BOOT-INF/lib/slf4j-log4j12-1.7.30.jar!/org/sl
f4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
Failed to instantiate SLF4J LoggerFactory
Reported exception:
java.lang.NoClassDefFoundError: ch/qos/logback/core/joran/spi/JoranException
        at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)

解决办法:

1.通过mvn dependency:tree 查看依赖树

2.只用logback的,其他的在pom.xml排除掉

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java项目中,使用@Slf4j注解可以方便地引入SLF4J和Log4j日志工具。使用@Slf4j注解的类会自动生成一个名为"log"的静态日志对象,可以直接使用该对象进行日志记录操作。为了使用@Slf4j注解,需要首先在项目的pom.xml文件中添加相应的依赖。在maven项目中,可以通过在pom.xml文件的<dependencies>标签内添加以下代码来引入依赖: <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.30</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.30</version> </dependency> 接下来,在需要使用@Slf4j注解的类上添加@Slf4j注解,然后就可以在该类中使用log对象进行日志记录了。例如,可以使用log.info("message")来输出一条信息级别的日志。需要注意的是,使用@Slf4j注解之前,还需要在项目中配置log4j.properties文件来指定日志的输出格式和目标。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [纯java项目添加slf4j+log4j日志工具](https://blog.csdn.net/shiyibodec/article/details/84333450)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [slf4j-1.7.rar](https://download.csdn.net/download/zhoukangshou/11720416)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [使用 SLF4J 进行高效的日志记录(@slf4j 注解)](https://blog.csdn.net/run65536/article/details/130602830)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值