使用logstash
收集系統上的日誌,並使用 grok
解析日誌,使用mutate
修改解析出來的字段類型、刪除字段、重命名字段,最後將解析好的日主輸出到 elasticsearch
中。html
vim output-es.yml
java
input { file { id => "mutate-id" path => ["/Users/huan/soft/elastic-stack/logstash/logstash/pipeline.conf/output-es/*.log"] start_position => "beginning" sincedb_path => "/Users/huan/soft/elastic-stack/logstash/logstash/pipeline.conf/output-es/sincedb.db" codec => multiline { pattern => "^\[+" negate => "true" what => "previous" charset => "UTF-8" auto_flush_interval => 2 } } } filter { grok { match => { "message" => "(?m)^\[%{INT:pid}\]%{SPACE}%{TIMESTAMP_ISO8601:createTime}%{SPACE}\[%{DATA:threadName}\]%{SPACE}%{LOGLEVEL:LEVEL}%{SPACE}%{JAVACLASS:javaClass}#(?<methodName>[a-zA-Z_]+):%{INT:linenumber}%{SPACE}-%{GREEDYDATA:msg}" remove_field => ["message"] } } mutate { convert => { "pid" => "integer" } rename => { "msg" => "message" } } # 格式化 createTime 將 源格式 轉換成 目標格式 date { match => ["createTime","yyyy-MM-dd HH:mm:ss.SSS","yyyy-MM-dd HH:mm:ss.SSS"] target => "@timestamp" remove_field => ["createTime"] } } output { # 能夠經過 template 或 template_name 指定es模板的名字 elasticsearch { hosts => ["http://localhost:9200","http://localhost:9201","http://localhost:9202"] user => "springboot_logstash" password => "123456" index => "springboot-%{+YYYY.MM.dd}" template_overwrite => "false" } }
elasticsearch
配置參數解析:hosts
: es
的訪問地址,建議使用非master
節點。user
: 訪問es的用戶名。password
:訪問es的密碼。index
:在es中的索引名稱。template
:設置本身的es模板路徑。template_name
:使用es中的索引模板名稱。上方的es的密碼是明文的,可能存在泄漏,能夠使用 logstash keystore
來解決。web
{ "error": { "root_cause": [ { "type": "security_exception", "reason": "action [indices:data/ write/bulk] is unauthorized for user [logstash_system] on indices [], this action is granted by the index privileges [create_doc,create,delete,index,write,all]" } ], "type": "secu rity_exception", "reason": "action [indices:data/write/bulk] is unauthorized for user [logstash_system] on indices [], this action is granted by the index privileges [create_doc ,create,delete,index,write,all]" }, "status": 403 }
當咱們使用系統自帶的logstash_system
用戶時,可能會報indices:data/write/bulk
這個操做沒有權限,解決方法以下(本身新建一個用戶和角色)。
spring
[9708] 2021-05-13 11:14:51.873 [http-nio-8080-exec-1] INFO org.springframework.web.servlet.DispatcherServlet#initServletBean:547 -Completed initialization in 1 ms [9708] 2021-05-13 11:14:51.910 [http-nio-8080-exec-1] ERROR com.huan.study.LogController#showLog:32 -請求:[/showLog]發生了異常 java.lang.ArithmeticException: / by zero at com.huan.study.LogController.showLog(LogController.java:30) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
bin/logstash -f output-es.yml
一、https://www.elastic.co/guide/en/logstash/current/keystore.htmljson
二、https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.htmlvim