Skip to content

Commit b49fd42

Browse files
committed
Per-module logging
1 parent 1cf9f6e commit b49fd42

File tree

4 files changed

+183
-11
lines changed

4 files changed

+183
-11
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
local fio = require('fio')
2+
local server = require('luatest.server')
3+
local t = require('luatest')
4+
local g = t.group()
5+
6+
g.before_each(function(cg)
7+
cg.server = server:new {
8+
workdir = fio.cwd() .. '/tmp',
9+
box_cfg = { log_level = 'warn',
10+
log_modules = {
11+
module1 = 'verbose',
12+
module2 = 'error' }
13+
}
14+
}
15+
cg.server:start()
16+
cg.server:exec(function()
17+
-- Creates new loggers --
18+
module1_log = require('log').new('module1')
19+
module2_log = require('log').new('module2')
20+
21+
-- Prints 'info' messages --
22+
module1_log.info('Info message from module1')
23+
--[[
24+
2023-07-20 11:04:36.166 [16300] main/103/interactive/module1 I> Info message from module1
25+
---
26+
...
27+
--]]
28+
29+
-- Swallows 'debug' messages --
30+
module1_log.debug('Debug message from module1')
31+
--[[
32+
---
33+
...
34+
--]]
35+
36+
-- Swallows 'info' messages --
37+
module2_log.info('Info message from module2')
38+
--[[
39+
---
40+
...
41+
--]]
42+
end)
43+
end)
44+
45+
g.after_each(function(cg)
46+
cg.server:stop()
47+
cg.server:drop()
48+
end)
49+
50+
local function find_in_log(cg, str, must_be_present)
51+
t.helpers.retrying({ timeout = 0.3, delay = 0.1 }, function()
52+
local found = cg.server:grep_log(str) ~= nil
53+
t.assert(found == must_be_present)
54+
end)
55+
end
56+
57+
g.test_log_contains_reached_states = function(cg)
58+
find_in_log(cg, 'Info message from module1', true)
59+
find_in_log(cg, 'Debug message from module1', false)
60+
find_in_log(cg, 'Info message from module2', false)
61+
end
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
local fio = require('fio')
2+
local server = require('luatest.server')
3+
local t = require('luatest')
4+
local g = t.group()
5+
6+
g.before_each(function(cg)
7+
cg.server = server:new {
8+
workdir = fio.cwd() .. '/tmp',
9+
box_cfg = { log_level = 'warn' }
10+
}
11+
cg.server:start()
12+
cg.server:exec(function()
13+
log = require('log')
14+
15+
-- Prints 'warn' messages --
16+
log.warn('Warning message')
17+
--[[
18+
2023-07-20 11:03:57.029 [16300] main/103/interactive/tarantool [C]:-1 W> Warning message
19+
---
20+
...
21+
--]]
22+
23+
-- Swallows 'debug' messages --
24+
log.debug('Debug message')
25+
--[[
26+
---
27+
...
28+
--]]
29+
end)
30+
end)
31+
32+
g.after_each(function(cg)
33+
cg.server:stop()
34+
cg.server:drop()
35+
end)
36+
37+
local function find_in_log(cg, str, must_be_present)
38+
t.helpers.retrying({ timeout = 0.3, delay = 0.1 }, function()
39+
local found = cg.server:grep_log(str) ~= nil
40+
t.assert(found == must_be_present)
41+
end)
42+
end
43+
44+
g.test_log_contains_reached_states = function(cg)
45+
find_in_log(cg, 'Warning message', true)
46+
find_in_log(cg, 'Debug message', false)
47+
end

doc/reference/configuration/cfg_logging.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ application.
99
* :ref:`log_nonblock <cfg_logging-log_nonblock>`
1010
* :ref:`too_long_threshold <cfg_logging-too_long_threshold>`
1111
* :ref:`log_format <cfg_logging-log_format>`
12+
* :ref:`log_modules <cfg_logging-log_modules>`
1213

1314
.. _cfg_logging-log_level:
1415

@@ -208,6 +209,30 @@ application.
208209
| Environment variable: TT_LOG_FORMAT
209210
| Dynamic: **yes**
210211
212+
213+
.. _cfg_logging-log_modules:
214+
215+
.. confval:: log_modules
216+
217+
Since version :doc:`2.11.0 </release/2.11.0>`.
218+
Configure the specified log levels for different modules.
219+
You can learn more about log levels from the :ref:`log_level <cfg_logging-log_level>` option description.
220+
221+
The example below shows how to set the ``verbose`` level for ``module1`` and the ``error`` level for ``module2``:
222+
223+
.. literalinclude:: /code_snippets/test/logging/log_modules_test.lua
224+
:language: lua
225+
:lines: 9-13
226+
:dedent:
227+
228+
Then, you can pass the ``box_cfg`` parameter to the ``box.cfg()`` function.
229+
Learn how to create a custom logger in the :ref:`log.new() <log-new>` function description.
230+
231+
| Type: table
232+
| Default: blank
233+
| Environment variable: TT_LOG_MODULES
234+
| Dynamic: **yes**
235+
211236
.. _cfg_logging-logging_example:
212237

213238
*********************

doc/reference/reference_lua/log.rst

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,35 @@ Below is a list of all ``log`` functions.
3131
+--------------------------------------+---------------------------------+
3232
| Name | Use |
3333
+======================================+=================================+
34-
| :ref:`log.cfg({}) | Configures a logger |
34+
| :ref:`log.cfg({}) | Configure a logger |
3535
| <log-cfg>` | |
3636
+--------------------------------------+---------------------------------+
3737
| :ref:`log.error() | |
3838
| <log-ug_message>` |br| | |
3939
| :ref:`log.warn() | |
4040
| <log-ug_message>` |br| | |
41-
| :ref:`log.info() | Logs a message with the |
41+
| :ref:`log.info() | Log a message with the |
4242
| <log-ug_message>` |br| | specified level |
4343
| :ref:`log.verbose() | |
4444
| <log-ug_message>` |br| | |
4545
| :ref:`log.debug() | |
4646
| <log-ug_message>` | |
4747
+--------------------------------------+---------------------------------+
48-
| :ref:`log.pid() | Gets the PID of a logger |
48+
| :ref:`log.pid() | Get the PID of a logger |
4949
| <log-pid>` | |
5050
+--------------------------------------+---------------------------------+
51-
| :ref:`log.rotate() | Rotates a log file |
51+
| :ref:`log.rotate() | Rotate a log file |
5252
| <log-rotate>` | |
5353
+--------------------------------------+---------------------------------+
54+
| :ref:`log.new() | Create a new logger with |
55+
| <log-new>` | the specified name. |
56+
+--------------------------------------+---------------------------------+
5457

5558
.. _log-cfg:
5659

5760
.. function:: log.cfg({})
5861

59-
Allows you to configure logging options.
62+
Configure logging options.
6063
The following options are available:
6164

6265
* ``level``: Specifies the level of detail the log has.
@@ -94,16 +97,16 @@ Below is a list of all ``log`` functions.
9497
verbose(message)
9598
debug(message)
9699

97-
Logs a message with the specified logging level.
100+
Log a message with the specified logging level.
98101
You can learn more about the available levels from the
99102
:ref:`log_level <cfg_logging-log_level>` property description.
100103

101104
The example below shows how to log a message with the ``info`` level:
102105

103-
.. code-block:: lua
104-
105-
log = require('log')
106-
log.info('Hello, world!')
106+
.. literalinclude:: /code_snippets/test/logging/log_test.lua
107+
:language: lua
108+
:lines: 13-21
109+
:dedent:
107110

108111
:param any message: A log message.
109112

@@ -146,8 +149,44 @@ Below is a list of all ``log`` functions.
146149

147150
.. function:: rotate()
148151

149-
Rotates the log.
152+
Rotate the log.
150153
For example, you need to call this function to continue logging after a log rotation program
151154
renames or moves a file with the latest logs.
152155

153156
:return: nil
157+
158+
.. _log-new:
159+
160+
.. function:: new(name)
161+
162+
Create a new logger with the specified name.
163+
You can configure a specific log level for a new logger using the :ref:`log_modules <cfg_logging-log_modules>` configuration property.
164+
165+
:param string name: a logger name
166+
:return: a logger instance
167+
168+
**Example:**
169+
170+
The code snippet below shows how to set the ``verbose`` level for ``module1`` and the ``error`` level for ``module2``:
171+
172+
.. literalinclude:: /code_snippets/test/logging/log_modules_test.lua
173+
:language: lua
174+
:lines: 9-13
175+
:dedent:
176+
177+
To create the ``module1`` and ``module1`` modules, call the ``new()`` function:
178+
179+
.. literalinclude:: /code_snippets/test/logging/log_modules_test.lua
180+
:language: lua
181+
:lines: 17-19
182+
:dedent:
183+
184+
Then, you can call functions corresponding to different logging levels to make sure
185+
that events with severities above or equal to the given levels are shown:
186+
187+
.. literalinclude:: /code_snippets/test/logging/log_modules_test.lua
188+
:language: lua
189+
:lines: 21-41
190+
:dedent:
191+
192+
At the same time, the events with severities below the specified levels are swallowed.

0 commit comments

Comments
 (0)