Skip to content

Instantly share code, notes, and snippets.

@dgwynne
Created July 12, 2016 05:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgwynne/eb23f2214cb02be397386b6a2d7f6210 to your computer and use it in GitHub Desktop.
Save dgwynne/eb23f2214cb02be397386b6a2d7f6210 to your computer and use it in GitHub Desktop.
lua bunyan
local logfile = require("logfile")
local P = require("posix")
local cjson = require("cjson")
local cookies = require("http_cookies")
local _M = {
fatal = 60,
error = 50,
warn = 40,
info = 30,
debug = 20,
trace = 10,
_pid = P.getpid().pid,
_host = P.uname('%n'),
}
local http_versions = {
["HTTP/1.1"] = '1.1',
["HTTP/1.0"] = '1.0',
}
function _M.log()
local now = ngx.now()
local ms = (now * 1000.0) % 1000
local ts = P.strftime("%Y-%m-%dT%H:%M:%S", P.gmtime(now))
local str = string.format("%s %s://%s%s %s", ngx.req.get_method(),
ngx.var.scheme, ngx.var.host, ngx.var.request_uri, ngx.status)
local req = {
["scheme"] = ngx.var.scheme,
["method"] = ngx.req.get_method(),
["host"] = ngx.var.host,
["url"] = ngx.var.request_uri,
["headers"] = ngx.req.get_headers(),
["remoteAddress"] = ngx.var.remote_addr,
["username"] = ngx.var.http_x_uq_user, -- XXX
["ssl_cipher"] = ngx.var.ssl_cipher,
["bytes"] = ngx.var.request_length,
}
if (req.headers.cookie ~= nil) then
req["cookies"] = cookies.parse(req.headers.cookie)
end
if (http_versions[ngx.var.server_protocol] ~= nil) then
req["httpVersion"] = http_versions[ngx.var.server_protocol]
end
local res = {
["statusCode"] = ngx.status,
["headers"] = ngx.resp.get_headers(),
["bytes"] = ngx.var.body_bytes_sent,
}
local msg = {
["v"] = 0,
["hostname"] = _M._host,
["name"] = "nginx",
["pid"] = _M._pid,
["level"] = _M.info,
["time"] = string.format("%s.%.03dZ", ts, ms),
["duration"] = ngx.var.request_time,
["msg"] = str,
["req_id"] = res.headers['x-request-id'] or req.headers['x-request-id'],
["req"] = req,
["res"] = res,
["upstream"] = ngx.var.upstream_addr
}
logfile.write(cjson.encode(msg) .. "\n")
end
return _M
xdlg@ozone nginx$ cat lua/logfile.lua
local P = require("posix")
local B = require("bit32")
local _M = { }
local logfile = "/var/www/logs/bunyan.log";
local fd, error, code = P.open(logfile, B.bor(P.O_WRONLY, P.O_APPEND))
if fd == nil then
function _M.write(str)
return nil, "error", -1
end
else
function _M.write(str)
P.fcntl(fd, P.F_WRLCK)
local len, err, code = P.write(fd, str)
P.fcntl(fd, P.F_UNLCK)
return len, err, code
end
end
return _M
local P = require("posix")
local B = require("bit32")
local _M = { }
local logfile = "/var/www/logs/bunyan.log";
local fd, error, code = P.open(logfile, B.bor(P.O_WRONLY, P.O_APPEND))
if fd == nil then
function _M.write(str)
return nil, "error", -1
end
else
function _M.write(str)
P.fcntl(fd, P.F_WRLCK)
local len, err, code = P.write(fd, str)
P.fcntl(fd, P.F_UNLCK)
return len, err, code
end
end
return _M
log_by_lua_block {
local bunyan = require("bunyan")
bunyan.log()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment