Skip to content

Commit 6c5c8b7

Browse files
committed
Print luatest logs to stdout when unified log is disabled
If unified logging is enabled, logs written by the runner are written both to its own log file and to the unified log file. This is consistent with how logs written by test processes are handled. However, if unified logging is disabled, then runner logs are written only to its log file while test process logs are also written to stdout and captured if capturing is enabled. Let's write runner logs to stdout in this case as well because they can be useful for debugging. While we are at it, let's get rid of using `tee` for logging: we can redirect all logs to the output beautifier directly and instruct the latter to write them to a file. No changelog because the feature was added by commit f8a1c10 ("Add logging to unified file"), which hasn't been released yet.
1 parent 2f16ac7 commit 6c5c8b7

File tree

4 files changed

+70
-42
lines changed

4 files changed

+70
-42
lines changed

luatest/log.lua

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,69 @@
1+
local checks = require('checks')
2+
local fio = require('fio')
13
local tarantool_log = require('log')
24

5+
local OutputBeautifier = require('luatest.output_beautifier')
36
local utils = require('luatest.utils')
47

58
-- Utils for logging
69
local log = {}
710
local default_level = 'info'
11+
local is_initialized = false
12+
13+
function log.initialize(options)
14+
checks({
15+
vardir = 'string',
16+
log_file = '?string',
17+
log_prefix = '?string',
18+
})
19+
if is_initialized then
20+
return
21+
end
22+
23+
local vardir = options.vardir
24+
local luatest_log_prefix = options.log_prefix or 'luatest'
25+
local luatest_log_file = fio.pathjoin(vardir, luatest_log_prefix .. '.log')
26+
local unified_log_file = options.log_file
27+
28+
fio.mktree(vardir)
29+
30+
if unified_log_file then
31+
-- Save the file descriptor as a global variable to use it in
32+
-- the `output_beautifier` module.
33+
local fh = fio.open(unified_log_file, {'O_CREAT', 'O_WRONLY', 'O_TRUNC'},
34+
tonumber('640', 8))
35+
rawset(_G, 'log_file', {fh = fh})
36+
end
37+
38+
local output_beautifier = OutputBeautifier:new({
39+
file = luatest_log_file,
40+
prefix = luatest_log_prefix,
41+
})
42+
output_beautifier:enable()
43+
44+
-- Redirect all logs to the pipe created by OutputBeautifier.
45+
local log_cfg = string.format('/dev/fd/%d', output_beautifier.pipes.stdout[1])
46+
47+
-- Logging cannot be initialized without configuring the `box` engine
48+
-- on a version less than 2.5.1 (see more details at [1]). Otherwise,
49+
-- this causes the `attempt to call field 'cfg' (a nil value)` error,
50+
-- so there are the following limitations:
51+
-- 1. There is no `luatest.log` file (but logs are still available
52+
-- in stdout and in the `run.log` file);
53+
-- 2. All logs from luatest are non-formatted and look like:
54+
--
55+
-- luatest | My log message
56+
--
57+
-- [1]: https://github.com/tarantool/tarantool/issues/689
58+
if utils.version_current_ge_than(2, 5, 1) then
59+
-- Initialize logging for luatest runner.
60+
-- The log format will be as follows:
61+
-- YYYY-MM-DD HH:MM:SS.ZZZ [ID] main/.../luatest I> ...
62+
require('log').cfg{log = log_cfg}
63+
end
64+
65+
is_initialized = true
66+
end
867

968
local function _log(level, msg, ...)
1069
if utils.version_current_ge_than(2, 5, 1) then

luatest/runner.lua

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ local Server = require('luatest.server')
1717
local sorted_pairs = require('luatest.sorted_pairs')
1818
local TestInstance = require('luatest.test_instance')
1919
local utils = require('luatest.utils')
20-
local OutputBeautifier = require('luatest.output_beautifier')
2120

2221
local ROCK_VERSION = require('luatest.VERSION')
2322

@@ -58,47 +57,11 @@ function Runner.run(args, options)
5857
end
5958
options = utils.merge(options.luatest.configure(), Runner.parse_cmd_line(args), options)
6059

61-
local log_prefix = options.log_prefix or 'luatest'
62-
local log_cfg = string.format("%s.log", log_prefix)
63-
64-
fio.mktree(Server.vardir)
65-
log_cfg = fio.pathjoin(Server.vardir, log_cfg)
66-
67-
if options.log_file then
68-
-- Save the file descriptor as a global variable to use it
69-
-- in the `output_beautifier` module: this module is connected to the
70-
-- the `server` module. We cannot link the `server` module to the `runner`
71-
-- module to explicitly give this parameter.
72-
local fh = fio.open(options.log_file, {'O_CREAT', 'O_WRONLY', 'O_TRUNC'},
73-
tonumber('640', 8))
74-
rawset(_G, 'log_file', {fh = fh})
75-
76-
local output_beautifier = OutputBeautifier:new({prefix = log_prefix})
77-
output_beautifier:enable()
78-
79-
-- `tee` copy logs to file and also to standard output, but we need
80-
-- process all captured data through the OutputBeatifier object.
81-
-- So we redirect stdout to the pipe created by OutputBeautifier.
82-
log_cfg = string.format("| tee %s > /dev/fd/%d",
83-
log_cfg, output_beautifier.pipes.stdout[1])
84-
end
85-
-- Logging cannot be initialized without configuring the `box` engine
86-
-- on a version less than 2.5.1 (see more details at [1]). Otherwise,
87-
-- this causes the `attempt to call field 'cfg' (a nil value)` error,
88-
-- so there are the following limitations:
89-
-- 1. There is no `luatest.log` file (but logs are still available
90-
-- in stdout and in the `run.log` file);
91-
-- 2. All logs from luatest are non-formatted and look like:
92-
--
93-
-- luatest | My log message
94-
--
95-
-- [1]: https://github.com/tarantool/tarantool/issues/689
96-
if utils.version_current_ge_than(2, 5, 1) then
97-
-- Initialize logging for luatest runner.
98-
-- The log format will be as follows:
99-
-- YYYY-MM-DD HH:MM:SS.ZZZ [ID] main/.../luatest I> ...
100-
require('log').cfg{log = log_cfg}
101-
end
60+
log.initialize({
61+
vardir = Server.vardir,
62+
log_file = options.log_file,
63+
log_prefix = options.log_prefix,
64+
})
10265

10366
if options.help then
10467
print(Runner.USAGE)

test/capture_test.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ local g = t.group()
55
local Capture = require('luatest.capture')
66
local capture = Capture:new()
77

8+
-- Disable luatest logging to avoid capturing it.
9+
require('luatest.log').info = function() end
10+
811
g.setup = function() capture:enable() end
912
g.teardown = function()
1013
capture:flush()

test/capturing_test.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ local helper = require('test.helpers.general')
55
local Capture = require('luatest.capture')
66
local capture = Capture:new()
77

8+
-- Disable luatest logging to avoid capturing it.
9+
require('luatest.log').info = function() end
10+
811
g.setup = function() capture:enable() end
912
g.teardown = function()
1013
capture:flush()

0 commit comments

Comments
 (0)