2

I would like to write JSON strings on every request into the access logs, so it would be easier to consume it later.

I am using the print() exposed by Lapis/Openresty, however I would like to over ride the timestamp, log level and other information that is part of the nginx log format.

How can I override it?

1 Answers1

5

To fill access log with json, you can use something like this in your nginx.conf:

log_format mydef "$json_log";
access_log logs/access.log mydef;
server {
    ...
    set $json_log '';
    log_by_lua_block {
        local json = require "cjson"
        ngx.var.json_log = json.encode({my_json_data = 1})
    }
}

If you want to remove the default prefix in nginx error log, it is impossible yet since the format is hardcoded inside nginx's source.

However, you can provide your data in customized format during log_by_lua context to your consumer.

罗泽轩
  • 1,603
  • 2
  • 14
  • 19