Java Log Framework-spring using Logback (spring/spring MVC)

Source: Internet
Author: User

Following the previous article, http://www.cnblogs.com/EasonJim/p/7800880.html is a common Java-based project, and if you want to integrate with spring and spring MVC, you need to do the following:

First, Spring

POM:

        <!--Logback -        <!--Https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -        <Dependency>            <groupId>Ch.qos.logback</groupId>            <Artifactid>Logback-classic</Artifactid>            <version>1.2.3</version>        </Dependency>        <!--Https://mvnrepository.com/artifact/ch.qos.logback/logback-core -        <Dependency>            <groupId>Ch.qos.logback</groupId>            <Artifactid>Logback-core</Artifactid>            <version>1.2.3</version>        </Dependency>        <!--https://mvnrepository.com/artifact/org.logback-extensions/logback-ext-spring -        <Dependency>            <groupId>Org.logback-extensions</groupId>            <Artifactid>Logback-ext-spring</Artifactid>            <version>0.1.4</version>        </Dependency>        <!--Https://mvnrepository.com/artifact/org.slf4j/slf4j-api -        <Dependency>            <groupId>Org.slf4j</groupId>            <Artifactid>Slf4j-api</Artifactid>            <version>1.7.25</version>        </Dependency>        <!--https://mvnrepository.com/artifact/org.slf4j/jcl-over-slf4j -        <Dependency>            <groupId>Org.slf4j</groupId>            <Artifactid>Jcl-over-slf4j</Artifactid>            <version>1.7.25</version>        </Dependency>

Where logback-ext-spring this jar package is intended for use with spring. Jcl-over-slf4j:commons-logging to the SLF4J bridge

You do not need to set Logback.xml to output logs at this time.

Example Project: https://github.com/easonjim/5_java_example/tree/master/springtest/test22/HelloSpring

Two, Spring MVC

As with the Pom, the key point is to configure listen in Web. XML and specify a Logback.xml file

Logback.xml:

<?XML version= "1.0" encoding= "UTF-8"?><Configuration>    <!--Console Output -       <Appendername= "STDOUT"class= "Ch.qos.logback.core.ConsoleAppender">       <!--Log Output Encoding -         <Encoding>UTF-8</Encoding>           <Layoutclass= "Ch.qos.logback.classic.PatternLayout">                <!--formatted output:%d represents the date,%thread represents the thread name,%-5level: the level displays 5 character widths from the left%msg: Log messages,%n are newline characters -             <pattern>%d{yyyy-mm-dd HH:mm:ss. SSS} [%thread]%-5level%logger{50}-%msg%n</pattern>           </Layout>       </Appender>       <!--Log Output Level -    <Root Level= "INFO">           <Appender-refref= "STDOUT" />       </Root> </Configuration>

Xml:

<?XML version= "1.0" encoding= "UTF-8"?><!--Configuring XML Schemas -<Web-appID= "webapp_id"version= "3.1"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd ">                                     <Display-name>Archetype Created Web Application</Display-name>      <!--Configuring the Servlet,spring takeover -    <servlet>        <Servlet-name>Srpingmvctest</Servlet-name>            <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>        <Load-on-startup>1</Load-on-startup>     </servlet>    <!--Configuring the Servlet,spring takeover -    <servlet-mapping>        <Servlet-name>Srpingmvctest</Servlet-name>        <Url-pattern>/</Url-pattern>    </servlet-mapping>    <!--Add a log listener--<context-param> <param-name>logbackconfiglocation</param-name&          Gt <param-value>classpath:logback.xml</param-value> </context-param> <listener> &L T;listener-class>ch.qos.logback.ext.spring.web.logbackconfiglistener</listener-class> </listener >  </Web-app>

After the configuration is complete, you can output the log.

Case Engineering: Https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test34/test1

Reference:

http://blog.csdn.net/evankaka/article/details/50637994

Java Log Framework-spring using Logback (spring/spring MVC)

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.