Skip to content

Commit 8494d84

Browse files
cyrilloskyukhin
authored andcommitted
lua/log: accept symbolic logging levels
Currently `log` module accepts only numeric values of logging levels. I turn `box.cfg` interface supports symbolic names (such as 'fatal', 'crit' and etc). Thus we should support the same in `log` module. Closes tarantool#5882 Reported-by: Alexander Turenko <[email protected]> Acked-by: Alexander Turenko <[email protected]> Acked-by: Serge Petrenko <[email protected]> Signed-off-by: Cyrill Gorcunov <[email protected]>
1 parent 68135b6 commit 8494d84

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## feature/lua/log
2+
3+
* Implemented support of symbolic log levels representation
4+
in `log` module (gh-5882). Now it is possible to specify
5+
levels the same way as in `box.cfg{}` call. For example
6+
instead of
7+
``` Lua
8+
require('log').cfg{level = 6}
9+
```
10+
One can use
11+
``` Lua
12+
require('log').cfg{level = 'verbose'}
13+
```

src/lua/log.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,12 @@ local function load_cfg(self, cfg)
499499
local m = "log.cfg: \'%s\' %s"
500500
error(m:format('level', msg))
501501
end
502+
-- Convert level to a numeric value since
503+
-- low level api operates with numbers only.
504+
if type(cfg.level) == 'string' then
505+
assert(log_level_keys[cfg.level] ~= nil)
506+
cfg.level = log_level_keys[cfg.level]
507+
end
502508
end
503509

504510
if cfg.nonblock ~= nil then

test/app-tap/logmod.test.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env tarantool
2+
3+
local test = require('tap').test('log')
4+
local log = require('log')
5+
6+
test:plan(8)
7+
8+
log.log_format('plain')
9+
10+
-- Test symbolic names for loglevels
11+
local _, err = pcall(log.cfg, {level='fatal'})
12+
test:ok(err == nil and log.cfg.level == 0, 'both got fatal')
13+
14+
_, err = pcall(log.cfg, {level='syserror'})
15+
test:ok(err == nil and log.cfg.level == 1, 'got syserror')
16+
17+
_, err = pcall(log.cfg, {level='error'})
18+
test:ok(err == nil and log.cfg.level == 2, 'got error')
19+
20+
_, err = pcall(log.cfg, {level='crit'})
21+
test:ok(err == nil and log.cfg.level == 3, 'got crit')
22+
23+
_, err = pcall(log.cfg, {level='warn'})
24+
test:ok(err == nil and log.cfg.level == 4, 'got warn')
25+
26+
_, err = pcall(log.cfg, {level='info'})
27+
test:ok(err == nil and log.cfg.level == 5, 'got info')
28+
29+
_, err = pcall(log.cfg, {level='verbose'})
30+
test:ok(err == nil and log.cfg.level == 6, 'got verbose')
31+
32+
_, err = pcall(log.cfg, {level='debug'})
33+
test:ok(err == nil and log.cfg.level == 7, 'got debug')
34+
35+
test:check()
36+
os.exit()

0 commit comments

Comments
 (0)