一、概述:
- 步骤:第一步加入相关pom文件;
- 步骤二:添加logback.xml配置;
- 步骤三:web.xml添加日志监听
二、pom.xml加入jar包
1 <!-- Logback --> 2 <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic --> 3 <dependency> 4 <groupId>ch.qos.logback</groupId> 5 <artifactId>logback-classic</artifactId> 6 <version>1.2.3</version> 7 </dependency> 8 <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core --> 9 <dependency> 10 <groupId>ch.qos.logback</groupId> 11 <artifactId>logback-core</artifactId> 12 <version>1.2.3</version> 13 </dependency> 14 <!-- https://mvnrepository.com/artifact/org.logback-extensions/logback-ext-spring --> 15 <dependency> 16 <groupId>org.logback-extensions</groupId> 17 <artifactId>logback-ext-spring</artifactId> 18 <version>0.1.4</version> 19 </dependency> 20 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> 21 <dependency> 22 <groupId>org.slf4j</groupId> 23 <artifactId>slf4j-api</artifactId> 24 <version>1.7.25</version> 25 </dependency> 26 <!-- https://mvnrepository.com/artifact/org.slf4j/jcl-over-slf4j --> 27 <dependency> 28 <groupId>org.slf4j</groupId> 29 <artifactId>jcl-over-slf4j</artifactId> 30 <version>1.7.25</version> 31 </dependency>
三、web 资源文件中配置logback.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration> 3 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 4 <Encoding>UTF-8</Encoding> 5 <layout class="ch.qos.logback.classic.PatternLayout"> 6 <!--格式化输出:%d表示日期,%thread表示线程名, 7 %-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 --> 8 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> 9 </layout> 10 </appender> 11 12 <root level="INFO"> 13 <appender-ref ref="STDOUT" /> 14 </root> 15 </configuration>
四、web.xml中添加日志监听
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> 3 <filter> 4 <filter-name>CharacterEncodingFilter</filter-name> 5 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 6 <init-param> 7 <param-name>encoding</param-name> 8 <param-value>utf-8</param-value> 9 </init-param> 10 </filter> 11 <filter-mapping> 12 <filter-name>CharacterEncodingFilter</filter-name> 13 <url-pattern>/*</url-pattern> 14 </filter-mapping> 15 <context-param> 16 <param-name>contextConfigLocation</param-name> 17 <param-value>classpath:spring.xml</param-value> 18 </context-param> 19 <listener> 20 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 21 </listener> 22 <servlet> 23 <servlet-name>springDispatcherServlet</servlet-name> 24 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 25 <init-param> 26 <param-name>contextConfigLocation</param-name> 27 <param-value>classpath:springmvc.xml</param-value> 28 </init-param> 29 <load-on-startup>1</load-on-startup> 30 </servlet> 31 <servlet-mapping> 32 <servlet-name>springDispatcherServlet</servlet-name> 33 <url-pattern>/</url-pattern> 34 </servlet-mapping> 35 <filter> 36 <filter-name>HiddenHttpMethodFilter</filter-name> 37 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 38 </filter> 39 <filter-mapping> 40 <filter-name>HiddenHttpMethodFilter</filter-name> 41 <url-pattern>/*</url-pattern> 42 </filter-mapping> 43 <!-- 添加日志监听器 --> 44 <context-param> 45 <param-name>logbackConfigLocation</param-name> 46 <param-value>classpath:logback.xml</param-value> 47 </context-param> 48 <listener> 49 <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class> 50 </listener> 51 </web-app>
参考地址:https://www.cnblogs.com/EasonJim/p/7810852.html
标签:xml,web,25,springmvc,springframework,使用,org,logback From: https://www.cnblogs.com/lixiuming521125/p/17069015.html