Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the detailed explanation of slf4j log4j logback relation and its related usage

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people are at a loss about the detailed explanation and related usage of slf4j log4j logback. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

I've been writing java for some time, and I've always been in the habit of using slf4j log4j to output logs. But always hold the "borrowlism" attitude, copy and paste the configuration file began to code, so this period of time to take a detailed look at the log library.

What is the relationship between slf4j log4j logback and The Simple Logging Facade for Java?

Generally speaking, slf4j is a series of logging interfaces, while log4j logback is a specific implementation of the logging framework. Next, let's follow the official documents to take a detailed look at their relationship.

The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks, such as java.util.logging, logback and log4j. SLF4J allows the end-user to plug in the desired logging framework at deployment time. Note that SLF4J-enabling your library/application implies the addition of only a single mandatory dependency, namely slf4j-api-1.7.21.jar.

The relationship between the three has been clearly described in this paragraph of the official document. Slf4j is translated as a simple log facade, which is an abstraction of the logging framework. Log4j and logback are several of the many logging frameworks.

Here are a few simple lines of code to verify.

Public class Program {public static void main (String [] args) {Logger logger = LoggerFactory.getLogger (Program.class); logger.info ("hello world");}}

As you can see from the running results, the log cannot be output in the console because no specific logger implementation is given. In other words, in the specific development, we need to bind a logging framework in order to use slf4j normally.

What about log4j and logback?

Log4j and logback are two popular logging frameworks. But there are differences between the two.

Log4j is an open source logging component implemented by apache. (Wrapped implementations)

Logback, also designed by the authors of log4j, has better features to replace a logging framework of log4j. Is a native implementation of slf4j. (Native implementations)

enter image description here

The above figure shows the application's call relationship to the logging framework. The application calls slf4j api, and the output of the log is ultimately implemented by the underlying logging framework. This picture also shows the difference between log4j and logback.

Official document description of logback

NATIVE IMPLEMENTATION There are also SLF4J bindings external to the SLF4J project, e.g. Logback which implements SLF4J natively. Logback's ch.qos.logback.classic.Logger class is a direct implementation of SLF4J's org.slf4j.Logger interface. Thus, using SLF4J in conjunction with logback involves strictly zero memory and computational overhead.

You can see that logback directly implements the interface of slf4j without consuming memory and computing overhead. Log4j is not a native implementation of slf4j, so slf4j api needs an adaptation layer when calling log4j.

Summary:

Slf4j is a logging facade of java, which implements the logging framework. Some general api,log4j and logback are specific logging frameworks.

They can be used alone or tied to slf4j.

Use it alone. Call the framework's own methods to output log information respectively.

Bind slf4j to be used together. Call the api of slf4j to enter log information, which is independent of the underlying log framework (requires the configuration file of the underlying framework)

Obviously we do not recommend using the logging framework alone here. Suppose log4j is already used in the project, and we load a class library that relies on another logging framework. At this point, we need to maintain two logging frameworks, which is a very troublesome thing. The use of slf4j is different, because the application calls the abstract layer of api, has nothing to do with the underlying logging framework, so you can change the logging framework at will.

Advantages of using slf4j to bind a logging system

The perspective of software engineering. Abstract, decoupled, easy to maintain. You can refer to the above example.

From the perspective of grammar design. Slf4j has {} placeholders, while log4j needs to concatenate strings with "+", which is not easy to read and consumes memory (heap memory).

For a detailed description, please see: http://www.importnew.com/7450.html

Log4j and logback

As a developer of log4j, he must be no stranger to log4j, which is an open source logging framework of apache. Logback is a little newer than log4j, designed and implemented by the authors of log4j, and the first version was released in 2011. Logback has relatively many improvements compared to log4j in terms of design and implementation. But there is little difference in usage between the two. Here are the advantages of logback:

Faster execution

Adequate testing

Logback-classic implements SLF4J very naturally.

Rich extended documentation

You can use XML configuration files or Groovy

Automatically reload the configuration file

Gracefully recover from the IWeiO error

Automatically clear old log archives

Automatically compress archive log files

You can refer to the official documentation for more advantages. Chinese version and English version

Above, from a performance perspective, you can migrate from log4j to logback as soon as possible.

The usage of slf4j binding log4j

As log4j is still used a lot, so introduce its basic usage.

Ide related Settings

Here we use IntelliJ IDEA2016.1 and maven.

Add related dependencies to pom.xml.

Org.slf4j slf4j-log4j12 1.7.21

Three jar packages are automatically added.

Configuration file

The normal operation of log4j requires a configuration file, and the configuration file type: log4j configuration file can be log4j.xml or log4j.properties. It needs to be configured with root, appender, layout, and so on.

Although most tutorials on the Internet are configured with log4j.properties (key-value pairs), I personally think that with xml configuration, the nodes are clearer, and there are code hints under idea, which can reduce the probability of configuration errors. I will not talk about the configuration file in detail, but will only mention a few key points.

Root logger and an appender must be configured.

Log output level, from high to low

FATAL ERROR WARN INFO DEBUG

The configuration file is given below.

Console output log configuration file (replication can be used directly)

For more information on the configuration file, please see: http://zengxiantao.iteye.com/blog/1881700

Application call / / class name. ClassLogger logger = LoggerFactory.getLogger (Program.class); / / output string logger.debug ("this is a debug msg"); / / placeholder logger.debug ("hi,welcome {}, today is {}", "admin", "Sunday"); usage of slf4j binding logback pom.xml adds dependency ch.qos.logback logback-classic 1.1.7

A lot of online tutorials have been added, but as long as you add this one, other dependencies on jar will be downloaded.

Configuration file

The configuration file is almost the same as log4j, as follows. Just choose the appender you need.

%-4relative [% thread]%-5level% logger {35} -% msg%n testFile-$ {bySecond} .log true%-4relative [% thread]%-5level% logger {35} -% msg%n logFile.log logFile.%d {yyyy-MM-dd} .log 30 3GB%-4relative [% thread]%-5level% logger {35} -% msg%n

For more information on configuration files, please refer to http://logback.qos.ch/manual/index.html

Although it is in English, it is very clear.

Program call

Api, which is also slf4j, has the same code.

/ / classLogger logger = LoggerFactory.getLogger (Program.class); / / output string logger.debug ("this is a debug msg"); / / placeholder logger.debug ("hi,welcome {}, today is {}", "admin", "Sunday"); after reading the above, have you mastered the detailed explanation of slf4j log4j logback relations and related usage? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report