json - Openresty自定义json访问日志

标签 json nginx customization access-log openresty

我想将每个请求的 JSON 字符串写入访问日志,这样以后使用起来会更容易。

我正在使用 Lapis/Openresty 公开的 print(),但是我想覆盖时间戳、日志级别和属于 nginx 日志格式的其他信息。

如何覆盖它?

最佳答案

要使用 json 填充访问日志,您可以在 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})
    }
}

如果你想删除 nginx 错误日志中的默认前缀,这是不可能的,因为该格式是硬编码在 nginx 的源代码中的。

但是,您可以在 log_by_lua 上下文中以自定义格式向消费者提供数据。

关于json - Openresty自定义json访问日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35945619/

相关文章:

ubuntu - 用 nginx-push-stream-module 重新编译 Nginx?

nginx - 将我的Docker容器暴露于GCP上的外部IP

jquery - 通过 AJAX 请求执行操作 DNN MVC

python - 如何将 dict 转换为递归 defaultdict 并加载 JSON?

node.js - 带 SSL 的 Nginx PM2 NodeJS 反向代理提供 HTTP 504

azure - 是否可以自定义 Backlogs View

content-management-system - 寻找允许内容注入(inject)的免费 CMS

java - 将 Gson 数组转换为 Arraylist

javascript - 如何将存储在json文件中的td类获取到html表中(使用ajax)

Python 创建自定义魔术方法