一, Maven依赖包 pom.xml

 1         <dependency>
 2             <groupId>org.slf4jgroupId>
 3             <artifactId>slf4j-apiartifactId>
 4             <version>1.6.0version>
 5             <type>jartype>
 6             <scope>compilescope>
 7         dependency>
 8         <dependency>
 9             <groupId>ch.qos.logbackgroupId>
10             <artifactId>logback-coreartifactId>
11             <version>0.9.28version>
12             <type>jartype>
13         dependency>
14         <dependency>
15             <groupId>ch.qos.logbackgroupId>
16             <artifactId>logback-classicartifactId>
17             <version>0.9.28version>
18             <type>jartype>
19         dependency>



二, 代码使用

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

/** logger */
private static Logger logger = LoggerFactory.getLogger(ExampleClass.class);

logger.debug(
"output logs");



三, 日志配置文件
配置多个logger
比如
第一个记载日志,
第二个输出告警
logback.xml

 1 xml version="1.0"?>
 2 <configuration>
 3     
 4     <property file="src\main\resources\app.properties" />
 5     <appender name="FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">
 6         <Encoding>UTF-8Encoding>
 7         <File>${path.log}/testProject.logFile>
 8         <Prudent>truePrudent>
 9         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
10             <FileNamePattern>${path.log}/testProject-%d{yyyy-MM-dd_HH}-%i.logFileNamePattern>
11             <MaxHistory>10MaxHistory>
12             <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
13                    <MaxFileSize>5MBMaxFileSize>
14             TimeBasedFileNamingAndTriggeringPolicy>
15         rollingPolicy>
16         <layout class="ch.qos.logback.classic.PatternLayout">
17             <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%nPattern>
18         layout>
19     appender>
20     <appender name="warningLoggerAppender"  class="ch.qos.logback.core.rolling.RollingFileAppender">
21         <Encoding>UTF-8Encoding>
22         <File>${path.alarm}/testProject-warning.txtFile>
23         <Prudent>truePrudent>
24         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
25             <FileNamePattern>${path.alarm}/testProject-warning-%d{yyyyMMddHHmmss}.txtFileNamePattern>
26             <MaxHistory>3MaxHistory>
27             <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
28                    <MaxFileSize>5MBMaxFileSize>
29             TimeBasedFileNamingAndTriggeringPolicy>
30         rollingPolicy>
31         <layout class="ch.qos.logback.classic.PatternLayout">
32             
33             <Pattern>%msg%nPattern>
34         layout>
35     appender>
36 
37     <logger name="com.test">
38         <level value="DEBUG" />
39         <appender-ref ref="FILE" />
40     logger>
41     <logger name="warningLogger">
42         <level value="TRACE" />
43         <appender-ref ref="warningLoggerAppender" />
44     logger>
45 configuration>