0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ingressのログの正規表現を作ってみた

Last updated at Posted at 2021-03-18

以下のフォーマットでingressのログがデフォルト出力します
https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/log-format/

log_format upstreaminfo
    '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" "$http_user_agent" '
    '$request_length $request_time [$proxy_upstream_name] [$proxy_alternative_upstream_name] $upstream_addr '
    '$upstream_response_length $upstream_response_time $upstream_status $req_id';

これの正規表現を作ってみました。
うまくいっているかどうかはまだ検証中ですが、ご参考程度に

ログはこんな感じで出力されます

2021-03-09T05:56:19+09:00	{"message":"2xx.1xx.xx.xx- - [08/Mar/2021:20:56:19 +0000] \"GET /config/hoge?index=0 HTTP/1.1\" 400 256 \"-\" \"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:76.0) Gecko/20100101 Firefox/76.0\" 354 0.000 [] [] - - - - b6f884a586112950a00000d6bdea058b"}

作った正規表現

^(?<time_key>[^ ]*)\t*\"(?<host>[^ ]*) (?<blank>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] \\"(?<method>\S+[^\"]) (?<path>[^\"]*?) (?<protocol>[^ ]*) (?<code>[^ ]*) (?<size>[^ ]*) \\"(?<referer>[^ ]*)\\" \\"(?<agent>[^\"]*)\\" (?<request_length>[^\ ]*) (?<request_time>[^\ ]*) (?<proxy_upstream_name>[^\ ]*) (?<proxy_alternative_upstream_name>[^\ ]*) (?<upstream_addr>[^\ ]*) (?<upstream_response_length>[^\ ]*) (?<upstream_response_time>[^\ ]*) (?<upstream_status>[^\ ]*) (?<req_id>[^\"]*)

参考)
正規表現を作るのに便利
https://regex101.com/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?