slf4j logback log4j的关系
2017-06-05 22:28
369 查看
一般getLogger有两种:
1)slf4j的LoggerFactory.getLogger
2) log4j的Logger.getLogger
第一种默认是用logback和logback.xml,如果没有logback的包,他也可以去找log4j。
第二种就是默认用log4j的了。
至于怎么配置slf4j来使用不同的实现,看
https://dzone.com/articles/how-configure-slf4j-different
Create a Maven based project and this in your
Now you may use
The above will get your program compiled, but when you run it, you will see these output.
What it’s saying is that at runtime, you are missing the logging "implementation" (or the logger binding), so
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
add the following:
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
Now we can experiment and swap different logger implementations, but your application code can remain the same. All we need is ot replace
another popular logger implementation, such as the Log4j.
Again, we must configure logging per implementation that we picked. In this case, we need an
Re-run your program, and you should see similar output.
The JDK actually comes with a logger package, and you can replace
Now the configuration for JDK logging is a bit difficult to work with. Not only need a config file, such as
but you would also need to add a System properties
get you started:
level=INFO
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
replace with this:
Here is a sample of configuration
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
Loggerin 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.xmland
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=DEBUGat 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-simplewith
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 an
src/main/resources/log4j.propertiesfile.
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.xmlwith 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 as
src/main/resources/logging.properties,
but you would also need to add a System properties
-Djava.util.logging.config.file=logging.propertiesin 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.xmlto
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.xmlto 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>
相关文章推荐
- slf4j log4j logback关系详解和相关用法
- slf4j和log4j、logback的关系
- slf4j log4j logback关系详解和相关用法
- slf4j log4j logback关系详解和相关用法
- slf4j log4j logback关系详解和相关用法
- Java日志管理:common-logging、slf4j、log4j、logback的关系
- slf4j log4j logback之间的关系,哪个更好些
- slf4j, log4j, logback, java.util.Logging关系
- log4j,logback,slf4j三者的关系
- slf4j、log4j、logback关系介绍
- log4j logback slf4j commons-logging java.util.logging的关系
- log4j,logback,slf4j之间的关系详解
- slf4j、logback和log4j的关系
- slf4j log4j logback关系详解和相关用法
- slf4j log4j logback关系详解和相关用法
- java日志组件逻辑关系(commons-logging,log4j,slf4j,logback)
- slf4j log4j logback关系详解和相关用法
- slf4j-api、slf4j-log4j12以及log4j之间什么关系
- log4j_logback_slf4j项目配置中的简单总结