From fe1724492988f5f1ef08177252ddc67e8eaa3ea6 Mon Sep 17 00:00:00 2001 From: Anna Pekarska Date: Fri, 10 Jul 2020 14:57:25 +0300 Subject: [PATCH 1/3] add support for logging in JSON format --- log.lua | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/log.lua b/log.lua index d7bc2d4..7341128 100644 --- a/log.lua +++ b/log.lua @@ -12,6 +12,7 @@ local log = { _version = "0.1.0" } log.usecolor = true log.outfile = nil log.level = "trace" +log.format = "txt" local modes = { @@ -66,20 +67,40 @@ for i, x in ipairs(modes) do local lineinfo = info.short_src .. ":" .. info.currentline -- Output to console - print(string.format("%s[%-6s%s]%s %s: %s", - log.usecolor and x.color or "", - nameupper, - os.date("%H:%M:%S"), - log.usecolor and "\27[0m" or "", - lineinfo, - msg)) + local output = "" + if log.format == "json" then + output = string.format( + "{\"message\": \"%s\", \"levelname\": \"%s\", \"asctime\": \"%s\", \"created\": %s}", + msg, + nameupper, + os.date("!%Y-%m-%d %H:%M:%S"), + os.time() + ) + + else + output = string.format("%s[%-6s%s]%s %s: %s", + log.usecolor and x.color or "", + nameupper, + os.date("%H:%M:%S"), + log.usecolor and "\27[0m" or "", + lineinfo, + msg) + end + + print(output) -- Output to log file if log.outfile then local fp = io.open(log.outfile, "a") - local str = string.format("[%-6s%s] %s: %s\n", + local out_str = "" + if log.format == "json" then + out_str = output.."\n" + else + out_str = string.format("[%-6s%s] %s: %s\n", nameupper, os.date(), lineinfo, msg) - fp:write(str) + end + + fp:write(out_str) fp:close() end From 6f98777d28aea0893407386ec70cdd9afd9e097f Mon Sep 17 00:00:00 2001 From: Anna Pekarska Date: Fri, 10 Jul 2020 18:36:03 +0300 Subject: [PATCH 2/3] extend json output --- log.lua | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/log.lua b/log.lua index 7341128..a58e801 100644 --- a/log.lua +++ b/log.lua @@ -67,16 +67,16 @@ for i, x in ipairs(modes) do local lineinfo = info.short_src .. ":" .. info.currentline -- Output to console - local output = "" if log.format == "json" then output = string.format( - "{\"message\": \"%s\", \"levelname\": \"%s\", \"asctime\": \"%s\", \"created\": %s}", + "{\"message\": \"%s\", \"levelname\": \"%s\", \"pathname\": \"%s\", \"lineno\": \"%s\", \"asctime\": \"%s\", \"created\": %s}", msg, nameupper, + info.short_src, + info.currentline, os.date("!%Y-%m-%d %H:%M:%S"), os.time() ) - else output = string.format("%s[%-6s%s]%s %s: %s", log.usecolor and x.color or "", @@ -92,7 +92,6 @@ for i, x in ipairs(modes) do -- Output to log file if log.outfile then local fp = io.open(log.outfile, "a") - local out_str = "" if log.format == "json" then out_str = output.."\n" else From 135134c5740c68f7486d8f962bad39e2c3199c21 Mon Sep 17 00:00:00 2001 From: Anna Pekarska Date: Fri, 10 Jul 2020 19:38:51 +0300 Subject: [PATCH 3/3] rebame log level attribute --- log.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/log.lua b/log.lua index a58e801..7e0b97b 100644 --- a/log.lua +++ b/log.lua @@ -14,7 +14,6 @@ log.outfile = nil log.level = "trace" log.format = "txt" - local modes = { { name = "trace", color = "\27[34m", }, { name = "debug", color = "\27[36m", }, @@ -69,7 +68,7 @@ for i, x in ipairs(modes) do -- Output to console if log.format == "json" then output = string.format( - "{\"message\": \"%s\", \"levelname\": \"%s\", \"pathname\": \"%s\", \"lineno\": \"%s\", \"asctime\": \"%s\", \"created\": %s}", + "{\"message\": \"%s\", \"level\": \"%s\", \"pathname\": \"%s\", \"lineno\": \"%s\", \"asctime\": \"%s\", \"created\": %s}", msg, nameupper, info.short_src,