Skip to content

Commit a005329

Browse files
Oleg Chaplashkinylobankov
Oleg Chaplashkin
authored andcommitted
Add syntax sugar for more convenient logging
You can use `luatest.log` for logging. This is convenient in case of debugging in several places at the same time: local luatest = require('luatest') luatest.log('outside') g.test_foo = function() luatest.log('inside') g.server:exec(function() luatest.log('hi!') end) end I> outside I> inside I> hi! The pretty conversion of arguments of any type will be performed automatically: luatest.log('My structure is %s', {a = 1, b = 2, c = {cc = 1}}) I> My structure is {a = 1, b = 2, c = {cc = 1}} Closes #326
1 parent f31fe34 commit a005329

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Fixed incorrent unix socket path length check (gh-341).
66
* Now net_box_uri can be accepted as table (gh-342).
77
* Fixed returning values from `Server:exec()` if some of them are nil (gh-350).
8+
* Introduce `luatest.log` helper (gh-326).
89

910
## 1.0.0
1011

luatest/init.lua

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ luatest.ReplicaSet = require('luatest.replica_set')
3333
local Group = require('luatest.group')
3434
local hooks = require('luatest.hooks')
3535
local parametrizer = require('luatest.parametrizer')
36+
local utils = require('luatest.utils')
37+
38+
--- Add syntax sugar for logging.
39+
--
40+
luatest.log = utils.log
3641

3742
--- Add before suite hook.
3843
--

luatest/pp.lua

+15-9
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ local TABLE_TOSTRING_SEP_LEN = string.len(TABLE_TOSTRING_SEP)
2121

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

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

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

80-
function Formatter.mt:format_table(tbl, indentLevel)
80+
function Formatter.mt:format_table(tbl, indentLevel, isLogLine)
8181
indentLevel = indentLevel or 1
8282
self.recursionTable[tbl] = true
8383

@@ -133,16 +133,16 @@ function Formatter.mt:format_table(tbl, indentLevel)
133133
count = count + 1
134134
result[count] = entry
135135
end
136-
return _table_tostring_format_result(tbl, result, indentLevel, self.printTableRefs)
136+
return _table_tostring_format_result(tbl, result, indentLevel, self.printTableRefs, isLogLine)
137137
end
138138
end
139139

140-
function Formatter.mt:format(v, indentLevel)
140+
function Formatter.mt:format(v, indentLevel, isLogLine)
141141
local type_v = type(v)
142142
if "string" == type_v then
143143
return string.format("%q", v)
144144
elseif "table" == type_v then
145-
return self:format_table(v, indentLevel)
145+
return self:format_table(v, indentLevel, isLogLine)
146146
elseif "number" == type_v then
147147
-- eliminate differences in formatting between various Lua versions
148148
if v ~= v then
@@ -169,18 +169,24 @@ end
169169
--
170170
-- * string are enclosed with " by default, or with ' if string contains a "
171171
-- * tables are expanded to show their full content, with indentation in case of nested tables
172-
function pp.tostring(value)
172+
function pp.tostring(value, is_logline)
173173
local formatter = Formatter:new(pp.TABLE_REF_IN_ERROR_MSG)
174-
local result = formatter:format(value)
174+
local result = formatter:format(value, nil, is_logline)
175175
if formatter.recursionDetected and not pp.TABLE_REF_IN_ERROR_MSG then
176176
-- some table contain recursive references,
177177
-- so we must recompute the value by including all table references
178178
-- else the result looks like crap
179-
return Formatter:new(true):format(value)
179+
return Formatter:new(true):format(value, nil, is_logline)
180180
end
181181
return result
182182
end
183183

184+
-- This function helps with displaying `value` of any type without line breaks ('\n')
185+
-- for logging. It is a simple wrapper over the tostring() function.
186+
function pp.tostringlog(value)
187+
return pp.tostring(value, true)
188+
end
189+
184190
local function has_new_line(s)
185191
return (string.find(s, '\n', 1, true) ~= nil)
186192
end

luatest/utils.lua

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
local digest = require('digest')
22
local fun = require('fun')
33
local yaml = require('yaml')
4+
local log = require('log')
5+
6+
local pp = require('luatest.pp')
47

58
local utils = {}
69

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

197+
function utils.log(msg, ...)
198+
local args = {...}
199+
for k, v in pairs(args) do
200+
args[k] = pp.tostringlog(v)
201+
end
202+
log.info(msg, unpack(args))
203+
end
204+
194205
return utils

0 commit comments

Comments
 (0)