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

How to get from commons-logging to slf4j

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

Share

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

Today, I would like to talk to you about how to go from commons-logging to slf4j. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.

In order to make the log implementation of the system configurable, java's heroes have implemented two underlying interface specifications, 1.commons-logging 2.slf4j

1. Commons-logging this is an Apache project with a long history and based on classloader. Including spring are using this log underlying interface specification.

Implementation principle: I define a LogFactory, this LogFactory can produce Log

As to which LogFactory to use depends on the configuration parameters of the system, so it can produce Log from different manufacturers.

For example:

Commongs-logging created LogFactory in this order.

a. First of all, determine whether there are settings in the jvm configuration parameters.

b. And then check the META-INF/services/org.apache.commons.logging.LogFactory file.

c. If you can't find it, find the configuration in the user's commons-logging.properties.

d. Finally, if it is not found, then use his default implementation LogFactoryImpl (the default implementation of LogFactoryImpl is more interesting, it can produce Log4JLogger, Jdk14Logger, Jdk13LumberjackLogger, SimpleLog by default)

Conclusion: if you use commons-logging, you must configure a real implementation of log*.jar work

2. Slf4j, whose full name is Simple Logging Facade for JAVA,java simple log facade, similar to Apache Common-Logging, is a facade package provided by different logging frameworks, and can be connected to a log implementation solution without modifying any configuration during deployment. However, he statically binds the real Log library at compile time. When using SLF4J, if you need to use a certain logging implementation, then you must choose the correct collection of SLF4J jar packages (various bridging packages). Slf4j is an encapsulation of a log system, which provides a unified API and does not provide a specific implementation of logging.

1.slf4j basic package slf4J-api.jar

2.slf4j bridges packages of specific log implementations:

Log4j:slf4j-log4j12.jar

Logback:logback-core 、 logback- classic

3. You can also use slf4j as an implementation of commongs-logging, which requires the introduction of jcl-over-slf4j.jar (the main concern here is that the LogFactory implementation class: org.apache.commons.logging.impl.SLF4JLogFactory is configured in the META-INF/services/org.apache.commons.logging.LogFactory file)

III. Summary

1. At present, the real ones on the market are log4j and logback. All written by one person. It is said that the performance of logback is much better than that of log4j. Let's take a look at the details.

As a universal, reliable, fast and flexible logging framework, LogBack will be used as a substitute for Log4j and SLF4J to form a complete implementation of the new logging system. LogBack claims excellent performance, "the performance of certain key operations, such as determining whether to log a log statement, has been significantly improved. This operation takes 3 nanoseconds in LogBack and 30 nanoseconds in Log4J. LogBack also creates a logger (logger) faster: 13 microseconds, compared with 23 microseconds in Log4J. More importantly, it takes only 94 nanoseconds to get an existing logger, while it takes 2234 nanoseconds for Log4J, which reduces the time to 1ram 23. The performance improvement compared with JUL is also significant. In addition, all LOGBack documents are fully provided free of charge, unlike Log4J, which only provides some free documents and requires users to buy paid documents.

two。 Look at the relationship between SLF4j and the concrete implementation:

3. Use other log interfaces to convert to slf4j.

1. Commons-loggin,slf4j is only a log abstract facade, not a specific log framework.

2. Log4j,logback is a specific logging framework.

3. Generally, slf4j+logback or slf4j+log4j,commons-logging+log4j is the first choice.

After reading the above, do you have any further understanding of how to get from commons-logging to slf4j? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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