输出到redis
<dependency>
<groupId>com.cwbase</groupId>
<artifactId>logback-redis-appender</artifactId>
<version>1.1.5</version>
</dependency>
bootstrap.properties
#日志配置
logging.redis.host=120.24.188.97
logging.redis.port=6379
logging.redis.password=123456
logging.redis.key=elk-list
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty scope="context" name="APP_NAME" source="spring.application.name"/>
<springProperty scope="context" name="REDIS_HOST" source="logging.redis.host"/>
<springProperty scope="context" name="REDIS_PORT" source="logging.redis.port"/>
<springProperty scope="context" name="REDIS_PASSWORD" source="logging.redis.password"/>
<springProperty scope="context" name="REDIS_KEY" source="logging.redis.key"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${PID:- } - %d{yyyy/MM/dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} [%16.-23thread] --- %msg --- %X{req.remoteHost}%X{req.queryString} [%logger{10}] %n</pattern>
</encoder>
</appender>
<appender name="logstash" class="com.cwbase.logback.RedisAppender">
<source>${APP_NAME}</source>
<key>${REDIS_KEY}</key>
<host>${REDIS_HOST}</host>
<port>${REDIS_PORT}</port>
<password>${REDIS_PASSWORD}</password>
</appender>
<appender name="logstash-async" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="logstash" />
</appender>
<root level="INFO">
<appender-ref ref="console" />
<appender-ref ref="logstash-async" />
</root>
</configuration>
输出到文件
application.yml
#日志配置
logging:
path: D:/data
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 日志存放路径 -->
<springProperty scope="context" name="logPath" source="logging.path"/>
<property name="log.path" value="${logPath}" />
<!-- 日志输出格式 -->
<property name="log.pattern"
value="%d{${HH:mm:ss.SSS}} %green([%X{mdcTraceId},%X{mdcTraceNum}]) %highlight(%-5level) %cyan(${PID:- }) [%20.20t] %red(%-40.40logger{39}){cyan} : %msg%n"/>
<property name="file.log.pattern"
value="%d{${yyyy-MM-dd HH:mm:ss.SSS}} %-5level ${PID:- } --- [%20.20t] %-40.40logger{39} : %msg%n"/>
<!-- 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
<!-- 记录日志的编码:此处设置字符集 - -->
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- 全局日志输出 -->
<appender name="file_application" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${log.path}/application/application.%i.log</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>1</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>${file.log.pattern}</pattern>
<!-- 记录日志的编码:此处设置字符集 - -->
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- info日志输出 -->
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/info.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/info/%d{yyyy-MM-dd}-%i.log</fileNamePattern>
<!--日志文件最大的大小-->
<maxFileSize>10MB</maxFileSize>
<!-- 最多保存30天的日志 -->
<maxHistory>30</maxHistory>
<!-- 最大限制 -->
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>${file.log.pattern}</pattern>
<!-- 记录日志的编码:此处设置字符集 - -->
<charset>UTF-8</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 只会打印info日志-->
<level>INFO</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- error日志输出 -->
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/error.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/error/%d{yyyy-MM-dd}-%i.log</fileNamePattern>
<!--日志文件最大的大小-->
<maxFileSize>10MB</maxFileSize>
<!-- 最多保存30天的日志 -->
<maxHistory>30</maxHistory>
<!-- 最大限制 -->
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>${file.log.pattern}</pattern>
<!-- 记录日志的编码:此处设置字符集 - -->
<charset>UTF-8</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>ERROR</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 系统模块日志级别控制 -->
<logger name="com.org.sys" level="debug"/>
<!-- Spring日志级别控制 -->
<logger name="org.springframework" level="warn"/>
<logger name="com.zaxxer.hikari" level="warn"/>
<logger name="io.lettuce.core" level="warn"/>
<logger name="io.seata.core" level="warn"/>
<logger name="com.netflix.loadbalancer" level="warn"/>
<root level="info">
<appender-ref ref="console" />
</root>
<!--系统操作日志-->
<root level="info">
<appender-ref ref="file_application"/>
<appender-ref ref="file_info" />
<appender-ref ref="file_error" />
</root>
</configuration>