SLF4J log front and slf4j front

Source: Internet
Author: User

SLF4J log front and slf4j front

SLF4J Official Website: http://www.slf4j.org/

In general, SLF4J enables our project to replace different log systems at minimal cost. You do not need to modify the code. You only need to add or delete the corresponding jar package and configuration file.

1. Add slf4j-api-xxx.jar to the project.(Xxx indicates the version number. This document uses version 1.7.7)

2. Create a project and write a few simple log records.

//import org.apache.log4j.PropertyConfigurator;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class test_slf4j {public static void main(String[] args) {// PropertyConfigurator.configure("log4j.properties");Logger logger = LoggerFactory.getLogger(test_slf4j.class);logger.trace("I'm trace log.");logger.debug("I'm debug log.");logger.info("I'm info log.");logger.warn("I'm warn log.");logger.error("I'm error log.");}}

3. The downloaded SLF4J compressed package comes with many log systems to experience several common ones.

[Simple]

Use the simple log to add slf4j-simple-xxx.jar to the project.

[Main] INFO test_slf4j-I'm info log. [main] WARN test_slf4j-I'm warn log. [main] ERROR test_slf4j-I'm error log.

It seems that because there is no custom configuration file, the trace and debug logs are filtered by default.

[JDK]

Try the built-in jdk logging system again, delete slf4j-simple-xxx.jar, add slf4j-jdk14-xxx.jar to the project.
Note: The project cannot contain multiple log systems. Otherwise, SLF4J will report an error because it does not know which log system to use.

August 30 12:44:12 test_slf4j main information: I'm info log. august 30 12:44:13 test_slf4j main warning: I'm warn log. august 30 12:44:13 test_slf4j main critical: I'm error log.

The results are similar to those of simple logs. Apart from different log formats, logs are filtered out at the trace and debug levels.

[Log4j]

The preceding log Content is too simple, which has little effect on our actual development. Of course, the more detailed the log file, the better. Especially when fixing bugs, you can accurately locate which line of code is excellent.
To try the commonly used Log4j, delete other log system jar package, add "slf4j-log4j12-xxx.jar", also need to add a Log4j1.2 version of jar package, go to the Log4j official website to download. Currently, Log4j1 has stopped updating, and Log4j2 has improved significantly over 1. I started using SLF4J + Log4j2, but it never succeeded. The online information is not much, wait for a moment to study and then write an SLF4J + Log4j2 article, here I use SLF4J-1.7.7 + Log4j-1.2.17.

After adding the required jar package, you also need to add the Log4j configuration file "log4j. properties ".

# Set log levels # log4j. rootLogger = DEBUG, Console, LogFile, ErrorFile # Output the log info to the Java Console # log4j. appender. console = org. apache. log4j. leleappenderlog4j. appender. console. target = System. outlog4j. appender. console. threshold = DEBUGlog4j. appender. console. layout = org. apache. log4j. patternLayoutlog4j. appender. console. layout. conversionPattern = <%-d {yyyy-MM-dd HH: mm: ss }>< % t> % l % n <% p> % m % n # Save the log info to the log file # log4j. appender. logFile = org. apache. log4j. rollingFileAppenderlog4j. appender. logFile. file = logs/log. loglog4j. appender. logFile. append = truelog4j. appender. logFile. maxFileSize = 2MBlog4j. appender. logFile. maxBackupIndex = 100log4j. appender. logFile. threshold = INFOlog4j. appender. logFile. layout = org. apache. log4j. patternLayoutlog4j. appender. logFile. layout. conversionPattern = <%-d {yyyy-MM-dd HH: mm: ss }>< % t> % l % n <% p> % m % n # Save the error info to the error file. A file one day. # log4j. appender. errorFile = org. apache. log4j. dailyRollingFileAppenderlog4j. appender. errorFile. file = logs/error. loglog4j. appender. errorFile. append = truelog4j. appender. errorFile. threshold = ERRORlog4j. appender. errorFile. layout = org. apache. log4j. patternLayoutlog4j. appender. errorFile. layout. conversionPattern = <%-d {yyyy-MM-dd HH \: mm \: ss }>< % t> % l % n <% p> % m % n

Create "log4j. properties" in the src root directory. Log4j searches for this configuration file by default at startup. If you place the configuration file elsewhere, you can use the code commented out above to load the specified configuration file first. The absolute path and relative path are supported.
Or directly run the above Code and the console outputs the result:

<2014-08-30 13:17:03> <main> test_slf4j.main(test_slf4j.java:17) <DEBUG> I'm debug log.<2014-08-30 13:17:03> <main> test_slf4j.main(test_slf4j.java:18) <INFO> I'm info log.<2014-08-30 13:17:03> <main> test_slf4j.main(test_slf4j.java:19) <WARN> I'm warn log.<2014-08-30 13:17:03> <main> test_slf4j.main(test_slf4j.java:20) <ERROR> I'm error log.

I configured log4j. rootLogger = DEBUG, so the trace is not output. Check two log files and record the corresponding logs.


What is the relationship between log4j and slf4j in java? Which one should I use to print logs?

Basically, log4j
Is slf4j able to meet special requirements? It has not been used ..

What is the use of slf4j-log4j12-158jar?

Slf4j is the hibernate log interface, usually we use log4j. jar to realize the logging function of hibernate, The slf4j-log4j.jar can be seen as an adapter to convert the slf4j interface into a suitable interface of log4j.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.