1. 程式人生 > 實用技巧 >Logstash 收集日誌

Logstash 收集日誌

安裝 Tomcat

# 安裝 jdk 
[root@web01 ~]# rpm -ivh jdk-8u181-linux-x64.rpm

# 下載
[root@web01 ~]# wget https://archive.apache.org/dist/tomcat/tomcat-10/v10.0.0-M7/bin/apache-tomcat-10.0.0-M7.tar.gz

# 解壓
[root@web01 ~]# tar xf apache-tomcat-10.0.0-M7.tar.gz -C /usr/local/

# 做軟連線
[root@web01 ~]# ln -s /usr/local/apache-tomcat-10.0.0-M7 /usr/local/tomcat

# 啟動 Tomcat
[root@web01 ~]# /usr/local/tomcat/bin/startup.sh

# 6.訪問頁面 10.0.0.7:8080

收集 Tomcat 訪問日誌(Access-log)

Tomcat 訪問日誌(Access-log)的格式在 server.xml 中可以直接修改,先修改訪問格式的日誌試試水 ~ ~ ~

# 把原來的日誌格式註釋,新增我們的格式
[root@web01 ~]# vim /usr/local/tomcat/conf/server.xml
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".log"
               pattern="{&quot;clientip&quot;:&quot;%h&quot;,&quot;ClientUser&quot;:&quot;%l&quot;,&quot;authenticated&quot;:&quot;%u&quot;,&quot;AccessTime&quot;:&quot;%t&quot;,&quot;method&quot;:&quot;%r&quot;,&quot;status&quot;:&quot;%s&quot;,&quot;SendBytes&quot;:&quot;%b&quot;,&quot;Query?string&quot;:&quot;%q&quot;,&quot;partner&quot;:&quot;%{Referer}i&quot;,&quot;AgentVersion&quot;:&quot;%{User-Agent}i&quot;}"/>
               
               
# 重啟 tomcat
[root@web01 ~]# /usr/local/tomcat/bin/shutdown.sh
[root@web01 ~]# /usr/local/tomcat/bin/startup.sh

 
# 配置收集新的 tomcat 日誌
[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_json_es.conf
input {
  file {
    path => "/usr/local/tomcat/logs/localhost_access_log.*.log"
    start_position => "beginning"
  }
}
output {
  elasticsearch {
    hosts => ["10.0.0.121:9200"]
    index => "tomcat_json_%{+YYYY-MM-dd}"
  }
}

收集 Tomcat 服務執行日誌(Catalina-log)

一般我們會收集 Tomcat 服務執行日誌(Catalina-log),當遇到報錯時,一條報錯會被分割成很多條資料,不方便檢視,解決方法:

①. — 修改 Tomcat 的 Catalina 日誌格式為 Json

​ — 1)開發修改輸出日誌為 Json

​ — 2)修改 Tomcat 配置,日誌格式為 Json

②. — 使用 Logstash 的 input 外掛下的 mutiline 模組

下面使用第二種方法,即 mutiline 模組實現服務執行日誌的切分

Mutiline 模組初識

# 使用 mutiline 模組
[root@web01 ~]# vim /etc/logstash/conf.d/test_mutiline.conf
input {
  stdin {
    codec => multiline {
	  # 匹配以 `[` 開頭
      pattern => "^\["
      # 匹配到
      negate => true
      # 向上合併,向下合併是 next
      what => "previous"
    }
  }
}
output {
  stdout {
    codec => "json"
  }
}

# 測試,輸入內容不會直接輸出,當遇到以 [ 開頭才會收集以上的日誌
[root@web01 ~]# logstash -f /etc/logstash/conf.d/test_mutiline.conf
......
...
]
A
B
C
D
[
{"tags":["multiline"],"host":"web01","@timestamp":"2020-08-14T02:30:33.906Z","message":"[\n]\nA\nB\nC\nD","@version":"1"}
a
b
c
d
e
f
g
[
{"tags":["multiline"],"host":"web01","@timestamp":"2020-08-14T02:28:58.590Z","message":"[\na\nb\nc\nd\ne\nf\ng","@version":"1"}

收集 Tomcat 錯誤日誌(Catalina-log)

# 收集 catalina 錯誤日誌
[root@web01 conf.d]# vim /etc/logstash/conf.d/catalina_out.conf
input {
  file {
    path => "/usr/local/tomcat/logs/catalina.*.log"
    start_position => "beginning"
    codec => multiline {
      pattern => "^\["
      negate => true
      what => "previous"
    }
  }
}

output {
  elasticsearch {
    hosts => ["10.0.0.121:9200"]
    index => "catalina_%{+YYYY-MM-dd}"
    codec => "json"
  }
}

追加 Tomcat 錯誤日誌(Catalina-log)

# 測試,手動新增一些錯誤的日誌,到 Catlina 日誌中 
[root@web01 ~]# cat tomcat_error.log >> /usr/local/tomcat/logs/catalina.2020-08-14.log

安裝 Nginx

[root@web01 ~]# yum install -y nginx

收集 Nginx 訪問日誌(Access-log)

# 配置 nginx 訪問日誌格式
[root@web01 ~]# vim /etc/nginx/nginx.conf
... ...
http {
    log_format  json  '{"@timestamp":"$time_iso8601",'
                      '"host":"$server_addr",'
                      '"clientip":"$remote_addr",'
                      '"size":$body_bytes_sent,'
                      '"responsetime":$request_time,'
                      '"upstreamtime":"$upstream_response_time",'
                      '"upstreamhost":"$upstream_addr",'
                      '"http_host":"$host",'
                      '"url":"$uri",'
                      '"referer":"$http_referer",'
                      '"agent":"$http_user_agent",'
                      '"status":"$status"}';

    access_log  /var/log/nginx/access.log  json;
... ...


# 配置收集訪問日誌
[root@web01 ~]# vim /etc/logstash/conf.d/nginx_json.conf
input {
  file {
    path => "/var/log/nginx/access.log"
    start_position => "beginning"
  }
}
output {
  elasticsearch {
    hosts => ["10.0.0.121:9200"]
    index => "nginx_json_%{+YYYY-MM-dd}"
  }
}

收集 Nginx 錯務日誌(Error-log)

Nginx 中的錯誤日誌不會像 Tomcat 中的錯誤日誌一樣分成很多行,只需要正常的配置,按行切分

將獲取日誌引數分離(方法一)

拆分 message 欄位

[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_json_es.conf
input {
  file {
    path => "/usr/local/tomcat/logs/localhost_access_log.*.log"
    start_position => "beginning"
  }
}

# 把收集到的資料進行處理
filter {
  json {
    # 將 message 欄位中的鍵值對,拆分成為索引文件中的欄位
    source => "message"
  }
}

output {
  elasticsearch {
    hosts => ["10.0.0.121:9200"]
    index => "tomcat_json_%{+YYYY-MM-dd}"
  }
}

移除 message 欄位

因為將 Message 欄位拆分到文件欄位後,就不需要 Message 欄位資料了,所以需要將 Message 欄位移除:

# message 資料已經拆分,資料還在,去掉 message 資料
filter {
  json {
    source => "message"
    remove_field => ["message"]
  }
}

將獲取日誌引數分離(方法二)

# 最方便的方法,只需要一行 codec => "json"
# file 中的日誌格式一定要是 Json 格式
[root@web01 ~]# vim /etc/logstash/conf.d/nginx_json.conf 
input {
  file {
    path => "/var/log/nginx/access.log"
    start_position => "beginning"
    codec => "json"
  }
}
output {
  elasticsearch {
    hosts => ["10.0.0.51:9200"]
    index => "nginx_json_%{+YYYY-MM-dd}"
  }
}