TIP
- 根据docker-compose安装elk,文件见百度网盘
- 修改启动logstash的配置,这里用的是redis的方法
- 详细见logstash官网文档配置
- https://www.elastic.co/guide/en/logstash-versioned-plugins/current/index.html
- 记得删除备注
input {
redis {
data_type => "list" #存储类型
type => "redis-input"
key => "logstash:redis"#key值,后面要与spring boot中key保持一致
host => "192.168.5.101"
port => 6379
threads => 5 #启动线程数量
codec => "json"
}
}
output {
elasticsearch {
hosts => ["192.168.5.108:9200"]
# index是定义将过滤后的日志推送到Elasticsearch后存储的名字
# kibana通过这个index来确定搜索索引
index => "springboot-elk-%{+YYYY.MM.dd}"
}
stdout { codec => rubydebug} #是否在控制台打印
}
TIP
- 在pom.xml中加入相关依赖
- 配置logstash的appender
<dependency>
<groupId>com.cwbase</groupId>
<artifactId>logback-redis-appender</artifactId>
<version>1.1.6</version>
</dependency>
TIP
- 在resources下新增一个logback-spring.xml / logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<Target>System.out</Target>
<encoder>
<pattern>[%d{HH:mm:ss}][%t][%p][%c]-%m%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>info</level>
</filter>
</appender>
<appender name="LOGSTASH" class="com.cwbase.logback.RedisAppender">
<source>boot-sleuth</source>
<type>dev</type>
<host>192.168.5.101</host>
<key>logstash:redis</key>
<tags>dev</tags>
<mdc>true</mdc>
<location>true</location>
<callerStackIndex>0</callerStackIndex>
</appender>
<!-- 配置日志的相关记录等级 -->
<root level="info">
<appender-ref ref="Console"/>
<appender-ref ref="LOGSTASH"/>
</root>
</configuration>
TIP
- 访问http://xxx.com:5601,打开kibana
- 根据上面的配置文件,查询相关索引,即可查看日志
- 多环境日志区分
- 首先,logback-spring.xml支持根据环境区分
<springProfile name="prod">
<appender ...>
...
</appender>
<root ...>
...
</root>
</springProfile>
<springProfile name="dev">
<appender ...>
...
</appender>
<root ...>
...
</root>
</springProfile>
TIP
- 然后是appender,里面的type用于区分
<appender name="LOGSTASH" class="com.cwbase.logback.RedisAppender">
<source>boot-sleuth</source>
<type>gateway</type>
<host>192.168.5.101</host>
<key>logstash:redis</key>
<tags>dev</tags>
<mdc>true</mdc>
<location>true</location>
<callerStackIndex>0</callerStackIndex>
</appender>
TIP
- 之后配置logstash的conf文件
input {
redis {
data_type => "list"
type => "gateway"
key => "logstash:redis"
host => "192.168.5.101"
port => 6379
threads => 5
codec => "json"
}
redis {
data_type => "list"
type => "member"
key => "logstash:redis"
host => "192.168.5.101"
port => 6379
threads => 5
codec => "json"
}
}
# 通过type进行判断,多个output时,需要指定template_name
output {
if [type] == "gateway" {
elasticsearch {
hosts => ["192.168.5.108:9200"]
index => "painaodai-gateway-%{+YYYY.MM.dd}"
template_name => "gateway"
}
}
if [type] == "member" {
elasticsearch {
hosts => ["192.168.5.108:9200"]
index => "painaodai-member-%{+YYYY.MM.dd}"
template_name => "member"
}
}
stdout {
codec => rubydebug
}
}