Skip to content

Commit 7dfd176

Browse files
p0rtalethemilchenko
authored andcommitted
roles: add log_requests option
1 parent 5ae99b5 commit 7dfd176

File tree

5 files changed

+126
-2
lines changed

5 files changed

+126
-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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,11 +536,16 @@ 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").
546+
Supported values are "debug", "verbose", "info", "warn" and "error".
547+
By default, requests are logged at "info" level.
548+
544549
User can access every working HTTP server from the configuration by name,
545550
using `require('roles.httpd').get_server(name)` method.
546551
If the `name` argument is `nil`, the default server is returned

roles/httpd.lua

Lines changed: 22 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,28 @@ 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 == "error" then
83+
log_requests = log.error
84+
elseif level == "warn" then
85+
log_requests = log.warn
86+
elseif level == "info" then
87+
log_requests = log.info
88+
elseif level == "verbose" then
89+
log_requests = log.verbose
90+
elseif level == "debug" then
91+
log_requests = log.debug
92+
else
93+
error("invalid log_requests: " .. log_requests)
94+
end
95+
elseif log_requests ~= nil then
96+
error("log_requests option should be a string")
97+
end
98+
7899
return {
100+
log_requests = log_requests,
79101
ssl_cert_file = node.ssl_cert_file,
80102
ssl_key_file = node.ssl_key_file,
81103
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 = {
@@ -80,6 +103,8 @@ g.before_each(function(cg)
80103
cfg = tls_config
81104
end
82105

106+
cfg.log = {level = cg.params.log_level}
107+
83108
local config_file = treegen.write_file(dir, 'config.yaml',
84109
yaml.encode(cfg))
85110
local opts = {config_file = config_file, chdir = dir}
@@ -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(address)
179+
local resp = http_client:get(string.format('http://%s/ping', address))
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('localhost:13002')
195+
assert_should_log(log_level >= LOG_LEVEL.DEBUG)
196+
197+
make_request('localhost:13001')
198+
assert_should_log(log_level >= LOG_LEVEL.VERBOSE)
199+
200+
make_request('localhost:13000')
201+
assert_should_log(log_level >= LOG_LEVEL.INFO)
202+
end

test/unit/httpd_role_test.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,24 @@ local validation_cases = {
209209
},
210210
err = "ssl_key_file and ssl_cert_file must be set to enable TLS",
211211
},
212+
["log_requests_invalid_string"] = {
213+
cfg = {
214+
server = {
215+
listen = "localhost:123",
216+
log_requests = "yes",
217+
},
218+
},
219+
err = "invalid log_requests",
220+
},
221+
["log_requests_invalid_type"] = {
222+
cfg = {
223+
server = {
224+
listen = "localhost:123",
225+
log_requests = 42,
226+
},
227+
},
228+
err = "log_requests option should be a string",
229+
}
212230
}
213231

214232
for name, case in pairs(validation_cases) do
@@ -311,3 +329,26 @@ g.test_edit_server_address = function()
311329
t.assert(result)
312330
t.assert_equals(result.port, cfg[httpd_role.DEFAULT_SERVER_NAME].listen)
313331
end
332+
333+
g.test_log_requests = function()
334+
local cfg = {
335+
server1 = {
336+
listen = 13002,
337+
log_requests = 'info',
338+
},
339+
server2 = {
340+
listen = 13003,
341+
log_requests = 'verbose',
342+
},
343+
}
344+
345+
httpd_role.apply(cfg)
346+
347+
local server1 = httpd_role.get_server('server1')
348+
t.assert(server1)
349+
t.assert_type(server1.options.log_requests, 'function')
350+
351+
local server2 = httpd_role.get_server('server2')
352+
t.assert(server2)
353+
t.assert_type(server2.options.log_requests, 'function')
354+
end

0 commit comments

Comments
 (0)