前置环境
部署步骤
pom.xml
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>6.5</version>
</dependency>
确认自己项目中是否存在 jackson依赖,一般情况下,如果有 redission、nacos的依赖就不需要引用。
注意logback的版本和jackson得版本存在关联差异性。我这边的对照版本是 jackson 2.11.4 => logback 6.5
defaults-logstash.xml
<?xml version="1.0" encoding="UTF-8"?>
<included>
<!--
// 参考:https://blog.csdn.net/Hello_World_QWP/article/details/115969198
// Failed to rename context [logback] as [nacos] java.lang.IllegalStateException: Context has been already given a name
// 启动配置
System.setProperty("nacos.logging.default.config.enabled","false");
-->
<!--
logstash配置
input {
tcp {
port => 5044
codec => "json_lines"
}
}
output {
elasticsearch {
hosts => ["esIP:9200"]
index => "log-%{+YYYY.MM.dd}"
#user => "elastic"
#password => "changeme"
}
stdout { codec => rubydebug }
}
-->
<!-- 需要配置logstash -->
<springProperty scope="context" name="logstash.ips" source="jamel.logstash.ips" defaultValue="127.0.0.1:5044"/>
<springProperty scope="context" name="logstash.connectionTTL" source="jamel.logstash.connectionTTL"
defaultValue="5 minutes"/>
<!-- https://github.com/logfellow/logstash-logback-encoder -->
<appender name="LOGSTASH_APPENDER" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<includeCallerData>true</includeCallerData>
<destination>${logstash.ips}</destination>
<!-- encoder必须配置,有多种可选 -->
<!-- 日志输出编码 -->
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<pattern>
<omitEmptyFields>true</omitEmptyFields>
<!-- 这块是日志变量,没有的可以忽略 -->
<pattern>
{
"severName": "${spring.application.name}",
"severIp": "%ip",
"userId": "%userId",
"port": "${server.port}",
"level": "%p",
"marker": "%xMarker",
"method": "%xLogger.%xMethod:%xLine",
"message": "%m",
"exception": "${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}",
"createTime": "%d{yyyy-MM-dd HH:mm:ss.SSS}"
}
</pattern>
</pattern>
</providers>
</encoder>
<!-- 轮询负载各端口 -->
<connectionStrategy>
<roundRobin>
<connectionTTL>${logstash.connectionTTL}</connectionTTL>
</roundRobin>
</connectionStrategy>
</appender>
</included>
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="com/jamel/log/xml/defaults.xml"/>
<!--日志通过logstash记录数据到ES-->
<include resource="com/jamel/log/xml/defaults-logstash.xml"/>
<springProfile name="dev,prod">
<root level="${log.level.console}">
<appender-ref ref="LOGSTASH_APPENDER"/>
</root>
</springProfile>
</configuration>
bootstrap.yml
jamel:
logstash:
ips: 47.107.76.32:5044
服务部署
SpringBoot启动服务
查看kibana情况,至此表示日志已经从springboot推送到es。可以通过kibana查看
本文含有隐藏内容,请 开通VIP 后查看