logback.xml添加下面内容
<!--LogStash访问host-->
<springProperty name="LOG_STASH_HOST" scope="context" source="logstash.host" defaultValue="localhost"/>
<!--接口访问记录日志输出到LogStash-->
<appender name="LOG_STASH_RECORD" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>${LOG_STASH_HOST}:4560</destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<timeZone>Asia/Shanghai</timeZone>
</timestamp>
<!--自定义日志输出格式-->
<pattern>
<pattern>
{
"project": "spring-cloud-learning",
"level": "%level",
"service": "${APP_NAME:-}",
"class": "%logger",
"traceId": "%X{traceId}",
"spanId": "%X{spanId}",
"message": "%message"
}
</pattern>
</pattern>
</providers>
</encoder>
<!--当有多个LogStash服务时,设置访问策略为轮询-->
<connectionStrategy>
<roundRobin>
<connectionTTL>5 minutes</connectionTTL>
</roundRobin>
</connectionStrategy>
</appender>
<logger name="com.fandf.log.aspect.WebLogAspect" level="DEBUG">
<appender-ref ref="LOG_STASH_RECORD"/>
</logger>
springboot项目配置文件添加
#logstash ip,mac本地连接方式
logstash:
host: host.docker.internal
打包成镜像后执行
docker run -p 9005:9005 --name user-service \
--link logstash:logstash \
-v /etc/localtime:/etc/localtime \
-d fandf/user-service:1.0.0
我的controller
调用接口
登录kibana创建索引
description 为swagger注解描述, traceId为全局链路id
- 只测试elk,可以在源码 application-docker.yml里面删除seata和nacos相关配置即可