spacewander commented on a change in pull request #5831:
URL: https://github.com/apache/apisix/pull/5831#discussion_r785441792



##########
File path: apisix/plugins/file-logger.lua
##########
@@ -0,0 +1,99 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- 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 plugin       =   require("apisix.plugin")
+local ngx          =   ngx
+local io_open      =   io.open
+
+
+local plugin_name  =   "file-logger"
+local file
+
+
+local schema = {
+    type = "object",
+    properties = {
+        path = {
+            type = "string",
+            pattern = [[^[^*&%%\`]+$]],
+            err = "not a valid filename"

Review comment:
       We don't have `err` in the jsonschema. BTW, we don't need to verify the 
path with regex? Different filesystems will have different requirements.

##########
File path: docs/zh/latest/plugins/file-logger.md
##########
@@ -0,0 +1,119 @@
+---
+title: file-logger
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## 目录
+
+- [**定义**](#定义)
+- [**属性列表**](#属性)
+- [**如何开启**](#如何启用)
+- [**测试插件**](#测试插件)
+- [**插件元数据设置**](#插件元数据设置)
+- [**禁用插件**](#禁用插件)
+
+## 定义
+
+`file-logger` 是一个插件,可将 Log 数据流推送到指定位置,例如,可以自定义输出路径:`logs/file.log`。
+
+## 属性列表
+
+| 名称             | 类型    | 必选项 | 默认值        | 有效值  | 描述                        
                     |
+| ---------------- | ------- | ------ | ------------- | ------- | 
------------------------------------------------ |
+| path              | string  | 必须   |               |         | 自定义输出文件路径     
              |
+
+## 如何开启
+
+这是有关如何为特定路由启用 `file-logger` 插件的示例。
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: 
edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+  "plugins": {
+    "file-logger": {
+      "path": "logs/file.log"
+    }
+  },
+  "upstream": {
+    "type": "roundrobin",
+    "nodes": {
+      "127.0.0.1:9001": 1
+    }
+  },
+  "uri": "/hello"
+}'
+```
+
+## 测试插件
+
+> 成功:
+
+```shell
+$ curl -i http://127.0.0.1:9080/hello
+HTTP/1.1 200 OK
+...
+hello, world
+```
+
+然后你可以在对应的 `logs` 目录下找到 `file.log` 文件,且该日志中现有字段的 `route_id` 被删除。初次接受请求时会额外生成一个 
`file_pointer` 文件,它用来保存打开目标输出文件的指针。
+
+> 通过 control API 重新打开该日志文件
+
+```shell
+$ curl -i http://localhost:9090/v1/plugin/file-logger/reopen -X PUT
+```
+
+然后下次请求时,会将之前缓冲区所有的日志数据刷新,到我们刚刚自定义的输出日志文件中。
+
+## 插件元数据设置
+
+| 名称             | 类型    | 必选项 | 默认值        | 有效值  | 描述                        
                     |
+| ---------------- | ------- | ------ | ------------- | ------- | 
------------------------------------------------ |
+| log_format       | object  | 可选   | {"host": "$host", "@timestamp": 
"$time_iso8601", "client_ip": "$remote_addr"} |         | 以 JSON 
格式的键值对来声明日志格式。对于值部分,仅支持字符串。如果是以 `$` 开头,则表明是要获取 __APISIX__ 变量或 [Nginx 
内置变量](http://nginx.org/en/docs/varindex.html)。特别的,**该设置是全局生效的**,意味着指定 
log_format 后,将对所有绑定 file-logger 的 Route 或 Service 生效。 |
+
+**APISIX 变量**

Review comment:
       Please update the doc according to the latest http-logger.

##########
File path: apisix/plugins/file-logger.lua
##########
@@ -0,0 +1,99 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- 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 plugin       =   require("apisix.plugin")
+local ngx          =   ngx
+local io_open      =   io.open
+
+
+local plugin_name  =   "file-logger"
+local file
+
+
+local schema = {
+    type = "object",
+    properties = {
+        path = {
+            type = "string",
+            pattern = [[^[^*&%%\`]+$]],
+            err = "not a valid filename"
+        },
+    },
+    required = {"path"}
+}
+
+
+local metadata_schema = {
+    type = "object",
+    properties = {
+        log_format = log_util.metadata_schema_log_format
+    }
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 399,
+    name = plugin_name,
+    schema = schema,
+    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
+
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return nil, err
+    end
+    return log_util.check_log_schema(conf)
+end
+
+
+local function write_file_data(conf, log_message)
+    local msg = core.json.encode(log_message) .. "\n"
+
+    if not file then
+        file = io_open(conf.path, 'a+')

Review comment:
       ```suggestion
           local file, err = io_open(conf.path, 'a+')
   ```
   is enough? And remember to check the err.

##########
File path: apisix/plugins/file-logger.lua
##########
@@ -0,0 +1,99 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- 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 plugin       =   require("apisix.plugin")
+local ngx          =   ngx
+local io_open      =   io.open
+
+
+local plugin_name  =   "file-logger"

Review comment:
       ```suggestion
   local plugin_name = "file-logger"
   ```

##########
File path: apisix/plugins/file-logger.lua
##########
@@ -0,0 +1,99 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- 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 plugin       =   require("apisix.plugin")
+local ngx          =   ngx
+local io_open      =   io.open
+
+
+local plugin_name  =   "file-logger"
+local file
+
+
+local schema = {
+    type = "object",
+    properties = {
+        path = {
+            type = "string",
+            pattern = [[^[^*&%%\`]+$]],
+            err = "not a valid filename"
+        },
+    },
+    required = {"path"}
+}
+
+
+local metadata_schema = {
+    type = "object",
+    properties = {
+        log_format = log_util.metadata_schema_log_format
+    }
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 399,
+    name = plugin_name,
+    schema = schema,
+    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
+
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return nil, err
+    end
+    return log_util.check_log_schema(conf)
+end
+
+
+local function write_file_data(conf, log_message)
+    local msg = core.json.encode(log_message) .. "\n"
+
+    if not file then
+        file = io_open(conf.path, 'a+')
+    end
+
+    file:write(msg)

Review comment:
       Ditto




-- 
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]


Reply via email to