slf4j logback log4j的关系

一般getLogger有两种:

1)slf4j的LoggerFactory.getLogger

2) log4j的Logger.getLogger

第一种默认是用logback和logback.xml,如果没有logback的包,他也可以去找log4j。

第二种就是默认用log4j的了。

至于怎么配置slf4j来使用不同的实现,看

https://dzone.com/articles/how-configure-slf4j-different

 

Using slf4j with Simple logger

Create a Maven based project and this in your pom.xml.

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

Now you may use Logger in your Java code like this.

package deng;
import org.slf4j.*;
public class Hello {
static Logger LOGGER = LoggerFactory.getLogger(Hello.class);
public static void main(String[] args) {
for (int i = 0; i < 10; i++)
if (i % 2 == 0)
LOGGER.info("Hello {}", i);
else
LOGGER.debug("I am on index {}", i);
}
}

The above will get your program compiled, but when you run it, you will see these output.

bash> java deng.Hello
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.

What it’s saying is that at runtime, you are missing the logging "implementation" (or the logger binding), so slf4jsimply use a "NOP" implmentation, which does nothing. In order to see the output properly, you may try use ansimple implementation that does not require any configuration at all! Just go back to your pom.xml and add the following:

 <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
</dependency>

Now you see logging output on STDOUT with INFO level. This simple logger will default show any INFO level message or higher. In order to see DEBUG messages, you would need to pass in this System Property -Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG at your Java startup.

Using slf4j with Log4j logger

Now we can experiment and swap different logger implementations, but your application code can remain the same. All we need is ot replace slf4j-simple with another popular logger implementation, such as the Log4j.

 <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>

Again, we must configure logging per implementation that we picked. In this case, we need ansrc/main/resources/log4j.properties file.

 log4j.rootLogger=DEBUG, STDOUT
log4j.logger.deng=INFO
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

Re-run your program, and you should see similar output.

Using slf4j with JDK logger

The JDK actually comes with a logger package, and you can replace pom.xml with this logger implementation.

 <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.5</version>
</dependency>

Now the configuration for JDK logging is a bit difficult to work with. Not only need a config file, such assrc/main/resources/logging.properties, but you would also need to add a System properties -Djava.util.logging.config.file=logging.properties in order to have it pick it up. Here is an example to get you started:

level=INFO

handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=FINEST
deng.level=FINEST

Using slf4j with Logback logger

The logback logger implementation is a super dupa quality implementation. If you intend to write serious code that go into production, you may want to evaluate this option. Again modify your pom.xml to replace with this:

 <dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>

Here is a sample of configuration src/main/resources/logback.xml to get things started.

<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="deng" level="DEBUG"/>
 <root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SLF4Jlog4jlogback是Java的三个不同的日志框架。SLF4J是Java的一个日志门面,它提供了一些通用的API,可以与不同的具体日志框架集成使用log4j是一个具体的日志框架,它提供了丰富的功能和配置选项。logback则是由log4j的作者设计完成的一个日志框架,它拥有更好的特性,并且是SLF4J的原生实现。 区别如下: 1. SLF4J是一个日志门面,它只提供了一些通用的API,而不是具体的实现。它的作用是为了让开发人员可以在不同的日志框架之间进行切换和集成,而不需要修改代码。 2. log4j是一个具体的日志框架,它提供了丰富的功能和配置选项。log4j可以与SLF4J结合使用,需要提供一些对应的jar包。 3. logback是由log4j的作者设计完成的日志框架,它是SLF4J的原生实现。logback拥有更好的特性,并且可以完整地实现SLF4J的API。logback包括了三个模块:logback-core、logback-classic和logback-access,分别用于提供基础功能、改良版本以及与Servlet容器集成。 因此,SLF4J提供了通用的日志接口,log4j是其中一个具体的实现,而logback则是log4j的改良版本,同时也是SLF4J的原生实现。根据具体需求和偏好,开发人员可以选择使用其中的任意一个日志框架。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Java日志框架SLF4Jlog4j以及logback的联系和区别](https://blog.csdn.net/weixin_30241919/article/details/101487496)[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: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值