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:
[email protected]


Reply via email to