1. logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<contextName>logback</contextName>
<!--输出到控制台-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger -%msg%n</pattern>
</encoder>
</appender>
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>C:gather-logscrm-info.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>C:gather-logsserver-info-%d{yyyyMMdd}.log.%i</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>2</maxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -%msg%n</Pattern>
</layout>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%date{yyyy-MM-dd HH:mm:ss} | %highlight(%-5level) | %boldYellow(%thread) | %boldGreen(%logger) |
%msg%n
</pattern>
</encoder>
</appender>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<File>C:gather-logscrm-error.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>C:gather-logsserver-error-%d{yyyyMMdd}.log.%i
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>2</maxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -%msg%n
</Pattern>
</layout>
</appender>
<!-- <logger name="com.example.demo1.controller" level="INFO">-->
<!-- </logger>-->
<root level="INFO">
<appender-ref ref="console"/>
<appender-ref ref="INFO_FILE"/>
<appender-ref ref="ERROR_FILE"/>
</root>
<!-- TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF -->
<logger name="org.springframework" level="DEBUG" additivity="true"/>
<logger name="org.apache" level="OFF"/>
<logger name="com.alibaba" level="OFF"/>
<!-- 如果这里设置了additivity=true 代码打印信息向上传递,将打印信息传递到root ,并且自己的appender-ref 也会打印,这样就是打印了俩次日志-->
<logger name="com.example.demo1" level="OFF" additivity="false">
<appender-ref ref="console"/>
</logger>
</configuration>
(1)
<contextName>logback</contextName>
<!-- name的值是变量的名称,value的值时变量定义的值。通过定义的值会被插入到logger上下文中。定义后,可以使“${}”来使用变量。 -->
<property name="log.path" value="G:/logs/pmp" />
(2)
<configuration scan="true" scanPeriod="60 seconds" debug="false">
scan: 当此属性设置为true时,配置文档如果发生改变,将会被重新加载,默认值为true
scanPeriod: 设置监测配置文档是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。
debug: 当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。
2. 设置Logger
<logger name="com.example.demo1" level="INFO" additivity="true">
<appender-ref ref="console"/>
</logger>
(1)logger 设置 level 时,则根据level 过滤日志信息,如果没有设置level ,则继承root的level。
(2)logger 的additivity属性,如果设置为true,则会将日志信息向上传递给root,同时也执行自己的<appender-ref> 这里会出现打印2次的现象。
如下:
package com.example.demo1.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
private static Logger logger= LoggerFactory.getLogger(MyController.class);
@RequestMapping("/getOut")
@ResponseBody
public String getBody(){
logger.info("-----------info");
logger.debug("----------debug");
logger.warn("-----------warn");
logger.error("----------error");
return "body";
}
}
3. appender
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>C:gather-logscrm-info.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>C:gather-logsserver-info-%d{yyyyMMdd}.log.%i</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>2</maxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -%msg%n</Pattern>
</layout>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%date{yyyy-MM-dd HH:mm:ss} | %highlight(%-5level) | %boldYellow(%thread) | %boldGreen(%logger) |
%msg%n
</pattern>
</encoder>
</appender>
(1) 日志打印保存的路径
<File>C:gather-logscrm-info.log</File>
(2) 日志文件的命名方式 如果: server-info-20190617.log.1
<fileNamePattern>C:gather-logsserver-info-%d{yyyyMMdd}.log.%i</fileNamePattern>
(3) 日志的最大设置为5M ,如果大于5M ,则归档,创建文件如: server-info-20190617.log.2
<maxFileSize>5MB</maxFileSize>
(4)日志的保存时间 2天
<maxHistory>2</maxHistory>
(5)Logger 的输入格式 eg:
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -%msg%n</Pattern>
</layout>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%date{yyyy-MM-dd HH:mm:ss} | %highlight(%-5level) | %boldYellow(%thread) | %boldGreen(%logger) | %msg%n
</pattern>
</encoder>
4. 配置log的dev prd环境
<springProfile name="dev">
<property name="log.path" value="/Users/guoxiwang/software/idea/log/dev" />
</springProfile>
<springProfile name="prd">
<property name="log.path" value="/Users/guoxiwang/software/idea/log/prd" />
</springProfile>
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文档的路径及文档名 -->
<file>${log.path}/web_debug.log</file>
<!--日志文档输出格式-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset> <!-- 设置字符集 -->
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志归档 -->
<fileNamePattern>${log.path}/web-debug-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>1MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文档保留天数-->
<maxHistory>15</maxHistory>
</rollingPolicy>
<!-- 此日志文档只记录debug级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>debug</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>