Wednesday, April 20, 2016

How to generate a JSON log from nginx?

Leave a Comment

I'm trying to generate a JSON log from nginx.

I'm aware of solutions like this one but some of the fields I want to log include user generated input (like HTTP headers) which need to be escaped properly.

I'm aware of the nginx changelog entries from Oct 2011 and May 2008 that say:

*) Change: now the 0x7F-0x1F characters are escaped as \xXX in an    access_log. *) Change: now the 0x00-0x1F, '"' and '\' characters are escaped as \xXX    in an access_log. 

but this still doesn't help since \xXX is invalid in a JSON string.

I've also looked at the HttpSetMiscModule module which has a set_quote_json_str directive, but this just seems to add \x22 around the strings which doesn't help.

Any idea for other solutions to log in JSON format from nginx?

2 Answers

Answers 1

You can try to use that one https://github.com/jiaz/nginx-http-json-log - addition module for Nginx.

Answers 2

You can try to use:

PS: The if parameter (1.7.0) enables conditional logging. A request will not be logged if the condition evaluates to “0” or an empty string:

map $status $http_referer{     ~\xXX  0;     default 1; }  access_log /path/to/access.log combined if=$http_referer; 

It’s a good idea to use a tool such as https://github.com/zaach/jsonlint to check your JSON data. You can test the output of your new logging format and make sure it’s real-and-proper JSON.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment