cll1778890283 opened a new issue, #12770: URL: https://github.com/apache/apisix/issues/12770
### Description --- --- Generated by EmmyLua(https://github.com/EmmyLua) --- Created by Administrator. --- DateTime: 2025/7/14 19:34 --- local plugin_name = "ale-request-logger" local core = require("apisix.core") local request_logger = require("apisix.plugins.ale-request-logger.init") local ngx = ngx local error = error local tostring = tostring local apps -- 插件元数据定义 local _M = { version = 0.1, priority = 99, -- 调整优先级为较低值 name = plugin_name, schema = request_logger.schema } -- 保持原有 schema 不变 function _M.apps() if not apps then return nil, nil end return apps.values, apps.conf_version end function _M.get_by_id(app_id) local app local apps = core.config.fetch_created_obj("/apps") if apps then app = apps:get(tostring(app_id)) end if not app then core.log.warn("failed to find app by id: ", app_id) return 404, nil end core.log.info("parsed app: ", core.json.delay_encode(app, true)) return nil, app.value end function _M.init_worker() core.log.warn("=======\n初始化方法开始执行===========\n") local err apps, err = core.config.new("/apps", { automatic = true, filter = function(app) core.log.info("filter app: ", core.json.delay_encode(app, true)) end, }) if not apps then error("failed to create etcd instance for fetching apps: " .. err) return end end function _M.access(conf, ctx) -- todo 测试etcd -- 测试引入其他模块打印 core.log.warn("引入其他模块成功", request_logger.TEST(conf)) if conf.custom_header then core.request.set_header(ctx, "X-Request-Logger", conf.custom_header) end -- 记录请求信息(如果配置了) if conf.log_request then -- 获取请求头(兼容所有版本) local headers = {} for k, v in pairs(ngx.req.get_headers()) do headers[k] = v end core.log.warn("[" .. plugin_name .. "] request headers: ", core.json.encode(headers)) -- 获取请求体(在 access 阶段无法可靠获取,仅记录是否有请求体) if method == "POST" or method == "PUT" or method == "PATCH" then core.log.warn("[" .. plugin_name .. "] request has body: ", ngx.req.has_body()) end end -- 如果想要获取body呢 local method = ngx.req.get_method() if conf.log_request_body and (method == "POST" or method == "PUT" or method == "PATCH") then ngx.req.read_body() local body = ngx.req.get_body_data() core.log.warn("[" .. plugin_name .. "] request body: ", body) if body then local body_json = core.json.decode(body) core.log.warn("[" .. plugin_name .. "] request body json: ", core.json.encode(body_json)) end end end -- 在响应体返回客户端前执行 function _M.body_filter(conf, ctx) if conf.log_response then -- 兼容所有版本的响应体获取 local chunk = ngx.arg[1] local eof = ngx.arg[2] -- 使用全局变量存储响应体片段(注意:可能占用大量内存) ctx.response_chunks = ctx.response_chunks or {} if chunk then table.insert(ctx.response_chunks, chunk) end -- 响应结束时合并并记录 if eof then local response_body = table.concat(ctx.response_chunks) if #response_body > 1024 then response_body = response_body:sub(1, 1024) .. " [truncated]" end core.log.warn("[" .. plugin_name .. "] response body: ", response_body) end end end -- 其他阶段保持不变... return _M -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
