Skip to content

Commit

Permalink
Add syntax sugar for more convenient logging
Browse files Browse the repository at this point in the history
You can use `t.log` for logging. This is convenient in case of doping in
several places at the same time:

    t.log('outside')
    g.test_foo = function()
        t.log('inside')
        g.server:exec(function() t.log('hi!') end)
    end

    I> outside
    I> inside
    I> hi!

The pretty conversion of arguments of any type will be performed
automatically:

    t.log('My structure is %s', {a = 1, b = 2, c = {cc = 1}})
    I> My structure is {a = 1, b = 2, c = {cc = 1}}

Closes tarantool#326
  • Loading branch information
ochaplashkin committed Dec 28, 2023
1 parent f37b353 commit 9942619
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Fixed incorrent unix socket path length check (gh-341).
* Now net_box_uri can be accepted as table (gh-342).
* Add syntax sugar for more convenient logging.

## 1.0.0

Expand Down
5 changes: 5 additions & 0 deletions luatest/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ luatest.ReplicaSet = require('luatest.replica_set')
local Group = require('luatest.group')
local hooks = require('luatest.hooks')
local parametrizer = require('luatest.parametrizer')
local utils = require('luatest.utils')

--- Add syntax sugar for logging.
--
luatest.log = utils.log

--- Add before suite hook.
--
Expand Down
24 changes: 15 additions & 9 deletions luatest/pp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local TABLE_TOSTRING_SEP_LEN = string.len(TABLE_TOSTRING_SEP)

-- Final function called in format_table() to format the resulting list of
-- string describing the table.
local function _table_tostring_format_result(tbl, result, indentLevel, printTableRefs)
local function _table_tostring_format_result(tbl, result, indentLevel, printTableRefs, isLogLine)
local dispOnMultLines = false

-- set dispOnMultLines to true if the maximum LINE_LENGTH would be exceeded with the values
Expand All @@ -46,7 +46,7 @@ local function _table_tostring_format_result(tbl, result, indentLevel, printTabl
end

-- now reformat the result table (currently holding element strings)
if dispOnMultLines then
if dispOnMultLines and not isLogLine then
local indentString = string.rep(" ", indentLevel - 1)
result = {
"{\n ",
Expand Down Expand Up @@ -77,7 +77,7 @@ function Formatter.mt:initialize(printTableRefs)
self.recursionTable = {}
end

function Formatter.mt:format_table(tbl, indentLevel)
function Formatter.mt:format_table(tbl, indentLevel, isLogLine)
indentLevel = indentLevel or 1
self.recursionTable[tbl] = true

Expand Down Expand Up @@ -133,16 +133,16 @@ function Formatter.mt:format_table(tbl, indentLevel)
count = count + 1
result[count] = entry
end
return _table_tostring_format_result(tbl, result, indentLevel, self.printTableRefs)
return _table_tostring_format_result(tbl, result, indentLevel, self.printTableRefs, isLogLine)
end
end

function Formatter.mt:format(v, indentLevel)
function Formatter.mt:format(v, indentLevel, isLogLine)
local type_v = type(v)
if "string" == type_v then
return string.format("%q", v)
elseif "table" == type_v then
return self:format_table(v, indentLevel)
return self:format_table(v, indentLevel, isLogLine)
elseif "number" == type_v then
-- eliminate differences in formatting between various Lua versions
if v ~= v then
Expand All @@ -169,18 +169,24 @@ end
--
-- * string are enclosed with " by default, or with ' if string contains a "
-- * tables are expanded to show their full content, with indentation in case of nested tables
function pp.tostring(value)
function pp.tostring(value, is_logline)
local formatter = Formatter:new(pp.TABLE_REF_IN_ERROR_MSG)
local result = formatter:format(value)
local result = formatter:format(value, nil, is_logline)
if formatter.recursionDetected and not pp.TABLE_REF_IN_ERROR_MSG then
-- some table contain recursive references,
-- so we must recompute the value by including all table references
-- else the result looks like crap
return Formatter:new(true):format(value)
return Formatter:new(true):format(value, nil, is_logline)
end
return result
end

-- This function helps with display variable of any type without line breaks ('\n')
-- for the log lines. It is a simple wrapper over the tostring() function.
function pp.tostringlog(value)
return pp.tostring(value, true)
end

local function has_new_line(s)
return (string.find(s, '\n', 1, true) ~= nil)
end
Expand Down
11 changes: 11 additions & 0 deletions luatest/utils.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local digest = require('digest')
local fun = require('fun')
local yaml = require('yaml')
local log = require('log')

local pp = require('luatest.pp')

local utils = {}

Expand Down Expand Up @@ -191,4 +194,12 @@ function utils.is_tarantool_binary(path)
return path:find('^.*/tarantool[^/]*$') ~= nil
end

function utils.log(msg, ...)
local args = {...}
for k, v in pairs(args) do
args[k] = pp.tostringlog(v)
end
log.info(msg, unpack(args))
end

return utils

0 comments on commit 9942619

Please sign in to comment.