ELK日誌收集系統進階使用,本文主要講解如何打造一個線上環境真實可用的日誌收集系統。有了它,你就可以和去服務器上撈日誌説再見了!
ELK環境安裝
docker-compose腳本
version: '3'
services:
elasticsearch:
image: elasticsearch:6.4.0
container_name: elasticsearch
environment:
- "cluster.name=elasticsearch" #設置集羣名稱為elasticsearch
- "discovery.type=single-node" #以單一節點模式啓動
- "ES_JAVA_OPTS=-Xms512m -Xmx512m" #設置使用jvm內存大小
- TZ=Asia/Shanghai
volumes:
- /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins #插件文件掛載
- /mydata/elasticsearch/data:/usr/share/elasticsearch/data #數據文件掛載
ports:
- 9200:9200
- 9300:9300
kibana:
image: kibana:6.4.0
container_name: kibana
links:
- elasticsearch:es #可以用es這個域名訪問elasticsearch服務
depends_on:
- elasticsearch #kibana在elasticsearch啓動之後再啓動
environment:
- "elasticsearch.hosts=http://es:9200" #設置訪問elasticsearch的地址
- TZ=Asia/Shanghai
ports:
- 5601:5601
logstash:
image: logstash:6.4.0
container_name: logstash
environment:
- TZ=Asia/Shanghai
volumes:
- /mydata/logstash/logstash.conf:/usr/share/logstash/pipeline/logstash.conf #掛載logstash的配置文件
depends_on:
- elasticsearch #kibana在elasticsearch啓動之後再啓動
links:
- elasticsearch:es #可以用es這個域名訪問elasticsearch服務
ports:
- 4560:4560
- 4561:4561
- 4562:4562
- 4563:4563
安裝要點
使用docker-compose命令運行所有服務:
docker-compose up -d
第一次啓動可能會發現Elasticsearch無法啓動,那是因為/usr/share/elasticsearch/data目錄沒有訪問權限,只需要修改/mydata/elasticsearch/data目錄的權限,再重新啓動;
chmod 777 /mydata/elasticsearch/data/
Logstash需要安裝json_lines插件。
logstash-plugin install logstash-codec-json_lines
分場景收集日誌
調試日誌:最全日誌,包含了應用中所有DEBUG級別以上的日誌,僅在開發、測試環境中開啓收集;
錯誤日誌:只包含應用中所有ERROR級別的日誌,所有環境只都開啓收集;
業務日誌:在我們應用對應包下打印的日誌,可用於查看我們自己在應用中打印的業務日誌;
記錄日誌:每個接口的訪問記錄,可以用來查看接口執行效率,獲取接口訪問參數。
Logback配置詳解
完全配置
configuration>
class="ch.qos.logback.core.rolling.RollingFileAppender">
DEBUGlevel>
filter>
${FILE_LOG_PATTERN}pattern>
UTF-8charset>
encoder>
${LOG_FILE_PATH}/debug/${APP_NAME}-%d{yyyy-MM-dd}-%i.logfileNamePattern>
${LOG_FILE_MAX_SIZE:-10MB}maxFileSize>
${LOG_FILE_MAX_HISTORY:-30}maxHistory>
rollingPolicy>
appender>
class="ch.qos.logback.core.rolling.RollingFileAppender">
ERRORlevel>
ACCEPTonMatch>
DENYonMismatch>
filter>
${FILE_LOG_PATTERN}pattern>
UTF-8charset>
encoder>
${LOG_FILE_PATH}/error/${APP_NAME}-%d{yyyy-MM-dd}-%i.logfileNamePattern>
${LOG_FILE_MAX_SIZE:-10MB}maxFileSize>
${LOG_FILE_MAX_HISTORY:-30}maxHistory>
rollingPolicy>
appender>
DEBUGlevel>
filter>
${LOG_STASH_HOST}:4560destination>
Asia/ShanghaitimeZone>
timestamp>
{
"project": "mall-tiny",
"level": "%level",
"service": "${APP_NAME:-}",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger",
"message": "%message",
"stack_trace": "%exception{20}"
}
pattern>
pattern>
providers>
encoder>
5 minutesconnectionTTL>
roundRobin>
connectionStrategy>
appender>
ERRORlevel>
ACCEPTonMatch>
DENYonMismatch>
filter>
${LOG_STASH_HOST}:4561destination>
Asia/ShanghaitimeZone>
timestamp>
{
"project": "mall-tiny",
"level": "%level",
"service": "${APP_NAME:-}",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger",
"message": "%message",
"stack_trace": "%exception{20}"
}
pattern>
pattern>
providers>
encoder>
5 minutesconnectionTTL>
roundRobin>
connectionStrategy>
appender>
${LOG_STASH_HOST}:4562destination>
Asia/ShanghaitimeZone>
timestamp>
{
"project": "mall-tiny",
"level": "%level",
"service": "${APP_NAME:-}",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger",
"message": "%message",
"stack_trace": "%exception{20}"
}
pattern>
pattern>
providers>
encoder>
5 minutesconnectionTTL>
roundRobin>
connectionStrategy>
appender>
${LOG_STASH_HOST}:4563destination>
Asia/ShanghaitimeZone>
timestamp>
{
"project": "mall-tiny",
"level": "%level",
"service": "${APP_NAME:-}",
"class": "%logger",
"message": "%message"
}
pattern>
pattern>
providers>
encoder>
5 minutesconnectionTTL>
roundRobin>
connectionStrategy>
appender>
root>
logger>
logger>
configuration>
配置要點解析
使用默認的日誌配置
springProperty
例如在application-dev.yml中定義了這些屬性:
logstash:
host: localhost
在logback-spring.xml中就可以直接這樣使用:
filter
ThresholdFilter:臨界值過濾器,過濾掉低於指定臨界值的日誌,比如下面的配置將過濾掉所有低於INFO級別的日誌。
class="ch.qos.logback.classic.filter.ThresholdFilter">
INFOlevel>
filter>
LevelFilter:級別過濾器,根據日誌級別進行過濾,比如下面的配置將過濾掉所有非ERROR級別的日誌。
class="ch.qos.logback.classic.filter.LevelFilter">
ERRORlevel>
ACCEPTonMatch>
DENYonMismatch>
filter>
appender
ConsoleAppender:控制日誌輸出到控制枱的形式,比如在console-appender.xml中定義的默認控制枱輸出。
"CONSOLE">
${CONSOLE_LOG_PATTERN}pattern>
encoder>
appender>
RollingFileAppender:控制日誌輸出到文件的形式,可以控制日誌文件生成策略,比如文件名稱格式、超過多大重新生成文件以及刪除超過多少天的文件。
class="ch.qos.logback.core.rolling.RollingFileAppender">
${LOG_FILE_PATH}/error/${APP_NAME}-%d{yyyy-MM-dd}-%i.logfileNamePattern>
${LOG_FILE_MAX_SIZE:-10MB}maxFileSize>
${LOG_FILE_MAX_HISTORY:-30}maxHistory>
rollingPolicy>
appender>
LogstashTcpSocketAppender:控制日誌輸出到Logstash的形式,可以用來配置Logstash的地址、訪問策略以及日誌的格式。
${LOG_STASH_HOST}:4561destination>
Asia/ShanghaitimeZone>
timestamp>
{
"project": "mall-tiny",
"level": "%level",
"service": "${APP_NAME:-}",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger",
"message": "%message",
"stack_trace": "%exception{20}"
}
pattern>
pattern>
providers>
encoder>
5 minutesconnectionTTL>
roundRobin>
connectionStrategy>
appender>
logger
調試日誌:所有的DEBUG級別以上日誌;
錯誤日誌:所有的ERROR級別日誌;
業務日誌:com.macro.mall包下的所有DEBUG級別以上日誌;
記錄日誌:com.macro.mall.tiny.component.WebLogAspect類下所有DEBUG級別以上日誌,該類是統計接口訪問信息的AOP切面類。
控制框架輸出日誌
Logstash配置詳解
完全配置
input {
tcp {
mode => "server"
host => "0.0.0.0"
port => 4560
codec => json_lines
type => "debug"
}
tcp {
mode => "server"
host => "0.0.0.0"
port => 4561
codec => json_lines
type => "error"
}
tcp {
mode => "server"
host => "0.0.0.0"
port => 4562
codec => json_lines
type => "business"
}
tcp {
mode => "server"
host => "0.0.0.0"
port => 4563
codec => json_lines
type => "record"
}
}
filter{
if [type] == "record" {
mutate {
remove_field => "port"
remove_field => "host"
remove_field => "@version"
}
json {
source => "message"
remove_field => ["message"]
}
}
}
output {
elasticsearch {
hosts => ["es:9200"]
action => "index"
codec => json
index => "mall-tiny-%{type}-%{ YYYY.MM.dd}"
template_name => "mall-tiny"
}
}
配置要點
input:使用不同端口收集不同類型的日誌,從4560~4563開啓四個端口;
filter:對於記錄類型的日誌,直接將JSON格式的message轉化到source中去,便於搜索查看;
output:按類型、時間自定義索引格式。
SpringBoot配置
開發環境配置:application-dev.yml
測試環境配置:application-test.yml
logstash:
host: 192.168.3.101
logging:
level:
root: debug
生產環境配置:application-prod.yml
logstash:
host: logstash-prod
logging:
level:
root: info
Kibana進階使用
首先啓動我們的測試Demo,然後通用調用接口(可以使用Swagger),產生一些日誌信息;
調用完成後在Management->Kibana->Index Patterns中可以創建Index Patterns,Kibana服務訪問地址:http://192.168.3.101:5601
創建完成後可以在Discover中查看所有日誌,調試日誌只需直接查看mall-tiny-debug*模式的日誌即可;
對於日誌搜索,kibana有非常強大的提示功能,可以通過搜索欄右側的Options按鈕打開;