Skip to content

Instantly share code, notes, and snippets.

@agladysh
Last active December 12, 2015 02:09
Show Gist options
  • Save agladysh/4696834 to your computer and use it in GitHub Desktop.
Save agladysh/4696834 to your computer and use it in GitHub Desktop.
Released under MIT license
-- For nginx combined log format:
-- http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
local query = '^'
.. '(%d+%.%d+%.%d+%.%d+)%s' -- IP
.. '%-%s' -- separator
.. '(.-)%s' -- Remote user, TODO: Support
.. '%[(.-)%]%s' -- Date, time
.. '"(.-)"%s' -- request
.. '(%d+)%s' -- HTTP status code
.. '(%d+)%s' -- body_bytes_sent
.. '"(.-)"%s' -- Referrer
.. '"(.-)"' -- User agent
-- Ignoring anything at the end of the string
for l in io.lines() do
local ip,
user,
datetime,
request,
status,
body_bytes_sent,
referrer,
user_agent
= l:match(query)
local request_method, request_url, request_protocol = "-", "-", "-"
if ip and request ~= "-" then
request_method, request_url, request_protocol = request:match(
"^(%w+)%s(.-)%s(.-)$"
)
end
if ip and request_method then
io.write(
ip, ",",
user, ",",
datetime, ",",
request_method, ",",
'"', request_url, '"', ",", -- should not contain '"'.
request_protocol, ",",
status, ",",
body_bytes_sent, ",",
'"', referrer, '"', ",", -- should not contain '"'.
'"', user_agent, '"', ",", -- should not contain '"'.
"\n"
)
else
io.stderr:write("BAD:", l, "\n")
io.stderr:flush()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment