Spring与logback集成

需求:

日志记录,日志格式化特殊需求化定制,那么需要logback

转载请注明出自:http://snv.iteye.com/

那么logback和spring进行集成开发

 

jar依赖:

<!-- logback start -->
 <dependency>
	<groupId>ch.qos.logback</groupId>
	<artifactId>logback-classic</artifactId>
	<version>1.0.13</version>
</dependency>
<!-- logback end -->

 spring相关:

<!-- apache common start -->
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-io</artifactId>
	<version>1.3.2</version>
</dependency>
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-collections4</artifactId>
	<version>4.0</version>
</dependency>
 
<dependency>
	<groupId>org.codehaus.jackson</groupId>
	<artifactId>jackson-mapper-lgpl</artifactId>
	<version>1.9.13</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
	<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>
	<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>jstl</artifactId>
	<version>1.2</version>
</dependency>
<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>jsp-api</artifactId>
	<version>2.0</version>
	<scope>provided</scope>
</dependency>
<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>servlet-api</artifactId>
	<version>2.5</version>
	<scope>provided</scope>
</dependency>

 

web.xml:

<listener>
	<listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
</listener>
<context-param>
	<param-name>logbackConfigLocation</param-name>
	<param-value>classpath:logback.xml</param-value>
</context-param>

logback.xml:注意路径

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<Pattern>%d{HH:mm:ss.SSS} %-5level %logger{80} - %msg%n</Pattern>
		</encoder>
	</appender>
	<appender name="FILE"
  class="ch.qos.logback.core.rolling.RollingFileAppender">
          <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
               <FileNamePattern>D:/logs/debug.%d{yyyy-MM-dd}.log</FileNamePattern>
               <MaxHistory>30</MaxHistory>
          </rollingPolicy>
          <encoder>
               <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
         </encoder>
       </appender>
	<root>
		<level value="INFO" />
		<appender-ref ref="FILE" />
	</root>
</configuration>

使用:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Test {
	private Logger log = LoggerFactory.getLogger(UsrController.class);
	public String login(HttpServletRequest request) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		log.info("username:{}  and pwd:{}", username, password);
		return null;
	}
}

 

你可能感兴趣的:(logback)