spacewander commented on code in PR #8279:
URL: https://github.com/apache/apisix/pull/8279#discussion_r1030998915


##########
t/plugin/syslog.t:
##########
@@ -324,3 +324,90 @@ qr/sending a batch logs to 127.0.0.1:(\d+)/
 --- grep_error_log_out
 sending a batch logs to 127.0.0.1:5044
 sending a batch logs to 127.0.0.1:5045
+
+
+=== TEST 9: add log format
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/plugin_metadata/syslog',
+                 ngx.HTTP_PUT,
+                 [[{
+                    "log_format": {
+                        "host": "$host",
+                        "client_ip": "$remote_addr"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+
+
+
+=== TEST 10: Add route and Enable Syslog Plugin, batch_max_size=1
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "syslog": {
+                                "batch_max_size": 1,
+                                "disable": false,
+                                "flush_limit": 1,
+                                "host" : "127.0.0.1",
+                                "port" : 5050
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/hello"
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+
+
+
+=== TEST 11: hit route and report sys logger
+--- extra_init_by_lua
+    local syslog = require("apisix.plugins.syslog.init")
+    local json = require("apisix.core.json")
+    local log = require("apisix.core.log")
+    local old_f = syslog.push_entry
+    syslog.push_entry = function(conf, ctx, entry)
+        log.info("syslog-log-format => " ..  json.encode(entry))
+        return old_f(conf, ctx, entry)
+    end
+--- request
+GET /hello
+--- response_body
+hello world
+--- wait: 0.5
+--- no_error_log
+[error]
+--- error_log eval
+qr/syslog-log-format.*\{.*"host":"localhost"/

Review Comment:
   I run this test locally and it can pass even without setting metadata, 
because the default log format already contains `"host":"localhost"` in
   
https://github.com/apache/apisix/blob/ab4fe8837da6aba4aca868291c0ea9f3876fa829/apisix/utils/log-util.lua#L128



##########
docs/en/latest/plugins/syslog.md:
##########
@@ -49,6 +49,40 @@ Logs can be set as JSON objects.
 
 This Plugin supports using batch processors to aggregate and process entries 
(logs/data) in a batch. This avoids the need for frequently submitting the 
data. The batch processor submits data every `5` seconds or when the data in 
the queue reaches `1000`. See [Batch 
Processor](../batch-processor.md#configuration) for more information or setting 
your custom configuration.
 
+## Metadata
+
+You can also set the format of the logs by configuring the Plugin metadata. 
The following configurations are available:
+
+| Name       | Type   | Required | Default                                     
                                  | Description                                 
                                                                                
                                                                                
                                            |
+| ---------- | ------ | -------- | 
----------------------------------------------------------------------------- | 
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 |
+| log_format | object | False    | {"host": "$host", "@timestamp": 
"$time_iso8601", "client_ip": "$remote_addr"} | Log format declared as key 
value pairs in JSON format. Values only support strings. 
[APISIX](../apisix-variable.md) or 
[Nginx](http://nginx.org/en/docs/varindex.html) variables can be used by 
prefixing the string with `$`. |
+
+:::info IMPORTANT
+
+Configuring the Plugin metadata is global in scope. This means that it will 
take effect on all Routes and Services which use the `syslog` Plugin.
+
+:::
+
+The example below shows how you can configure through the Admin API:
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/plugin_metadata/syslog -H 'X-API-KEY: 
edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '

Review Comment:
   ```suggestion
   curl http://127.0.0.1:9180/apisix/admin/plugin_metadata/syslog -H 
'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
   ```



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