Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions apisix/plugins/file-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local log_util = require("apisix.utils.log-util")
local core = require("apisix.core")
local ngx = ngx
local io_open = io.open
local log_util = require("apisix.utils.log-util")
local core = require("apisix.core")
local ngx = ngx
local io_open = io.open
local is_apisix_or, process = pcall(require, "resty.apisix.process")


local plugin_name = "file-logger"

local std_out_file = "/dev/stdout"

local schema = {
type = "object",
properties = {
path = {
type = "string"
},
include_resp_body = {type = "boolean", default = false},
log_to_stdout = { type = "boolean", default = false },
include_resp_body = { type = "boolean", default = false },
include_resp_body_expr = {
type = "array",
minItems = 1,
Expand All @@ -39,18 +39,16 @@ local schema = {
}
}
},
required = {"path"}
required = { "path" }
}


local metadata_schema = {
type = "object",
properties = {
log_format = log_util.metadata_schema_log_format
}
}


local _M = {
version = 0.1,
priority = 399,
Expand All @@ -59,15 +57,13 @@ local _M = {
metadata_schema = metadata_schema
}


function _M.check_schema(conf, schema_type)
if schema_type == core.schema.TYPE_METADATA then
return core.schema.check(metadata_schema, conf)
end
return core.schema.check(schema, conf)
end


local open_file_cache
if is_apisix_or then
-- TODO: switch to a cache which supports inactive time,
Expand Down Expand Up @@ -112,15 +108,29 @@ if is_apisix_or then
end
end

local function open_std_out(conf)
if not conf.log_to_stdout then
return
end
local std_out, err = io_open(std_out_file, "w")
if not std_out then
core.log.error("failed to open" .. std_out_file .. ", err: ", err)
return
end
std_out:setvbuf("no")
return std_out
end

local function write_file_data(conf, log_message)
local msg = core.json.encode(log_message)

local file, err
local file, std_out, err
if open_file_cache then
file, err = open_file_cache(conf)
std_out = open_std_out(conf)
else
file, err = io_open(conf.path, 'a+')
std_out = open_std_out(conf)
end

if not file then
Expand All @@ -136,6 +146,14 @@ local function write_file_data(conf, log_message)
core.log.error("failed to write file: ", conf.path, ", error info: ", err)
end

if conf.log_to_stdout and std_out then
ok, err = std_out:write(msg)
if not ok then
core.log.error("failed to write " .. std_out_file .. ", error info: ", err)
end
std_out:close()
end

-- file will be closed by gc, if open_file_cache exists
if not open_file_cache then
file:close()
Expand All @@ -152,5 +170,4 @@ function _M.log(conf, ctx)
write_file_data(conf, entry)
end


return _M
9 changes: 5 additions & 4 deletions docs/en/latest/plugins/file-logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ The `file-logger` Plugin is used to push log streams to a specific location.

## Attributes

| Name | Type | Required | Description |
| ---- | ------ | -------- | ------------- |
| path | string | True | Log file path. |
| include_resp_body | boolean | False | When set to `true` includes the response body in the log file. |
| Name | Type | Required | Description |
|------------------------|---------| -------- |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| path | string | True | Log file path. |
| include_resp_body | boolean | False | When set to `true` includes the response body in the log file. |
| include_resp_body_expr | array | False | When the `include_resp_body` attribute is set to `true`, use this to filter based on [lua-resty-expr](https://github.com/api7/lua-resty-expr). If present, only logs the response into file if the expression evaluates to `true`. |
| log_to_std | boolean | False | When set to `true` log to standard output |

## Metadata

Expand Down
11 changes: 6 additions & 5 deletions docs/zh/latest/plugins/file-logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ description: API 网关 Apache APISIX file-logger 插件可用于将日志数据

## 属性

| 名称 | 类型 | 必选项 | 描述 |
| ---------------- | ------- | ------ | ------------------------------------------------ |
| path | string | 是 | 自定义输出文件路径。例如:`logs/file.log`。 |
| include_resp_body | boolean | 否 | 当设置为 `true` 时,生成的文件包含响应体。 |
| include_resp_body_expr | array | 否 | 当 `include_resp_body` 属性设置为 `true` 时,使用该属性并基于 [lua-resty-expr](https://github.com/api7/lua-resty-expr) 进行过滤。 如果存在,则仅在表达式计算结果为 `true` 时记录响应。 |
| 名称 | 类型 | 必选项 | 描述 |
|------------------------|---------| ------ |---------------------------------------------------------------------------------------------------------------------------------------------|
| path | string | 是 | 自定义输出文件路径。例如:`logs/file.log`。 |
| include_resp_body | boolean | 否 | 当设置为 `true` 时,生成的文件包含响应体。 |
| include_resp_body_expr | array | 否 | 当 `include_resp_body` 属性设置为 `true` 时,使用该属性并基于 [lua-resty-expr](https://github.com/api7/lua-resty-expr) 进行过滤。 如果存在,则仅在表达式计算结果为 `true` 时记录响应。 |
| log_to_std | boolean | 否 | 当设置为 `true` 时,输出到标准输出中。 |

## 插件元数据设置

Expand Down