Spring MVC Integration Slf4j-logback

Source: Internet
Author: User

1. Spring MVC Integration slf4j-log4j

There are a lot of articles on the Internet for reference on SLF4J and log4j, but the logback, especially the spring MVC integration Logback, are relatively small, and some of them have such problems. Before getting to the point, let's briefly introduce the Spring MVC integration slf4j-log4j process, as follows:

1) Add slf4j-log4j dependencies to the Pom.xml file, and three new jar packages will be added to the completed classpath, respectively: Slf4j-api.jar, Log4j.jar and Slf4j-log4j.jar

2) Add the Log4j.properties profile in the current classpath and write the file according to the log4j parameter syntax

3) After the completion of the above two steps, the normal Java project will be able to use slf4j-log4j for log processing; For Java Web Projects, you will also need to configure log4jconfiglocation and Log4jconfiglistener in the. xml file

2. A brief comparison between log4j and Logback

This article is intended to illustrate the use of logback instead of log4j as a log processing component of the Spring MVC project. Although the two authors are the same, log4j has long been hosted by the Apache Foundation and has not been updated since May 2012. and Logback from birth is the author of the log4j to replace the purpose of development, so on the one hand logback inherited log4j a lot of usage, making learning and migration cost is not high, on the other hand logback in performance is significantly better than log4j, Especially in a large number of concurrent environments, and added some features that log4j does not (such as compressing log files into zip packets, etc.)

3. Spring MVC integration slf4j-logback3.1. Add dependency

<dependency><groupid>ch.qos.logback</groupid><artifactid>logback-classic</ Artifactid><version>1.1.3</version></dependency><dependency><groupid> Org.logback-extensions</groupid><artifactid>logback-ext-spring</artifactid><version> 0.1.2</version></dependency><dependency><groupid>org.slf4j</groupid>< Artifactid>jcl-over-slf4j</artifactid><version>1.7.12</version></dependency>

As shown above is the dependency required for integration, where:

The first logback-classic contains the Slf4j-api.jar, Logback-core.jar, and Logback-classsic.jar required by Logback itself.

The second logback-ext-spring is officially provided by the support of spring, its role is equivalent to the Log4jconfiglistener in log4j, this listener, the Internet is mostly used by their own implementation, The reason for this is that the plugin does not appear to be prominent in official documents, causing most people to be unaware of its existence.

The third jcl-over-slf4j is used to replace a large number of commons-logging in the spring source code with SLF4J, only to see the log printed by the spring framework itself after adding this dependency, otherwise you can only see the logs that are printed by the developer itself.

3.2. Writing Logback.xml

Logback As with log4j, you also need to write a configuration file in Classpath. However, the Logback configuration file appears to be more complex than log4j: Log4j supports not only XML-formatted configuration files, but also the properties format, and Logback only supports XML format. Fortunately, the official provides an online tool, you can convert the log4j properties file directly into the Logback XML file, the address is as follows:

http://logback.qos.ch/translator/

The detailed usage of logback and the syntax of its XML file can be found in its user Guide at the following address:

Http://logback.qos.ch/manual/introduction.html

3.3. Configure Web. xml

Similar to log4j, Logback is integrated into the spring MVC project and needs to be configured in Web. XML, as well as configuring a config location and a config listener as follows:

<context-param>         <param-name>logbackConfigLocation</param-name>         <param-value> Classpath:logback.xml</param-value></context-param><listener>         <listener-class> Ch.qos.logback.ext.spring.web.logbackconfiglistener</listener-class></listener>

Where Logbackconfiglistener is provided by the aforementioned logback-ext-spring dependency, if it is not relied on it will not find this listener class

4. Other

As you can see from the above, the steps for integrating slf4j-log4j and slf4j-logback into spring MVC (or extending to other Java Web projects) are largely the same. Once the integration is complete, the specific implementation of logback (or log4j) can be hidden through the API provided by SLF4J, and the log processing is done directly

When using SLF4J-API, it is important to note that SLF4J uses a singleton pattern, and every logger instance created in the project is pressed into the name you passed in (the incoming class<?> The instance is also converted to a string name) and is saved to a static concurrenthashmap, so as long as name (or class<?> instance) is the same, each return is actually the same logger instance. Therefore, it is not necessary to use the logger instance as a constant or static member, which can be taken with. In fact, its authors do not recommend it.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring MVC Integration Slf4j-logback

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.