LoginSignup
7
7

More than 5 years have passed since last update.

GKE上のnginxのアクセスログフォーマットをStackdriverのフォーマットにする

Posted at

すごく雑に書きます
スクリーンショット 2019-02-08 19.37.11.png

https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
このフォーマットに当てはまるとStackdriverのコンソール上でタグ付け表示などがされ、フィルタリングしやすくなります

やり方

log_format acc_json_format escape=json '{"severity": "INFO",'
    '"timestamp": "$time_iso8601",'
    '"httpRequest": {'
            '"requestMethod": "$request_method",'
            '"requestUrl": "$request",'
            '"requestSize": "$request_length",'
            '"status": $status,'
            '"responseSize": "$body_bytes_sent",'
            '"userAgent": "$http_user_agent",'
            '"remoteIp": "$remote_addr",'
            '"serverIp": "$server_addr",'
            '"referer": "$http_referer",'
            '"latency": "$upstream_response_time",'
            '"cacheLookup": false,'
            '"cacheHit": false,'
            '"cacheValidatedWithOriginServer": false,'
            '"cacheFillBytes": "",'
            '"protocol": "$server_protocol"'
    '},'
    # その他の情報
    '"uuid": "$x_request_id",'
    '"host": "$http_host",'
    '"user_id": "$http_x_user_id",'
    '"session_id": "$http_x_session_id"}';

access_log  logs/access.log acc_json_format;

httpRequestに当てはまらないフィールドもjsonPayloadとして記録されるので便利です

7
7
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
7
7