You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/10/21 09:13:55 UTC

[GitHub] [apisix] dabue opened a new pull request #2488: [faeture]add error-log-logger plugin

dabue opened a new pull request #2488:
URL: https://github.com/apache/apisix/pull/2488


   ### What this PR does / why we need it:
   <!--- Why is this change required? What problem does it solve? -->
   <!--- If it fixes an open issue, please link to the issue here. -->
   add the plugin to collect error log and report to remote server .
   fix #2410
   ### Pre-submission checklist:
   
   * [ ] Did you explain what problem does this PR solve? Or what new features have been added?
   Add the plugin to collect error log and report to remote server . 
   This plugin is  global and starts at the init function, I used a default host and port to send the error log at this version because I haven't found the way to get the conf of plugin. I want a help .
   * [ ] Have you added corresponding test cases?
   not yet.
   
   


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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] dabue commented on a change in pull request #2488: feat: add error-log-logger plugin

Posted by GitBox <gi...@apache.org>.
dabue commented on a change in pull request #2488:
URL: https://github.com/apache/apisix/pull/2488#discussion_r511593697



##########
File path: t/plugin/error-log-logger.t
##########
@@ -0,0 +1,122 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    my $stream_single_server = <<_EOC_;
+    # fake server, only for test
+    server {
+        listen 33333;
+
+        content_by_lua_block {
+            local exiting = ngx.worker.exiting
+            local sock, err = ngx.req.socket(true)
+            if not sock then
+                ngx.log(ngx.WARN, "socket error:" .. err)
+                return
+            end
+            sock:settimeout(30 * 1000)
+            while(not exiting())
+            do
+                local data, err =  sock:receive()
+                if (data) then
+                    ngx.log(ngx.INFO, "[Server] receive data:" .. data)
+                else 
+                    if err ~= "timeout" then
+                        ngx.log(ngx.WARN, "socket error:" .. err)
+                        return
+                    end
+                end
+            end
+        }
+    }
+_EOC_
+
+    $block->set_value("stream_config", $stream_single_server);
+});
+
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    my $stream_default_server = <<_EOC_;
+	    content_by_lua_block {
+	    	ngx.log(ngx.INFO, "a stream server")
+	    }
+_EOC_
+
+    $block->set_value("stream_server_config", $stream_default_server);
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: log a warn level message
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            core.log.warn("this is a warning message for test.\n")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/\[Server\] receive data:.*this is a warning message for test./
+--- wait: 1
+
+
+
+=== TEST 2: log an error level message
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            core.log.error("this is an error message for test.\n")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/\[Server\] receive data:.*this is an error message for test./
+--- wait: 1
+
+
+
+=== TEST 3: log an info level message

Review comment:
       This plugin is global, and it's attributes are in the config.yaml which should be created before the test cases  are executed. I've not found a way to include the right address case and the wrong address case in a single '.t' file .




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] dabue commented on a change in pull request #2488: feat: add error-log-logger plugin

Posted by GitBox <gi...@apache.org>.
dabue commented on a change in pull request #2488:
URL: https://github.com/apache/apisix/pull/2488#discussion_r513924200



##########
File path: apisix/plugins/error-log-logger.lua
##########
@@ -0,0 +1,235 @@
+--
+-- 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 core = require("apisix.core")
+local plugin_name = "error-log-logger"
+local errlog = require "ngx.errlog"
+local batch_processor = require("apisix.utils.batch-processor")
+local table = core.table
+local ngx = ngx
+local tcp = ngx.socket.tcp
+local select = select
+local type = type
+local string = string
+local tostring = tostring
+local buffers
+local timer
+local schema = {
+    type = "object",
+    properties = {
+        host = {type = "string"},
+        port = {type = "integer", minimum = 0},
+        tls = {type = "boolean", default = false},
+        tls_options = {type = "string"},
+        timeout = {type = "integer", minimum = 1, default= 3},
+        keepalive = {type = "integer", minimum = 1, default= 30},
+        name = {type = "string", default = "tcp logger"},
+        level = {type = "string", default = "WARN"},
+        batch_max_size = {type = "integer", minimum = 0, default = 1000},
+        max_retry_count = {type = "integer", minimum = 0, default = 0},
+        retry_delay = {type = "integer", minimum = 0, default = 1},
+        buffer_duration = {type = "integer", minimum = 1, default = 60},
+        inactive_timeout = {type = "integer", minimum = 1, default = 5},
+    },
+    additionalProperties = false,
+}
+
+local log_level = {
+    STDERR =    ngx.STDERR,
+    EMERG  =    ngx.EMERG,
+    ALERT  =    ngx.ALERT,
+    CRIT   =    ngx.CRIT,
+    ERR    =    ngx.ERR,
+    ERROR  =    ngx.ERR,
+    WARN   =    ngx.WARN,
+    NOTICE =     ngx.NOTICE,
+    INFO   =    ngx.INFO,
+    DEBUG  =    ngx.DEBUG
+}
+
+local config = {
+    name = plugin_name,
+    timeout = 3,
+    keepalive = 30,
+    level = "WARN",
+    tls = false,
+    retry_delay = 1,
+    batch_max_size = 1000,
+    max_retry_count = 0,
+    buffer_duration = 60,
+    inactive_timeout = 5,
+}
+local _M = {
+    version = 0.1,
+    priority = 1091,
+    name = plugin_name,
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+local function try_attr(t, ...)
+    local count = select('#', ...)
+    for i = 1, count do
+        local attr = select(i, ...)
+        t = t[attr]
+        if type(t) ~= "table" then
+            return false
+        end
+    end
+
+    return true
+end
+
+local function load_attr()
+    local local_conf = core.config.local_conf()
+    if try_attr(local_conf, "plugin_attr", plugin_name) then
+        local attr = local_conf.plugin_attr[plugin_name]
+        config.host = attr.host

Review comment:
       has updated it 




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] membphis commented on a change in pull request #2488: feat: add error-log-logger plugin

Posted by GitBox <gi...@apache.org>.
membphis commented on a change in pull request #2488:
URL: https://github.com/apache/apisix/pull/2488#discussion_r513495284



##########
File path: apisix/plugins/error-log-logger.lua
##########
@@ -0,0 +1,235 @@
+--
+-- 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 core = require("apisix.core")
+local plugin_name = "error-log-logger"
+local errlog = require "ngx.errlog"
+local batch_processor = require("apisix.utils.batch-processor")
+local table = core.table
+local ngx = ngx
+local tcp = ngx.socket.tcp
+local select = select
+local type = type
+local string = string
+local tostring = tostring
+local buffers
+local timer
+local schema = {
+    type = "object",
+    properties = {
+        host = {type = "string"},
+        port = {type = "integer", minimum = 0},
+        tls = {type = "boolean", default = false},
+        tls_options = {type = "string"},
+        timeout = {type = "integer", minimum = 1, default= 3},
+        keepalive = {type = "integer", minimum = 1, default= 30},
+        name = {type = "string", default = "tcp logger"},
+        level = {type = "string", default = "WARN"},
+        batch_max_size = {type = "integer", minimum = 0, default = 1000},
+        max_retry_count = {type = "integer", minimum = 0, default = 0},
+        retry_delay = {type = "integer", minimum = 0, default = 1},
+        buffer_duration = {type = "integer", minimum = 1, default = 60},
+        inactive_timeout = {type = "integer", minimum = 1, default = 5},
+    },
+    additionalProperties = false,
+}
+
+local log_level = {
+    STDERR =    ngx.STDERR,
+    EMERG  =    ngx.EMERG,
+    ALERT  =    ngx.ALERT,
+    CRIT   =    ngx.CRIT,
+    ERR    =    ngx.ERR,
+    ERROR  =    ngx.ERR,
+    WARN   =    ngx.WARN,
+    NOTICE =     ngx.NOTICE,
+    INFO   =    ngx.INFO,
+    DEBUG  =    ngx.DEBUG
+}
+
+local config = {
+    name = plugin_name,
+    timeout = 3,
+    keepalive = 30,
+    level = "WARN",
+    tls = false,
+    retry_delay = 1,
+    batch_max_size = 1000,
+    max_retry_count = 0,
+    buffer_duration = 60,
+    inactive_timeout = 5,
+}
+local _M = {
+    version = 0.1,
+    priority = 1091,
+    name = plugin_name,
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+local function try_attr(t, ...)
+    local count = select('#', ...)
+    for i = 1, count do
+        local attr = select(i, ...)
+        t = t[attr]
+        if type(t) ~= "table" then
+            return false
+        end
+    end
+
+    return true
+end
+
+local function load_attr()
+    local local_conf = core.config.local_conf()
+    if try_attr(local_conf, "plugin_attr", plugin_name) then
+        local attr = local_conf.plugin_attr[plugin_name]
+        config.host = attr.host

Review comment:
       this is not a good way to set the default value. 
   
   we should use JSONSchema to check if it is valid and set the default value.
   
   you can take a look at this code: 
   
   https://github.com/apache/apisix/blob/master/apisix/plugins/http-logger.lua#L58-L71

##########
File path: apisix/plugins/error-log-logger.lua
##########
@@ -0,0 +1,235 @@
+--
+-- 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 core = require("apisix.core")
+local plugin_name = "error-log-logger"
+local errlog = require "ngx.errlog"
+local batch_processor = require("apisix.utils.batch-processor")
+local table = core.table
+local ngx = ngx
+local tcp = ngx.socket.tcp
+local select = select
+local type = type
+local string = string
+local tostring = tostring
+local buffers
+local timer
+local schema = {
+    type = "object",
+    properties = {
+        host = {type = "string"},
+        port = {type = "integer", minimum = 0},
+        tls = {type = "boolean", default = false},
+        tls_options = {type = "string"},
+        timeout = {type = "integer", minimum = 1, default= 3},
+        keepalive = {type = "integer", minimum = 1, default= 30},
+        name = {type = "string", default = "tcp logger"},
+        level = {type = "string", default = "WARN"},
+        batch_max_size = {type = "integer", minimum = 0, default = 1000},
+        max_retry_count = {type = "integer", minimum = 0, default = 0},
+        retry_delay = {type = "integer", minimum = 0, default = 1},
+        buffer_duration = {type = "integer", minimum = 1, default = 60},
+        inactive_timeout = {type = "integer", minimum = 1, default = 5},
+    },
+    additionalProperties = false,
+}
+
+local log_level = {
+    STDERR =    ngx.STDERR,
+    EMERG  =    ngx.EMERG,
+    ALERT  =    ngx.ALERT,
+    CRIT   =    ngx.CRIT,
+    ERR    =    ngx.ERR,
+    ERROR  =    ngx.ERR,
+    WARN   =    ngx.WARN,
+    NOTICE =     ngx.NOTICE,
+    INFO   =    ngx.INFO,
+    DEBUG  =    ngx.DEBUG
+}
+
+local config = {
+    name = plugin_name,
+    timeout = 3,
+    keepalive = 30,
+    level = "WARN",
+    tls = false,
+    retry_delay = 1,
+    batch_max_size = 1000,
+    max_retry_count = 0,
+    buffer_duration = 60,
+    inactive_timeout = 5,
+}
+local _M = {
+    version = 0.1,
+    priority = 1091,
+    name = plugin_name,
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+local function try_attr(t, ...)
+    local count = select('#', ...)
+    for i = 1, count do
+        local attr = select(i, ...)
+        t = t[attr]
+        if type(t) ~= "table" then
+            return false
+        end
+    end
+
+    return true
+end
+
+local function load_attr()
+    local local_conf = core.config.local_conf()
+    if try_attr(local_conf, "plugin_attr", plugin_name) then
+        local attr = local_conf.plugin_attr[plugin_name]
+        config.host = attr.host
+        config.port = attr.port
+        config.level = attr.level or config.level
+        config.timeout = attr.timeout or config.timeout
+        config.keepalive = attr.keepalive or config.keepalive
+        config.tls = attr.tls or config.tls
+        config.tls_options = attr.tls_options
+        config.retry_delay = attr.retry_delay or config.retry_delay
+        config.batch_max_size = attr.batch_max_size or config.batch_max_size
+        config.max_retry_count = attr.max_retry_count or config.max_retry_count
+        config.buffer_duration = attr.buffer_duration or config.buffer_duration
+        config.inactive_timeout = attr.inactive_timeout or config.inactive_timeout
+    end
+end
+
+local function send_to_server(data)
+    local res = false
+    local err_msg
+    local sock, soc_err = tcp()
+
+    if not sock then
+        err_msg = "failed to init the socket " .. soc_err
+        return res, err_msg
+    end
+
+    sock:settimeout(config.timeout*1000)

Review comment:
       style: need a space between the arguments

##########
File path: t/plugin/error-log-logger.t
##########
@@ -0,0 +1,122 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    my $stream_single_server = <<_EOC_;
+    # fake server, only for test
+    server {
+        listen 33333;
+
+        content_by_lua_block {
+            local exiting = ngx.worker.exiting
+            local sock, err = ngx.req.socket(true)
+            if not sock then
+                ngx.log(ngx.WARN, "socket error:" .. err)
+                return
+            end
+            sock:settimeout(30 * 1000)
+            while(not exiting())
+            do
+                local data, err =  sock:receive()
+                if (data) then
+                    ngx.log(ngx.INFO, "[Server] receive data:" .. data)
+                else 
+                    if err ~= "timeout" then
+                        ngx.log(ngx.WARN, "socket error:" .. err)
+                        return
+                    end
+                end
+            end
+        }
+    }
+_EOC_
+
+    $block->set_value("stream_config", $stream_single_server);
+});
+
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    my $stream_default_server = <<_EOC_;
+	    content_by_lua_block {
+	    	ngx.log(ngx.INFO, "a stream server")
+	    }
+_EOC_
+
+    $block->set_value("stream_server_config", $stream_default_server);
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: log a warn level message
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            core.log.warn("this is a warning message for test.\n")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/\[Server\] receive data:.*this is a warning message for test./
+--- wait: 1
+
+
+
+=== TEST 2: log an error level message
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            core.log.error("this is an error message for test.\n")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/\[Server\] receive data:.*this is an error message for test./
+--- wait: 1
+
+
+
+=== TEST 3: log an info level message

Review comment:
       nice ^_^

##########
File path: apisix/plugins/error-log-logger.lua
##########
@@ -0,0 +1,235 @@
+--
+-- 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 core = require("apisix.core")
+local plugin_name = "error-log-logger"
+local errlog = require "ngx.errlog"
+local batch_processor = require("apisix.utils.batch-processor")
+local table = core.table
+local ngx = ngx
+local tcp = ngx.socket.tcp
+local select = select
+local type = type
+local string = string
+local tostring = tostring
+local buffers
+local timer
+local schema = {
+    type = "object",
+    properties = {
+        host = {type = "string"},
+        port = {type = "integer", minimum = 0},
+        tls = {type = "boolean", default = false},
+        tls_options = {type = "string"},
+        timeout = {type = "integer", minimum = 1, default= 3},
+        keepalive = {type = "integer", minimum = 1, default= 30},
+        name = {type = "string", default = "tcp logger"},
+        level = {type = "string", default = "WARN"},
+        batch_max_size = {type = "integer", minimum = 0, default = 1000},
+        max_retry_count = {type = "integer", minimum = 0, default = 0},
+        retry_delay = {type = "integer", minimum = 0, default = 1},
+        buffer_duration = {type = "integer", minimum = 1, default = 60},
+        inactive_timeout = {type = "integer", minimum = 1, default = 5},
+    },
+    additionalProperties = false,
+}
+
+local log_level = {
+    STDERR =    ngx.STDERR,
+    EMERG  =    ngx.EMERG,
+    ALERT  =    ngx.ALERT,
+    CRIT   =    ngx.CRIT,
+    ERR    =    ngx.ERR,
+    ERROR  =    ngx.ERR,
+    WARN   =    ngx.WARN,
+    NOTICE =     ngx.NOTICE,
+    INFO   =    ngx.INFO,
+    DEBUG  =    ngx.DEBUG
+}
+
+local config = {
+    name = plugin_name,
+    timeout = 3,
+    keepalive = 30,
+    level = "WARN",
+    tls = false,
+    retry_delay = 1,
+    batch_max_size = 1000,
+    max_retry_count = 0,
+    buffer_duration = 60,
+    inactive_timeout = 5,
+}
+local _M = {
+    version = 0.1,
+    priority = 1091,
+    name = plugin_name,
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+local function try_attr(t, ...)
+    local count = select('#', ...)
+    for i = 1, count do
+        local attr = select(i, ...)
+        t = t[attr]
+        if type(t) ~= "table" then
+            return false
+        end
+    end
+
+    return true
+end
+
+local function load_attr()
+    local local_conf = core.config.local_conf()
+    if try_attr(local_conf, "plugin_attr", plugin_name) then
+        local attr = local_conf.plugin_attr[plugin_name]
+        config.host = attr.host
+        config.port = attr.port
+        config.level = attr.level or config.level
+        config.timeout = attr.timeout or config.timeout
+        config.keepalive = attr.keepalive or config.keepalive
+        config.tls = attr.tls or config.tls
+        config.tls_options = attr.tls_options
+        config.retry_delay = attr.retry_delay or config.retry_delay
+        config.batch_max_size = attr.batch_max_size or config.batch_max_size
+        config.max_retry_count = attr.max_retry_count or config.max_retry_count
+        config.buffer_duration = attr.buffer_duration or config.buffer_duration
+        config.inactive_timeout = attr.inactive_timeout or config.inactive_timeout
+    end
+end
+
+local function send_to_server(data)
+    local res = false
+    local err_msg
+    local sock, soc_err = tcp()
+
+    if not sock then
+        err_msg = "failed to init the socket " .. soc_err
+        return res, err_msg
+    end
+
+    sock:settimeout(config.timeout*1000)
+
+    local ok, err = sock:connect(config.host, config.port)
+    if not ok then
+        err_msg = "failed to connect the TCP server: host[" .. config.host
+                  .. "] port[" .. tostring(config.port) .. "] err: " .. err
+        return res, err_msg
+    end
+
+    if config.tls then
+        ok, err = sock:sslhandshake(true, config.tls_options, false)
+        if not ok then
+            return false, "failed to to perform TLS handshake to TCP server: host["
+                          .. config.host .. "] port[" .. tostring(config.port) .. "] err: " .. err
+        end
+    end
+
+    local bytes, err = sock:send(data)
+    if not bytes then
+        sock:close()
+        err_msg = "failed to send data to TCP server: host[" .. config.host
+                  .. "] port[" .. tostring(config.port) .. "] err: " .. err
+        return res, err_msg
+    end
+
+    sock:setkeepalive(config.keepalive * 1000)
+    return true
+end
+
+local function process()
+    local entries = {}
+    local logs = errlog.get_logs(10)
+    while ( logs and #logs>0 ) do
+        for i = 1, #logs, 3 do
+            table.insert(entries, logs[i + 2])
+        end
+        logs = errlog.get_logs(10)
+    end
+    if #entries == 0 then
+        return
+    end
+    local log_buffer = buffers[config.id]
+    if log_buffer then
+        for i = 1, #entries do
+            log_buffer:push(entries[i])
+        end
+        return
+    end
+    -- Generate a function to be executed by the batch processor
+    local func = function(entries)
+        return send_to_server(entries)
+    end
+    local config_bat = {
+        name = config.name,
+        retry_delay = config.retry_delay,
+        batch_max_size = config.batch_max_size,
+        max_retry_count = config.max_retry_count,
+        buffer_duration = config.buffer_duration,
+        inactive_timeout = config.inactive_timeout,
+    }
+
+    local err
+    log_buffer, err = batch_processor:new(func, config_bat)
+
+    if not log_buffer then
+        core.log.error("error when creating the batch processor: ", err)
+        return
+    end
+    buffers[config.id] = log_buffer
+    for i = 1, #entries do
+        log_buffer:push(entries[i])
+    end
+
+end
+function _M.init()
+    if ngx.get_phase() ~= "init" and ngx.get_phase() ~= "init_worker"  then
+        return
+    end
+    buffers = {}

Review comment:
       style: add a blank line before this line

##########
File path: conf/config-default.yaml
##########
@@ -208,3 +209,9 @@ plugin_attr:
   log-rotate:
     interval: 3600    # rotate interval (unit: second)
     max_kept: 168     # max number of log files will be kept
+#  error-log-logger:

Review comment:
       we can set those by `plugin_metadata` via Admin API.
   
   https://github.com/apache/apisix/blob/master/apisix/plugins/http-logger.lua#L58-L71

##########
File path: t/plugin/error-log-logger.t
##########
@@ -0,0 +1,122 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    my $stream_single_server = <<_EOC_;
+    # fake server, only for test
+    server {
+        listen 33333;
+
+        content_by_lua_block {
+            local exiting = ngx.worker.exiting
+            local sock, err = ngx.req.socket(true)
+            if not sock then
+                ngx.log(ngx.WARN, "socket error:" .. err)
+                return
+            end
+            sock:settimeout(30 * 1000)
+            while(not exiting())
+            do
+                local data, err =  sock:receive()
+                if (data) then
+                    ngx.log(ngx.INFO, "[Server] receive data:" .. data)
+                else 
+                    if err ~= "timeout" then
+                        ngx.log(ngx.WARN, "socket error:" .. err)
+                        return
+                    end
+                end
+            end
+        }
+    }
+_EOC_
+
+    $block->set_value("stream_config", $stream_single_server);
+});
+
+
+add_block_preprocessor(sub {

Review comment:
       one `add_block_preprocessor ` is enough for your case.

##########
File path: bin/apisix
##########
@@ -224,6 +224,8 @@ http {
     lua_ssl_verify_depth 5;
     ssl_session_timeout 86400;
 
+    lua_capture_error_log     128k; # cache for  capture the error log

Review comment:
       only set this directive when the user enabled `error-log-logger` plugin




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] membphis commented on pull request #2488: feat: add error-log-logger plugin

Posted by GitBox <gi...@apache.org>.
membphis commented on pull request #2488:
URL: https://github.com/apache/apisix/pull/2488#issuecomment-716069896


   need to update the doc ^_^


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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] membphis commented on a change in pull request #2488: feat: add error-log-logger plugin

Posted by GitBox <gi...@apache.org>.
membphis commented on a change in pull request #2488:
URL: https://github.com/apache/apisix/pull/2488#discussion_r510666517



##########
File path: conf/config-default.yaml
##########
@@ -193,6 +193,7 @@ plugins:                          # plugin list
   - proxy-mirror
   - request-id
   - hmac-auth
+  - error-log-logger

Review comment:
       Do not need to be turned on by default.
   
   `# - error-log-logger`, should be fine.

##########
File path: conf/config-default.yaml
##########
@@ -201,3 +202,9 @@ plugin_attr:
   log-rotate:
     interval: 3600    # rotate interval (unit: second)
     max_kept: 168     # max number of log files will be kept
+  error-log-logger:
+    host: "127.0.0.1"

Review comment:
       need some comment for each field

##########
File path: t/plugin/error-log-logger.t
##########
@@ -0,0 +1,122 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    my $stream_single_server = <<_EOC_;
+    # fake server, only for test
+    server {
+        listen 33333;
+
+        content_by_lua_block {
+            local exiting = ngx.worker.exiting
+            local sock, err = ngx.req.socket(true)
+            if not sock then
+                ngx.log(ngx.WARN, "socket error:" .. err)

Review comment:
       `ngx.log(ngx.WARN, "socket error:" .. err)`
   
   to
   
   `ngx.log(ngx.WARN, "socket error:", err)`

##########
File path: t/plugin/error-log-logger.t
##########
@@ -0,0 +1,122 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    my $stream_single_server = <<_EOC_;
+    # fake server, only for test
+    server {
+        listen 33333;
+
+        content_by_lua_block {
+            local exiting = ngx.worker.exiting
+            local sock, err = ngx.req.socket(true)
+            if not sock then
+                ngx.log(ngx.WARN, "socket error:" .. err)
+                return
+            end
+            sock:settimeout(30 * 1000)
+            while(not exiting())
+            do
+                local data, err =  sock:receive()
+                if (data) then
+                    ngx.log(ngx.INFO, "[Server] receive data:" .. data)
+                else 
+                    if err ~= "timeout" then
+                        ngx.log(ngx.WARN, "socket error:" .. err)
+                        return
+                    end
+                end
+            end
+        }
+    }
+_EOC_
+
+    $block->set_value("stream_config", $stream_single_server);
+});
+
+
+add_block_preprocessor(sub {

Review comment:
       why we need two `add_block_preprocessor`? I think one is enough

##########
File path: apisix/plugins/error-log-logger.lua
##########
@@ -0,0 +1,140 @@
+--
+-- 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 core = require("apisix.core")
+local plugin_name = "error-log-logger"
+local errlog = require "ngx.errlog"
+local ngx = ngx
+local tcp = ngx.socket.tcp
+local select = select
+local type = type
+local string = string
+
+local timer
+local schema = {
+    type = "object",
+    properties = {},
+    additionalProperties = false,
+}
+
+local log_level = {
+    STDERR =    ngx.STDERR,
+    EMERG  =    ngx.EMERG,
+    ALERT  =    ngx.ALERT,
+    CRIT   =    ngx.CRIT,
+    ERR    =    ngx.ERR,
+    ERROR  =    ngx.ERR,
+    WARN   =    ngx.WARN,
+    NOTICE =     ngx.NOTICE,
+    INFO   =    ngx.INFO,
+    DEBUG  =    ngx.DEBUG
+}
+
+local _M = {
+    version = 0.1,
+    priority = 1091,
+    name = plugin_name,
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+local function try_attr(t, ...)
+    local count = select('#', ...)
+    for i = 1, count do
+        local attr = select(i, ...)
+        t = t[attr]
+        if type(t) ~= "table" then
+            return false
+        end
+    end
+
+    return true
+end
+
+
+local function report()
+    local local_conf = core.config.local_conf()
+    local host, port
+    local timeout = 3
+    local keepalive = 3
+    local level = "warn"
+    if try_attr(local_conf, "plugin_attr", plugin_name) then
+        local attr = local_conf.plugin_attr[plugin_name]
+        host = attr.host
+        port = attr.port
+        level = attr.loglevel or level
+        timeout = attr.timeout or timeout
+        keepalive = attr.keepalive or keepalive
+    end
+    level = log_level[string.upper(level)]
+
+    local status, err = errlog.set_filter_level(level)
+    if not status then
+        core.log.warn("failed to set filter level by ngx.errlog, the error is :", err)
+        return
+    end
+
+    local sock, soc_err = tcp()
+    if not sock then
+        core.log.warn("failed to init the socket " .. soc_err)
+        return
+    end
+    sock:settimeout(timeout*1000)
+    local ok, err = sock:connect(host, port)
+    if not ok then
+        core.log.warn("connect to the server failed for " .. err)
+        return
+    end
+    local logs = errlog.get_logs(10)
+    while ( logs and #logs>0 ) do
+        for i = 1, #logs, 3 do
+            if logs[i] <= level then --ommit the lower log producted at the initial

Review comment:
       I think we can remove this line, it seems useless

##########
File path: apisix/plugins/error-log-logger.lua
##########
@@ -0,0 +1,140 @@
+--
+-- 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 core = require("apisix.core")
+local plugin_name = "error-log-logger"
+local errlog = require "ngx.errlog"
+local ngx = ngx
+local tcp = ngx.socket.tcp
+local select = select
+local type = type
+local string = string
+
+local timer
+local schema = {
+    type = "object",
+    properties = {},
+    additionalProperties = false,
+}
+
+local log_level = {
+    STDERR =    ngx.STDERR,
+    EMERG  =    ngx.EMERG,
+    ALERT  =    ngx.ALERT,
+    CRIT   =    ngx.CRIT,
+    ERR    =    ngx.ERR,
+    ERROR  =    ngx.ERR,
+    WARN   =    ngx.WARN,
+    NOTICE =     ngx.NOTICE,
+    INFO   =    ngx.INFO,
+    DEBUG  =    ngx.DEBUG
+}
+
+local _M = {
+    version = 0.1,
+    priority = 1091,
+    name = plugin_name,
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+local function try_attr(t, ...)
+    local count = select('#', ...)
+    for i = 1, count do
+        local attr = select(i, ...)
+        t = t[attr]
+        if type(t) ~= "table" then
+            return false
+        end
+    end
+
+    return true
+end
+
+
+local function report()
+    local local_conf = core.config.local_conf()
+    local host, port
+    local timeout = 3
+    local keepalive = 3
+    local level = "warn"
+    if try_attr(local_conf, "plugin_attr", plugin_name) then
+        local attr = local_conf.plugin_attr[plugin_name]
+        host = attr.host
+        port = attr.port
+        level = attr.loglevel or level
+        timeout = attr.timeout or timeout
+        keepalive = attr.keepalive or keepalive
+    end
+    level = log_level[string.upper(level)]
+
+    local status, err = errlog.set_filter_level(level)
+    if not status then
+        core.log.warn("failed to set filter level by ngx.errlog, the error is :", err)
+        return
+    end
+
+    local sock, soc_err = tcp()
+    if not sock then
+        core.log.warn("failed to init the socket " .. soc_err)
+        return
+    end
+    sock:settimeout(timeout*1000)
+    local ok, err = sock:connect(host, port)
+    if not ok then
+        core.log.warn("connect to the server failed for " .. err)
+        return
+    end
+    local logs = errlog.get_logs(10)
+    while ( logs and #logs>0 ) do
+        for i = 1, #logs, 3 do
+            if logs[i] <= level then --ommit the lower log producted at the initial
+                local bytes, err = sock:send(logs[i + 2])
+                if not bytes then
+                    core.log.info("send data  failed for " , err, ", the data:", logs[i + 2] )

Review comment:
       `] )` remove this space

##########
File path: conf/config-default.yaml
##########
@@ -201,3 +202,9 @@ plugin_attr:
   log-rotate:
     interval: 3600    # rotate interval (unit: second)
     max_kept: 168     # max number of log files will be kept
+  error-log-logger:
+    host: "127.0.0.1"
+    port: 33333
+    loglevel: "warn"
+

Review comment:
       one blank line is enough here

##########
File path: conf/config-default.yaml
##########
@@ -201,3 +202,9 @@ plugin_attr:
   log-rotate:
     interval: 3600    # rotate interval (unit: second)
     max_kept: 168     # max number of log files will be kept
+  error-log-logger:
+    host: "127.0.0.1"
+    port: 33333
+    loglevel: "warn"

Review comment:
       need a better name: `log_level` or `level`

##########
File path: apisix/plugins/error-log-logger.lua
##########
@@ -0,0 +1,140 @@
+--
+-- 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 core = require("apisix.core")
+local plugin_name = "error-log-logger"
+local errlog = require "ngx.errlog"
+local ngx = ngx
+local tcp = ngx.socket.tcp
+local select = select
+local type = type
+local string = string
+
+local timer
+local schema = {
+    type = "object",
+    properties = {},
+    additionalProperties = false,
+}
+
+local log_level = {
+    STDERR =    ngx.STDERR,
+    EMERG  =    ngx.EMERG,
+    ALERT  =    ngx.ALERT,
+    CRIT   =    ngx.CRIT,
+    ERR    =    ngx.ERR,
+    ERROR  =    ngx.ERR,
+    WARN   =    ngx.WARN,
+    NOTICE =     ngx.NOTICE,
+    INFO   =    ngx.INFO,
+    DEBUG  =    ngx.DEBUG
+}
+
+local _M = {
+    version = 0.1,
+    priority = 1091,
+    name = plugin_name,
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+local function try_attr(t, ...)
+    local count = select('#', ...)
+    for i = 1, count do
+        local attr = select(i, ...)
+        t = t[attr]
+        if type(t) ~= "table" then
+            return false
+        end
+    end
+
+    return true
+end
+
+
+local function report()
+    local local_conf = core.config.local_conf()
+    local host, port
+    local timeout = 3
+    local keepalive = 3
+    local level = "warn"
+    if try_attr(local_conf, "plugin_attr", plugin_name) then
+        local attr = local_conf.plugin_attr[plugin_name]
+        host = attr.host
+        port = attr.port
+        level = attr.loglevel or level
+        timeout = attr.timeout or timeout

Review comment:
       need to add this field in `config-default.yaml`

##########
File path: t/plugin/error-log-logger.t
##########
@@ -0,0 +1,122 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    my $stream_single_server = <<_EOC_;
+    # fake server, only for test
+    server {
+        listen 33333;
+
+        content_by_lua_block {
+            local exiting = ngx.worker.exiting
+            local sock, err = ngx.req.socket(true)
+            if not sock then
+                ngx.log(ngx.WARN, "socket error:" .. err)
+                return
+            end
+            sock:settimeout(30 * 1000)
+            while(not exiting())
+            do
+                local data, err =  sock:receive()
+                if (data) then
+                    ngx.log(ngx.INFO, "[Server] receive data:" .. data)
+                else 
+                    if err ~= "timeout" then
+                        ngx.log(ngx.WARN, "socket error:" .. err)
+                        return
+                    end
+                end
+            end
+        }
+    }
+_EOC_
+
+    $block->set_value("stream_config", $stream_single_server);
+});
+
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    my $stream_default_server = <<_EOC_;
+	    content_by_lua_block {
+	    	ngx.log(ngx.INFO, "a stream server")
+	    }
+_EOC_
+
+    $block->set_value("stream_server_config", $stream_default_server);
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: log a warn level message
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            core.log.warn("this is a warning message for test.\n")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/\[Server\] receive data:.*this is a warning message for test./
+--- wait: 1
+
+
+
+=== TEST 2: log an error level message
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            core.log.error("this is an error message for test.\n")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/\[Server\] receive data:.*this is an error message for test./
+--- wait: 1
+
+
+
+=== TEST 3: log an info level message

Review comment:
       more test case about when user set different address or wrong address

##########
File path: apisix/plugins/error-log-logger.lua
##########
@@ -0,0 +1,140 @@
+--
+-- 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 core = require("apisix.core")
+local plugin_name = "error-log-logger"
+local errlog = require "ngx.errlog"
+local ngx = ngx
+local tcp = ngx.socket.tcp
+local select = select
+local type = type
+local string = string
+
+local timer
+local schema = {
+    type = "object",
+    properties = {},
+    additionalProperties = false,
+}
+
+local log_level = {
+    STDERR =    ngx.STDERR,
+    EMERG  =    ngx.EMERG,
+    ALERT  =    ngx.ALERT,
+    CRIT   =    ngx.CRIT,
+    ERR    =    ngx.ERR,
+    ERROR  =    ngx.ERR,
+    WARN   =    ngx.WARN,
+    NOTICE =     ngx.NOTICE,
+    INFO   =    ngx.INFO,
+    DEBUG  =    ngx.DEBUG
+}
+
+local _M = {
+    version = 0.1,
+    priority = 1091,
+    name = plugin_name,
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+local function try_attr(t, ...)
+    local count = select('#', ...)
+    for i = 1, count do
+        local attr = select(i, ...)
+        t = t[attr]
+        if type(t) ~= "table" then
+            return false
+        end
+    end
+
+    return true
+end
+
+
+local function report()
+    local local_conf = core.config.local_conf()
+    local host, port
+    local timeout = 3
+    local keepalive = 3
+    local level = "warn"
+    if try_attr(local_conf, "plugin_attr", plugin_name) then
+        local attr = local_conf.plugin_attr[plugin_name]
+        host = attr.host
+        port = attr.port
+        level = attr.loglevel or level
+        timeout = attr.timeout or timeout
+        keepalive = attr.keepalive or keepalive
+    end
+    level = log_level[string.upper(level)]
+
+    local status, err = errlog.set_filter_level(level)
+    if not status then
+        core.log.warn("failed to set filter level by ngx.errlog, the error is :", err)
+        return
+    end
+
+    local sock, soc_err = tcp()
+    if not sock then
+        core.log.warn("failed to init the socket " .. soc_err)
+        return
+    end
+    sock:settimeout(timeout*1000)
+    local ok, err = sock:connect(host, port)
+    if not ok then
+        core.log.warn("connect to the server failed for " .. err)
+        return
+    end
+    local logs = errlog.get_logs(10)
+    while ( logs and #logs>0 ) do
+        for i = 1, #logs, 3 do
+            if logs[i] <= level then --ommit the lower log producted at the initial
+                local bytes, err = sock:send(logs[i + 2])

Review comment:
       https://github.com/apache/apisix/blob/master/apisix/utils/batch-processor.lua
   
   I think we can use this way to send the error log.

##########
File path: apisix/plugins/error-log-logger.lua
##########
@@ -0,0 +1,140 @@
+--
+-- 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 core = require("apisix.core")
+local plugin_name = "error-log-logger"
+local errlog = require "ngx.errlog"
+local ngx = ngx
+local tcp = ngx.socket.tcp
+local select = select
+local type = type
+local string = string
+
+local timer
+local schema = {
+    type = "object",
+    properties = {},
+    additionalProperties = false,
+}
+
+local log_level = {
+    STDERR =    ngx.STDERR,
+    EMERG  =    ngx.EMERG,
+    ALERT  =    ngx.ALERT,
+    CRIT   =    ngx.CRIT,
+    ERR    =    ngx.ERR,
+    ERROR  =    ngx.ERR,
+    WARN   =    ngx.WARN,
+    NOTICE =     ngx.NOTICE,
+    INFO   =    ngx.INFO,
+    DEBUG  =    ngx.DEBUG
+}
+
+local _M = {
+    version = 0.1,
+    priority = 1091,
+    name = plugin_name,
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+local function try_attr(t, ...)
+    local count = select('#', ...)
+    for i = 1, count do
+        local attr = select(i, ...)
+        t = t[attr]
+        if type(t) ~= "table" then
+            return false
+        end
+    end
+
+    return true
+end
+
+
+local function report()
+    local local_conf = core.config.local_conf()
+    local host, port
+    local timeout = 3
+    local keepalive = 3
+    local level = "warn"
+    if try_attr(local_conf, "plugin_attr", plugin_name) then
+        local attr = local_conf.plugin_attr[plugin_name]
+        host = attr.host
+        port = attr.port
+        level = attr.loglevel or level
+        timeout = attr.timeout or timeout
+        keepalive = attr.keepalive or keepalive
+    end
+    level = log_level[string.upper(level)]
+
+    local status, err = errlog.set_filter_level(level)
+    if not status then
+        core.log.warn("failed to set filter level by ngx.errlog, the error is :", err)
+        return
+    end
+
+    local sock, soc_err = tcp()
+    if not sock then
+        core.log.warn("failed to init the socket " .. soc_err)
+        return
+    end
+    sock:settimeout(timeout*1000)
+    local ok, err = sock:connect(host, port)
+    if not ok then
+        core.log.warn("connect to the server failed for " .. err)
+        return
+    end
+    local logs = errlog.get_logs(10)
+    while ( logs and #logs>0 ) do
+        for i = 1, #logs, 3 do
+            if logs[i] <= level then --ommit the lower log producted at the initial
+                local bytes, err = sock:send(logs[i + 2])
+                if not bytes then
+                    core.log.info("send data  failed for " , err, ", the data:", logs[i + 2] )
+                    return
+                end
+            end
+        end
+        logs = errlog.get_logs(10)
+	end
+    sock:setkeepalive(keepalive*1000)

Review comment:
       code style: need a space, `keepalive * 1000`




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] dabue commented on a change in pull request #2488: feat: add error-log-logger plugin

Posted by GitBox <gi...@apache.org>.
dabue commented on a change in pull request #2488:
URL: https://github.com/apache/apisix/pull/2488#discussion_r511587958



##########
File path: t/plugin/error-log-logger.t
##########
@@ -0,0 +1,122 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    my $stream_single_server = <<_EOC_;
+    # fake server, only for test
+    server {
+        listen 33333;
+
+        content_by_lua_block {
+            local exiting = ngx.worker.exiting
+            local sock, err = ngx.req.socket(true)
+            if not sock then
+                ngx.log(ngx.WARN, "socket error:" .. err)
+                return
+            end
+            sock:settimeout(30 * 1000)
+            while(not exiting())
+            do
+                local data, err =  sock:receive()
+                if (data) then
+                    ngx.log(ngx.INFO, "[Server] receive data:" .. data)
+                else 
+                    if err ~= "timeout" then
+                        ngx.log(ngx.WARN, "socket error:" .. err)
+                        return
+                    end
+                end
+            end
+        }
+    }
+_EOC_
+
+    $block->set_value("stream_config", $stream_single_server);
+});
+
+
+add_block_preprocessor(sub {

Review comment:
       In the test case ,  I need to add a new TCP server to receive the error log data.  At the beginnging, I want to use a special port instead of a built-in one, so I  aad the first add_block_preprocessor, but there is a  built-in server which has no handler , It'll cases error if I don't add_block_preprocessor for it.




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] dabue commented on a change in pull request #2488: feat: add error-log-logger plugin

Posted by GitBox <gi...@apache.org>.
dabue commented on a change in pull request #2488:
URL: https://github.com/apache/apisix/pull/2488#discussion_r511594471



##########
File path: t/plugin/error-log-logger.t
##########
@@ -0,0 +1,122 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    my $stream_single_server = <<_EOC_;
+    # fake server, only for test
+    server {
+        listen 33333;
+
+        content_by_lua_block {
+            local exiting = ngx.worker.exiting
+            local sock, err = ngx.req.socket(true)
+            if not sock then
+                ngx.log(ngx.WARN, "socket error:" .. err)
+                return
+            end
+            sock:settimeout(30 * 1000)
+            while(not exiting())
+            do
+                local data, err =  sock:receive()
+                if (data) then
+                    ngx.log(ngx.INFO, "[Server] receive data:" .. data)
+                else 
+                    if err ~= "timeout" then
+                        ngx.log(ngx.WARN, "socket error:" .. err)
+                        return
+                    end
+                end
+            end
+        }
+    }
+_EOC_
+
+    $block->set_value("stream_config", $stream_single_server);
+});
+
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    my $stream_default_server = <<_EOC_;
+	    content_by_lua_block {
+	    	ngx.log(ngx.INFO, "a stream server")
+	    }
+_EOC_
+
+    $block->set_value("stream_server_config", $stream_default_server);
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: log a warn level message
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            core.log.warn("this is a warning message for test.\n")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/\[Server\] receive data:.*this is a warning message for test./
+--- wait: 1
+
+
+
+=== TEST 2: log an error level message
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            core.log.error("this is an error message for test.\n")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- error_log eval
+qr/\[Server\] receive data:.*this is an error message for test./
+--- wait: 1
+
+
+
+=== TEST 3: log an info level message

Review comment:
       or I create a new 't' file to add the wrong address case?




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] dabue commented on pull request #2488: feat: add error-log-logger plugin

Posted by GitBox <gi...@apache.org>.
dabue commented on pull request #2488:
URL: https://github.com/apache/apisix/pull/2488#issuecomment-717667834


   @membphis  please have a review, thx.


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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] dabue commented on a change in pull request #2488: feat: add error-log-logger plugin

Posted by GitBox <gi...@apache.org>.
dabue commented on a change in pull request #2488:
URL: https://github.com/apache/apisix/pull/2488#discussion_r511587958



##########
File path: t/plugin/error-log-logger.t
##########
@@ -0,0 +1,122 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    my $stream_single_server = <<_EOC_;
+    # fake server, only for test
+    server {
+        listen 33333;
+
+        content_by_lua_block {
+            local exiting = ngx.worker.exiting
+            local sock, err = ngx.req.socket(true)
+            if not sock then
+                ngx.log(ngx.WARN, "socket error:" .. err)
+                return
+            end
+            sock:settimeout(30 * 1000)
+            while(not exiting())
+            do
+                local data, err =  sock:receive()
+                if (data) then
+                    ngx.log(ngx.INFO, "[Server] receive data:" .. data)
+                else 
+                    if err ~= "timeout" then
+                        ngx.log(ngx.WARN, "socket error:" .. err)
+                        return
+                    end
+                end
+            end
+        }
+    }
+_EOC_
+
+    $block->set_value("stream_config", $stream_single_server);
+});
+
+
+add_block_preprocessor(sub {

Review comment:
       In the test case ,  I need to add a new TCP server to receive the error log data.  At the beginnging, I want to use a special port instead of a built-in one, so I  aad the first add_block_preprocessor, but there is a  built-in server which has no handler , It'll cause error if I don't add_block_preprocessor for it.




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] dabue commented on a change in pull request #2488: feat: add error-log-logger plugin

Posted by GitBox <gi...@apache.org>.
dabue commented on a change in pull request #2488:
URL: https://github.com/apache/apisix/pull/2488#discussion_r511587958



##########
File path: t/plugin/error-log-logger.t
##########
@@ -0,0 +1,122 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    my $stream_single_server = <<_EOC_;
+    # fake server, only for test
+    server {
+        listen 33333;
+
+        content_by_lua_block {
+            local exiting = ngx.worker.exiting
+            local sock, err = ngx.req.socket(true)
+            if not sock then
+                ngx.log(ngx.WARN, "socket error:" .. err)
+                return
+            end
+            sock:settimeout(30 * 1000)
+            while(not exiting())
+            do
+                local data, err =  sock:receive()
+                if (data) then
+                    ngx.log(ngx.INFO, "[Server] receive data:" .. data)
+                else 
+                    if err ~= "timeout" then
+                        ngx.log(ngx.WARN, "socket error:" .. err)
+                        return
+                    end
+                end
+            end
+        }
+    }
+_EOC_
+
+    $block->set_value("stream_config", $stream_single_server);
+});
+
+
+add_block_preprocessor(sub {

Review comment:
       In the test case ,  I need to add a new TCP server to receive the error log data.  At the beginnging, I want to use a special port instead of the built-in one, so I  aad the first add_block_preprocessor, but there is a  built-in server which has no handler , It'll cause error if I don't add_block_preprocessor for it.




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] dabue commented on pull request #2488: feat: add error-log-logger plugin

Posted by GitBox <gi...@apache.org>.
dabue commented on pull request #2488:
URL: https://github.com/apache/apisix/pull/2488#issuecomment-717654853


   @membphis  how to resolve the conflicts. there was no notice when I push my update.


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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org