Skip to content

Commit 1e813fb

Browse files
author
p0rtale
committed
roles: add log_requests option
1 parent 5ae99b5 commit 1e813fb

File tree

5 files changed

+109
-2
lines changed

5 files changed

+109
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88

99
### Added
1010

11+
- `log_requests` option into `roles.httpd`.
12+
1113
### Changed
1214

1315
### Fixed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,11 +536,14 @@ roles_cfg:
536536
listen: 8081
537537
additional:
538538
listen: '127.0.0.1:8082'
539+
log_requests: 'verbose'
539540
```
540541
541542
Server address should be provided either as a URI or as a single port
542543
(in this case, `0.0.0.0` address is used).
543544

545+
You can configure HTTP request logging level using `log_requests` parameter (e.g. "verbose"). Supported values are "info", "verbose" and "debug". By default, requests are logged at "info" level.
546+
544547
User can access every working HTTP server from the configuration by name,
545548
using `require('roles.httpd').get_server(name)` method.
546549
If the `name` argument is `nil`, the default server is returned

roles/httpd.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local checks = require('checks')
22
local urilib = require('uri')
3+
local log = require('log')
34
local http_server = require('http.server')
45

56
local M = {
@@ -75,7 +76,22 @@ end
7576
-- parse_params returns table with set options from config to pass
7677
-- it into new() function.
7778
local function parse_params(node)
79+
local log_requests = node.log_requests
80+
if type(log_requests) == "string" then
81+
local level = log_requests:lower()
82+
if level == "info" then
83+
log_requests = log.info
84+
elseif level == "verbose" then
85+
log_requests = log.verbose
86+
elseif level == "debug" then
87+
log_requests = log.debug
88+
else
89+
error("invalid log_requests: " .. log_requests)
90+
end
91+
end
92+
7893
return {
94+
log_requests = log_requests,
7995
ssl_cert_file = node.ssl_cert_file,
8096
ssl_key_file = node.ssl_key_file,
8197
ssl_password = node.ssl_password,

test/integration/httpd_role_test.lua

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,20 @@ local http_client = require('http.client').new()
99

1010
local helpers = require('test.helpers')
1111

12-
local g = t.group(nil, t.helpers.matrix({use_tls = {true, false}}))
12+
local LOG_LEVEL = {
13+
INFO = 5,
14+
VERBOSE = 6,
15+
DEBUG = 7,
16+
}
17+
18+
local g = t.group(nil, t.helpers.matrix({
19+
use_tls = {true, false},
20+
log_level = {
21+
LOG_LEVEL.INFO,
22+
LOG_LEVEL.VERBOSE,
23+
LOG_LEVEL.DEBUG,
24+
},
25+
}))
1326

1427
local ssl_data_dir = fio.abspath(fio.pathjoin(helpers.get_testdir_path(), "ssl_data"))
1528

@@ -36,10 +49,16 @@ local config = {
3649
['roles.httpd'] = {
3750
default = {
3851
listen = 13000,
52+
log_requests = 'info',
3953
},
4054
additional = {
4155
listen = 13001,
42-
}
56+
log_requests = 'verbose',
57+
},
58+
additional_debug = {
59+
listen = 13002,
60+
log_requests = 'debug',
61+
},
4362
},
4463
['test.mocks.mock_role'] = {
4564
{
@@ -49,6 +68,10 @@ local config = {
4968
id = 2,
5069
name = 'additional',
5170
},
71+
{
72+
id = 3,
73+
name = 'additional_debug',
74+
}
5275
},
5376
},
5477
instances = {
@@ -75,6 +98,8 @@ g.before_each(function(cg)
7598

7699
local dir = treegen.prepare_directory({}, {})
77100

101+
os.setenv('TT_LOG_LEVEL', tostring(cg.params.log_level))
102+
78103
local cfg = config
79104
if cg.params.use_tls then
80105
cfg = tls_config
@@ -146,3 +171,32 @@ g.test_change_server_addr_on_the_run = function(cg)
146171
t.assert_equals(resp.status, 200, 'response not 200')
147172
t.assert_equals(resp.body, 'pong')
148173
end
174+
175+
g.test_log_requests = function(cg)
176+
t.skip_if(cg.params.use_tls)
177+
178+
local function make_request(port)
179+
local resp = http_client:get(string.format('http://localhost:%d/ping', port))
180+
t.assert_equals(resp.status, 200, 'response not 200')
181+
end
182+
183+
local function assert_should_log(expected)
184+
local grep_res = cg.server:grep_log('GET /ping', math.huge)
185+
if expected then
186+
t.assert(grep_res)
187+
else
188+
t.assert_not(grep_res)
189+
end
190+
end
191+
192+
local log_level = tonumber(cg.params.log_level)
193+
194+
make_request(13002)
195+
assert_should_log(log_level >= LOG_LEVEL.DEBUG)
196+
197+
make_request(13001)
198+
assert_should_log(log_level >= LOG_LEVEL.VERBOSE)
199+
200+
make_request(13000)
201+
assert_should_log(log_level >= LOG_LEVEL.INFO)
202+
end

test/unit/httpd_role_test.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local g = t.group()
44
local httpd_role = require('roles.httpd')
55
local helpers = require('test.helpers')
66
local fio = require('fio')
7+
local log = require('log')
78

89
local ssl_data_dir = fio.abspath(fio.pathjoin(helpers.get_testdir_path(), "ssl_data"))
910

@@ -209,8 +210,18 @@ local validation_cases = {
209210
},
210211
err = "ssl_key_file and ssl_cert_file must be set to enable TLS",
211212
},
213+
["invalid_log_requests"] = {
214+
cfg = {
215+
server = {
216+
listen = "localhost:123",
217+
log_requests = "yes",
218+
},
219+
},
220+
err = "invalid log_requests",
221+
}
212222
}
213223

224+
214225
for name, case in pairs(validation_cases) do
215226
local test_name = ('test_validate_http_%s%s'):format(
216227
(case.err ~= nil) and 'fails_on_' or 'success_for_',
@@ -311,3 +322,24 @@ g.test_edit_server_address = function()
311322
t.assert(result)
312323
t.assert_equals(result.port, cfg[httpd_role.DEFAULT_SERVER_NAME].listen)
313324
end
325+
326+
g.test_log_requests = function()
327+
local cfg = {
328+
server1 = {
329+
listen = 13002,
330+
log_requests = false,
331+
},
332+
server2 = {
333+
listen = 13003,
334+
log_requests = log.verbose,
335+
},
336+
}
337+
338+
httpd_role.apply(cfg)
339+
340+
for name, body in pairs(cfg) do
341+
local res = httpd_role.get_server(name)
342+
t.assert(res)
343+
t.assert_equals(res.options.log_requests, body.log_requests)
344+
end
345+
end

0 commit comments

Comments
 (0)