caddyserver / transform-encoder

Log encoder module for custom log formats
Apache License 2.0
79 stars 17 forks source link

Combined log format #13

Closed ebanDev closed 2 years ago

ebanDev commented 2 years ago

Hi, I tried to make the combined (nginx) log format whit this plugin, but it doesn't seem to work :( Here is my template

format formatted {request>remote_addr} - - [{ts}] "{request>method} {request>uri} {request>proto}" {status size} "{request>headers>Referer} {request>headers>User-Agent}" {
        time_format "02/Jan/2006:15:04:05 -0700"
}

But the result is not as excepted :(

92.184.106.124:57788 - - [13/Mar/2022:08:27:06 +0000] GET /file.html HTTP/2.0 - ["https://domain.eu/"] ["Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0"]
mohammed90 commented 2 years ago

Oops, my bad. It needs slight adjustments. The correct template in Caddyfile is:

format formatted `{request>remote_addr} - {request>user_id} [{ts}] "{request>method} {request>uri} {request>proto}" {status} {size} "{request>headers>Referer>[0]}" "{request>headers>User-Agent>[0]}"`

So for you it should be

format formatted `{request>remote_addr} - {request>user_id} [{ts}] "{request>method} {request>uri} {request>proto}" {status} {size} "{request>headers>Referer>[0]}" "{request>headers>User-Agent>[0]}"` {
        time_format "02/Jan/2006:15:04:05 -0700"
}

Edit: here's the output with that exact config:

127.0.0.1 - - [1647204054.825556] "GET / HTTP/2.0" 200 20590 "-" "Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0"
127.0.0.1 - - [1647204054.9815192] "GET /favicon.ico HTTP/2.0" 404 0 "https://localhost/" "Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0"