Skip to content

Commit

Permalink
Improve log submodule of luatest
Browse files Browse the repository at this point in the history
We improved `luatest.log` module for convenient logging and refactored
it:

    local t = require('luatest.log')
    t.log('Info log: %s', 'foo')
    t.log.warn('Warn log from %s', 'foo')

The following functions of the `luatest.log` module are presented:

    t.log.error(msg[, args])
    t.log.warn(msg[, args])
    t.log.info(msg[, args])
    t.log.verbose(msg[, args])
    t.log.debug(msg[, args])

    t.log() - syntax sugar for t.log.info(...)

Follows tarantool#326
  • Loading branch information
ochaplashkin committed Feb 5, 2024
1 parent f8a1c10 commit 85d157c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
3 changes: 1 addition & 2 deletions luatest/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ 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
luatest.log = require('luatest.log')

--- Add before suite hook.
--
Expand Down
47 changes: 47 additions & 0 deletions luatest/log.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
local tarantool_log = require('log')

local utils = require('luatest.utils')
local pp = require('luatest.pp')

-- Utils for logging
local log = {}


local function _log(level, msg, ...)
if not utils.version_current_ge_than(2, 5, 1) then
return
end
local args = {...}
for k, v in pairs(args) do
args[k] = pp.tostringlog(v)
end
return tarantool_log[level](msg, unpack(args))
end

local function log_info(t, msg, ...)
return _log('info', msg, ...)
end

function log.info(msg, ...)
return _log('info', msg, ...)
end

function log.verbose(msg, ...)
return _log('verbose', msg, ...)
end

function log.debug(msg, ...)
return _log('info', msg, ...)
end

function log.warn(msg, ...)
return _log('warn', msg, ...)
end

function log.error(msg, ...)
return _log('error', msg, ...)
end

setmetatable(log, {__call = log_info})

return log
11 changes: 0 additions & 11 deletions luatest/utils.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
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 @@ -194,12 +191,4 @@ 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 85d157c

Please sign in to comment.