Skip to content

Instantly share code, notes, and snippets.

@dhcmrlchtdj
Last active August 27, 2022 15:07
Show Gist options
  • Select an option

  • Save dhcmrlchtdj/2c16fed564773c10e83aa54936749074 to your computer and use it in GitHub Desktop.

Select an option

Save dhcmrlchtdj/2c16fed564773c10e83aa54936749074 to your computer and use it in GitHub Desktop.
nginx log in JSON with njs
function logJSON(r) {
const v = r.variables
return JSON.stringify({
time: v.time_iso8601,
id: v.request_id,
latency: Number(v.request_time),
remote: r.remoteAddress,
request: {
http_version: r.httpVersion,
protocol: v.scheme,
method: r.method,
hostname: v.host,
port: v.server_port,
pathname: r.uri,
query: r.args,
header: r.headersIn,
size: Number(v.request_length),
},
response: {
status: r.status,
header: r.headersOut,
size: Number(v.bytes_sent),
},
})
}
export default { logJSON }
load_module /usr/lib/nginx/modules/ngx_http_js_module.so;
http {
js_import logger from 'logger.js';
js_set $logJSON logger.logJSON;
log_format logJSON escape=none '$logJSON';
access_log /var/log/nginx/access.log logJSON;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment