logback日誌配置
阿新 • • 發佈:2019-01-24
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>