Skip to content

Commit d985997

Browse files
Oleg Chaplashkinylobankov
authored andcommitted
Add more logs
Everything is logged via new `luatest.log` module. Important and most popular modules improved by additional logs about internal states or logic. This should help in investigating problems. Closes #325
1 parent 105c69d commit d985997

File tree

9 files changed

+41
-17
lines changed

9 files changed

+41
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Add memory leak detection during server process execution (gh-349).
77
- Improve `luatest.log` function if a `nil` value is passed (gh-360).
88
- Added `assert_error_covers`.
9+
- Add more logs (gh-326).
910

1011
## 1.0.1
1112

luatest/assertions.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local math = require('math')
88
local comparator = require('luatest.comparator')
99
local mismatch_formatter = require('luatest.mismatch_formatter')
1010
local pp = require('luatest.pp')
11+
local log = require('luatest.log')
1112
local utils = require('luatest.utils')
1213

1314
local prettystr = pp.tostring
@@ -177,6 +178,7 @@ end
177178
-- @string[opt] message
178179
-- @bool[opt] deep_analysis print diff.
179180
function M.assert_equals(actual, expected, message, deep_analysis)
181+
log.info('Assert %s equals to %s', actual, expected)
180182
if not comparator.equals(actual, expected) then
181183
failure(M.private.error_msg_equality(actual, expected, deep_analysis), message, 2)
182184
end
@@ -283,6 +285,7 @@ end
283285
-- @param expected
284286
-- @string[opt] message
285287
function M.assert_not_equals(actual, expected, message)
288+
log.info('Assert %s not equals to %s', actual, expected)
286289
if comparator.equals(actual, expected) then
287290
fail_fmt(2, message, 'Actual and expected values are equal: %s', prettystr(actual))
288291
end
@@ -384,6 +387,8 @@ M.private.str_match = str_match
384387
-- @bool[opt] is_pattern
385388
-- @string[opt] message
386389
function M.assert_str_contains(value, expected, is_pattern, message)
390+
log.info('Assert string %s contains %s', value, expected)
391+
387392
M.assert_type(value, 'string', nil, 2)
388393
M.assert_type(expected, 'string', nil, 2)
389394

@@ -526,6 +531,7 @@ end
526531
-- @param ... arguments for function
527532
function M.assert_error_msg_contains(expected_partial, fn, ...)
528533
local no_error, error_msg = pcall(fn, ...)
534+
log.info('Assert error message %s contains %s', error_msg, expected_partial)
529535
if no_error then
530536
local failure_message = string.format(
531537
'Function successfully returned: %s\nExpected error containing: %s',

luatest/helpers.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
local clock = require('clock')
66
local fiber = require('fiber')
7-
local log = require('log')
7+
local log = require('luatest.log')
88

99
local helpers = {}
1010

@@ -69,8 +69,7 @@ function helpers.retrying(config, fn, ...)
6969
if (clock.time() - started_at) > timeout then
7070
return fn(...)
7171
end
72-
log.debug('Retrying in ' .. delay .. ' sec. due to error:')
73-
log.debug(result)
72+
log.verbose('Retrying in %d sec due to error: %s', delay, result)
7473
fiber.sleep(delay)
7574
end
7675
end

luatest/hooks.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local log = require('luatest.log')
12
local utils = require('luatest.utils')
23
local comparator = require('luatest.comparator')
34

@@ -129,6 +130,7 @@ local function run_group_hooks(runner, group, hooks_type)
129130
end
130131

131132
local function run_test_hooks(self, test, hooks_type, legacy_name)
133+
log.verbose('Run hook %s', hooks_type)
132134
local group = test.group
133135
local hook
134136
-- Support for group.setup/teardown methods (legacy API)
@@ -143,6 +145,7 @@ local function run_test_hooks(self, test, hooks_type, legacy_name)
143145
end
144146

145147
local function run_named_test_hooks(self, test, hooks_type)
148+
log.verbose('Run hook %s', hooks_type)
146149
local group = test.group
147150
local hook = group['run_' .. hooks_type]
148151
if hook then
@@ -172,7 +175,9 @@ function export.patch_runner(Runner)
172175
run_named_test_hooks(self, test, 'before_test')
173176

174177
if test:is('success') then
178+
log.info('Start test %s', test.name)
175179
super(self, test, ...)
180+
log.info('End test %s', test.name)
176181
end
177182

178183
run_named_test_hooks(self, test, 'after_test')

luatest/process.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ local errno = require('errno')
33
local fun = require('fun')
44
local ffi = require('ffi')
55
local fio = require('fio')
6-
local log = require('log')
76

7+
local log = require('luatest.log')
88
local Class = require('luatest.class')
99
local OutputBeautifier = require('luatest.output_beautifier')
1010

@@ -77,7 +77,7 @@ function Process.mt:initialize()
7777
self._pid_ull = ffi.cast('void*', 0ULL + self.pid)
7878
ffi.gc(self._pid_ull, function(x)
7979
local pid = tonumber(ffi.cast(ffi.typeof(0ULL), x))
80-
log.debug("Killing GC'ed process " .. pid)
80+
log.debug("Killing GC'ed process %d", pid)
8181
Process.kill_pid(pid, nil, {quiet = true})
8282
end)
8383
end

luatest/replica_proxy.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
local checks = require('checks')
66
local fiber = require('fiber')
77
local fio = require('fio')
8-
local log = require('log')
98
local socket = require('socket')
109
local uri = require('uri')
1110

11+
local log = require('luatest.log')
1212
local utils = require('luatest.utils')
1313
local Connection = require('luatest.replica_conn')
1414

luatest/replica_set.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
local checks = require('checks')
66
local fio = require('fio')
7-
local log = require('log')
87

98
local helpers = require('luatest.helpers')
109
local Server = require('luatest.server')
10+
local log = require('luatest.log')
1111
local utils = require('luatest.utils')
1212

1313
local ReplicaSet = {}
@@ -72,6 +72,7 @@ function ReplicaSet:initialize()
7272
else
7373
self.servers = {}
7474
end
75+
log.info('Replica set %s initialized', self.id)
7576
end
7677

7778
--- Build a server object for the replica set.
@@ -147,8 +148,7 @@ function ReplicaSet:delete_server(alias)
147148
if server_index then
148149
table.remove(self.servers, server_index)
149150
else
150-
log.warn(('Server with alias "%s" does not exist in replica set')
151-
:format(alias))
151+
log.warn('Server %s does not exist in replica set %s', alias, self.id)
152152
end
153153
end
154154

@@ -174,6 +174,7 @@ function ReplicaSet:start(opts)
174174
server:wait_until_ready()
175175
end
176176
end
177+
log.info('All servers started in replica set %s', self.id)
177178
end
178179

179180
--- Stop all servers in the replica set.
@@ -235,6 +236,7 @@ function ReplicaSet:wait_for_fullmesh(opts)
235236
end
236237
end
237238
end, self)
239+
log.info('Full mesh is ready in replica set %s', self.id)
238240
end
239241

240242
return ReplicaSet

luatest/runner.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
local clock = require('clock')
66
local fio = require('fio')
7-
local log = require('log')
87

98
local assertions = require('luatest.assertions')
109
local capturing = require('luatest.capturing')
1110
local Class = require('luatest.class')
1211
local GenericOutput = require('luatest.output.generic')
1312
local hooks = require('luatest.hooks')
13+
local log = require('luatest.log')
1414
local loader = require('luatest.loader')
1515
local pp = require('luatest.pp')
1616
local Server = require('luatest.server')
@@ -96,7 +96,7 @@ function Runner.run(args, options)
9696
-- Initialize logging for luatest runner.
9797
-- The log format will be as follows:
9898
-- YYYY-MM-DD HH:MM:SS.ZZZ [ID] main/.../luatest I> ...
99-
log.cfg{
99+
require('log').cfg{
100100
log = log_cfg,
101101
level = options.log_level or 5,
102102
}
@@ -337,11 +337,13 @@ function Runner.mt:bootstrap()
337337
load_tests(path)
338338
end
339339
self.groups = self.luatest.groups
340+
log.verbose('Bootstrap finished: %d test(s), %d group(s)', #self.paths, #self.groups)
340341
end
341342

342343
function Runner.mt:cleanup()
343344
if not self.no_clean then
344345
fio.rmtree(Server.vardir)
346+
log.verbose('Directory %s removed via cleanup procedure', Server.vardir)
345347
end
346348
end
347349

@@ -505,6 +507,7 @@ function Runner.mt:run_tests(tests_list)
505507
end
506508
rawget(_G, 'current_test').value = test
507509
self:run_test(test)
510+
log.verbose('Test %s marked as %s', test.name, test.status)
508511
if self.result.aborted then
509512
break
510513
end

luatest/server.lua

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ local fio = require('fio')
1010
local fun = require('fun')
1111
local http_client = require('http.client')
1212
local json = require('json')
13-
local log = require('log')
1413
local net_box = require('net.box')
1514
local tarantool = require('tarantool')
1615
local uri = require('uri')
@@ -19,6 +18,7 @@ local _, luacov_runner = pcall(require, 'luacov.runner') -- luacov may not be in
1918
local assertions = require('luatest.assertions')
2019
local HTTPResponse = require('luatest.http_response')
2120
local Process = require('luatest.process')
21+
local log = require('luatest.log')
2222
local utils = require('luatest.utils')
2323

2424
local DEFAULT_VARDIR = '/tmp/t'
@@ -117,6 +117,7 @@ function Server:new(object, extra)
117117
if not object.tests[t.name] then
118118
object.tests[t.name] = t
119119
t.servers[object.id] = object
120+
log.verbose('Server %s used in %s test', object.alias, t.name)
120121
end
121122
end
122123
return v(...)
@@ -324,7 +325,6 @@ function Server:start(opts)
324325
for _, v in ipairs(args) do
325326
table.insert(log_cmd, string.format('%q', v))
326327
end
327-
log.debug(table.concat(log_cmd, ' '))
328328

329329
self.process = Process:start(command, args, env, {
330330
chdir = self.chdir,
@@ -344,7 +344,7 @@ function Server:start(opts)
344344
self:wait_until_ready()
345345
end
346346

347-
log.debug('Started server PID: ' .. self.process.pid)
347+
log.info('Server %s (pid: %d) started', self.alias, self.process.pid)
348348
end
349349

350350
--- Restart the server with the given parameters.
@@ -378,7 +378,7 @@ function Server:restart(params, opts)
378378
}, {wait_until_ready = '?boolean'})
379379

380380
if not self.process then
381-
log.warn("Process wasn't started")
381+
log.warn('Cannot restart server %s since its process not started', self.alias)
382382
end
383383
self:stop()
384384

@@ -387,7 +387,7 @@ function Server:restart(params, opts)
387387
end
388388

389389
self:start(opts)
390-
log.debug('Restarted server PID: ' .. self.process.pid)
390+
log.info('Server %s (pid: %d) restarted', self.alias, self.process.pid)
391391
end
392392

393393
-- Save server artifacts by copying the working directory.
@@ -396,6 +396,7 @@ end
396396
-- following string: `Failed to copy artifacts for server (alias: <alias>, workdir: <workdir>)`.
397397
function Server:save_artifacts()
398398
if self.artifacts_saved then
399+
log.verbose('Artifacts of server %s already saved to %s', self.alias, self.artifacts)
399400
return
400401
end
401402
local ok, err = fio.copytree(self.workdir, self.artifacts)
@@ -404,12 +405,16 @@ function Server:save_artifacts()
404405
:format(self.alias, fio.basename(self.workdir))
405406
log.error(('%s: %s'):format(self.artifacts, err))
406407
end
408+
log.verbose('Artifacts of server %s saved from %s to %s',
409+
self.alias, self.workdir, self.artifacts)
407410
self.artifacts_saved = true
408411
end
409412

410413
-- Wait until the given condition is `true` (anything except `false` and `nil`).
411414
-- Throws an error when the server process is terminated or timeout exceeds.
412415
local function wait_for_condition(cond_desc, server, func, ...)
416+
log.verbose('Wait for %s condition for server %s (pid: %d) within %d sec',
417+
cond_desc, server.alias, server.process.pid, WAIT_TIMEOUT)
413418
local deadline = clock.time() + WAIT_TIMEOUT
414419
while true do
415420
if not server.process:is_alive() then
@@ -437,6 +442,7 @@ function Server:stop()
437442
self:coverage('shutdown')
438443
end
439444
self.net_box:close()
445+
log.verbose('Connection to server %s (pid: %d) closed', self.alias, self.process.pid)
440446
self.net_box = nil
441447
end
442448

@@ -470,7 +476,7 @@ function Server:stop()
470476
)
471477
)
472478
end
473-
log.debug('Killed server process PID ' .. self.process.pid)
479+
log.info('Process of server %s (pid: %d) killed', self.alias, self.process.pid)
474480
self.process = nil
475481
end
476482
end
@@ -774,6 +780,8 @@ function Server:grep_log(pattern, bytes_num, opts)
774780
return rawget(_G, 'box_cfg_log_file') or box.cfg.log end)
775781
local file = fio.open(filename, {'O_RDONLY', 'O_NONBLOCK'})
776782

783+
log.verbose('Trying to grep %s in server\'s log file %s', pattern, filename)
784+
777785
local function fail(msg)
778786
local err = errno.strerror()
779787
if file ~= nil then

0 commit comments

Comments
 (0)