Java中多环境Logback配置与ELK日志发送
Java中多环境Logback配置与ELK日志发送
一、项目基于SpringBoot实现,引入SpringBoot相关库后,本文还要讲上传到ELK的Logstash,所以需要在pom.xml中加入:
<!-- https://mvnrepository.com/artifact/net.logstash.logback/logstash-logback-encoder --> <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>5.2</version> </dependency>
二、基于SpringBoot,要实现日志的多环境配置,比如test\dev\uat\prod,这时需要把logback.xml改为logback-spring.xml,否则无法使用基于 spring.profiles.active=dev 等方式来配置环境。
<?xml version="1.0" encoding="UTF-8" ?> <configuration scan="true" scanPeriod="30 seconds"> <!-- 上下文名称 --> <contextName>test</contextName> <!-- 存放日志文件路径 --> <property name="Log_Home" value="./logs"/> <!-- INFO级别 --> <appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>${Log_Home}/info.log</File> <!-- 根据时间来制定滚动策略 --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern> ${Log_Home}/info.%d{yyyy-MM-dd}.%i.log.gz </FileNamePattern> <!-- 多久后自动清楚旧的日志文件,单位:月 --> <MaxHistory>12</MaxHistory> <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- 默认值是 10MB,文档最大值 --> <MaxFileSize>100MB</MaxFileSize> </TimeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n</Pattern> </encoder> </appender> <!-- ch.qos.logback.core.ConsoleAppender 控制台输出 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 --> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n</pattern> </encoder> </appender> <!--上传日志到LogStash--> <appender name="LOGSTASH_uat" class="net.logstash.logback.appender.LogstashTcpSocketAppender"> <destination>127.0.0.1:5044</destination> <!-- encoder必须配置,有多种可选 --> <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"> <customFields>{"appName":"backend-sync"}</customFields> </encoder> <connectionStrategy> <roundRobin> <connectionTTL>5 minutes</connectionTTL> </roundRobin> </connectionStrategy> </appender> <!--上传日志到LogStash--> <appender name="LOGSTASH_dev" class="net.logstash.logback.appender.LogstashTcpSocketAppender"> <destination>127.0.0.1:5044</destination> <!-- encoder必须配置,有多种可选 --> <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"> <customFields>{"appName":"backend-sync"}</customFields> </encoder> <connectionStrategy> <roundRobin> <connectionTTL>5 minutes</connectionTTL> </roundRobin> </connectionStrategy> </appender> <springProfile name="uat,prod"> <!-- 当前日志总级别为TRACE、DEBUG、INFO、 WARN、ERROR、ALL和 OF --> <root level="WARN"> <appender-ref ref="STDOUT"/> <appender-ref ref="FILE_INFO"/> <appender-ref ref="LOGSTASH_uat"/> </root> <logger name="com.yungoal" level="INFO"> <appender-ref ref="STDOUT"/> <appender-ref ref="FILE_INFO"/> <appender-ref ref="LOGSTASH_uat"/> </logger> </springProfile> <springProfile name="dev,test"> <!-- 当前日志总级别为TRACE、DEBUG、INFO、 WARN、ERROR、ALL和 OF --> <root level="WARN"> <appender-ref ref="STDOUT"/> <appender-ref ref="FILE_INFO"/> <appender-ref ref="LOGSTASH_dev"/> </root> </springProfile> </configuration>
三、使用方法
方法一:java -jar myapplication.jar --spring.profiles.active=dev
方法二:创建spring boot的application.properties在类目录下,然后里面加入一行spring.profiles.active=dev,然后再建立几个环境文件,如application-dev.properties、application-uat.properties等,系统将自动通过application.properties中的spring.profiles.active=dev来选择。
宋兴柱:转载内容,请标明出处,谢谢!源文来自 宝贝云知识分享:https://www.dearcloud.cn