Tarantool development patches archive
 help / color / mirror / Atom feed
From: Cyrill Gorcunov <gorcunov@gmail.com>
To: tml <tarantool-patches@dev.tarantool.org>
Cc: yaroslav.dynnikov@gmail.com
Subject: [Tarantool-patches] [PATCH v3 2/3] lua/log: allow to use json formatter early
Date: Thu,  2 Jul 2020 12:50:53 +0300	[thread overview]
Message-ID: <20200702095054.367131-3-gorcunov@gmail.com> (raw)
In-Reply-To: <20200702095054.367131-1-gorcunov@gmail.com>

There is no reason to not allow for json formatter
on early logging stage.

We add verification that

	box.cfg{log="syslog:", log_format="json"}
or
	require('log').cfg{log="syslog:", format="json"}

is triggering error since syslog output requires
predefined structure and can't use json.

Fixes #5121

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
---
 src/box/lua/load_cfg.lua |  6 +++---
 src/lua/log.lua          | 42 ++++++++++++++++++++++++++++++----------
 2 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/src/box/lua/load_cfg.lua b/src/box/lua/load_cfg.lua
index f2f2df6f8..549c14cf3 100644
--- a/src/box/lua/load_cfg.lua
+++ b/src/box/lua/load_cfg.lua
@@ -459,13 +459,13 @@ local function prepare_cfg(cfg, default_cfg, template_cfg,
                             module_cfg[k], modify_cfg[k], readable_name)
         elseif template_cfg[k] == 'module' then
             local old_value = module_cfg[k].cfg_get(k, v)
-            module_cfg_backup[k] = old_value
+            module_cfg_backup[k] = old_value or box.NULL
 
-            local ok, msg = module_cfg[k].cfg_set(k, v)
+            local ok, msg = module_cfg[k].cfg_set(cfg, k, v)
             if not ok then
                 -- restore back the old values for modules
                 for module_k, module_v in pairs(module_cfg_backup) do
-                    module_cfg[module_k].cfg_set(module_k, module_v)
+                    module_cfg[module_k].cfg_set(nil, module_k, module_v)
                 end
                 box.error(box.error.CFG, readable_name, msg)
             end
diff --git a/src/lua/log.lua b/src/lua/log.lua
index bed690526..0d427a0b7 100644
--- a/src/lua/log.lua
+++ b/src/lua/log.lua
@@ -194,7 +194,7 @@ local function verify_static(k, v)
 end
 
 -- Test if format is valid.
-local function verify_format(key, name)
+local function verify_format(key, name, cfg)
     assert(log_cfg[key] ~= nil)
 
     if not fmt_str2num[name] then
@@ -202,11 +202,25 @@ local function verify_format(key, name)
         return false, m:format(fmt_list())
     end
 
+    local log_type = ffi.C.log_type()
+
+    -- When comes from log.cfg{} or box.cfg{}
+    -- initial call we might be asked to setup
+    -- syslog with json which is not allowed.
+    --
+    -- Note the cfg table comes from two places:
+    -- box api interface and log module itself.
+    -- The good thing that we're only needed log
+    -- entry which is the same key for both.
+    if cfg ~= nil and cfg['log'] ~= nil then
+        if string.startswith(cfg['log'], "syslog:") then
+            log_type = ffi.C.SAY_LOGGER_SYSLOG
+        end
+    end
+
     if fmt_str2num[name] == ffi.C.SF_JSON then
-        if ffi.C.log_type() == ffi.C.SAY_LOGGER_SYSLOG or
-            ffi.C.log_type() == ffi.C.SAY_LOGGER_BOOT then
-            local m = "%s can't be used with " ..
-            "syslog or boot-time logger"
+        if log_type == ffi.C.SAY_LOGGER_SYSLOG then
+            local m = "%s can't be used with syslog logger"
             return false, m:format(fmt_num2str[ffi.C.SF_JSON])
         end
     end
@@ -239,11 +253,11 @@ local verify_ops = {
 }
 
 -- Verify a value for the particular key.
-local function verify_option(k, v)
+local function verify_option(k, v, ...)
     assert(k ~= nil)
 
     if verify_ops[k] ~= nil then
-        return verify_ops[k](k, v)
+        return verify_ops[k](k, v, ...)
     end
 
     return true
@@ -372,10 +386,18 @@ local function box_api_cfg_get(key)
 end
 
 -- Set value to log from box.cfg{}.
-local function box_api_cfg_set(key, value)
+local function box_api_cfg_set(cfg, key, value)
     local log_key = box2log_keys[key]
+    local aux_data
+
+    -- a special case where we need to restore
+    -- nil value from previous setup attempt.
+    if value == box.NULL then
+        log_cfg[log_key] = nil
+        return true
+    end
 
-    local ok, msg = verify_option(log_key, value)
+    local ok, msg = verify_option(log_key, value, cfg)
     if not ok then
         return false, msg
     end
@@ -450,7 +472,7 @@ local function load_cfg(oldcfg, cfg)
     end
 
     if cfg.format ~= nil then
-        local ok, msg = verify_option('format', cfg.format)
+        local ok, msg = verify_option('format', cfg.format, cfg)
         if not ok then
             local m = "log.cfg: \'%s\' %s"
             error(m:format('format', msg))
-- 
2.26.2

  parent reply	other threads:[~2020-07-02  9:51 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-02  9:50 [Tarantool-patches] [PATCH v3 0/3] log: allow json formatter in boottime logger Cyrill Gorcunov
2020-07-02  9:50 ` [Tarantool-patches] [PATCH v3 1/3] core/say: allow to use json in boot logger Cyrill Gorcunov
2020-07-02  9:50 ` Cyrill Gorcunov [this message]
2020-07-02 10:05   ` [Tarantool-patches] [PATCH v3 2/3] lua/log: allow to use json formatter early Oleg Babin
2020-07-02 10:18     ` Cyrill Gorcunov
2020-07-02 10:29       ` Oleg Babin
2020-07-02  9:50 ` [Tarantool-patches] [PATCH v3 3/3] test: app-tap/logger -- test json in boottime logger Cyrill Gorcunov
2020-07-02 10:08 ` [Tarantool-patches] [PATCH v3 0/3] log: allow json formatter " Oleg Babin
2020-07-02 10:46 ` Yaroslav Dynnikov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200702095054.367131-3-gorcunov@gmail.com \
    --to=gorcunov@gmail.com \
    --cc=tarantool-patches@dev.tarantool.org \
    --cc=yaroslav.dynnikov@gmail.com \
    --subject='Re: [Tarantool-patches] [PATCH v3 2/3] lua/log: allow to use json formatter early' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox