logback.xml文件
<configuration dabug="false">
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--日志生成位置-->
<File>E:/Log/SSM4.3.7.log</File>
<!--日志生成格式-->
<encoder>
<Pattern>%d %5p [%t] %c:%L - %m%n</Pattern>
</encoder>
<!--日志生成每天打包压缩一次-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>E:/Log/SSM4.3.7.log%d{yyyy-MM-dd}.gz</FileNamePattern>
</rollingPolicy>
</appender>
<!--日志生成级别-->
<root level="debug">
<appender-ref ref="FILE"></appender-ref>
</root>
<!--需要生成日志的包-->
<logger name="com.ssm" level="debug"/>
<logger name="org" level="error"/>
</configuration>
Controller和日志相关主要代码
//日志
final Logger logger = LoggerFactory.getLogger(StudentController.class);
@Autowired //自动装配ByType
@Resource(type=StudentService.class) //自动装配ByName 使用name属性时用byname 使用type时用bytype
//@Qualifier //ByNmame注入
private StudentService studentServices;
@RequestMapping("/register")
public String register(Student student){
//注册方法
try {
studentServices.addStu(student);
//ModelAndView mv = new ModelAndView();
//其底层执行的是request.setAttribute()方法
} catch (Exception e) {
//把异常添加到日志中
logger.error(e.getMessage());
}
return "jsp/ok";
}
pom.xml文件
<!-- 日志文件管理包 -->
<!-- logback日志文件管理包版本 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.logback-extensions</groupId>
<artifactId>logback-ext-spring</artifactId>
<version>0.1.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.4</version>
</dependency>
web.xml配置logback
<!-- 配置logback日志 -->
<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>