Skip to content

Commit 2458993

Browse files
outsinrefffonion
authored andcommitted
fix(core): respect custom proxy_access_log (#12073)
* fix(core): respect custom proxy_access_log Kong now has a fixed access log format `kong_log_format` that prevents customization and error on `kong start`. Related to #11663. If the `proxy_access_log` is not a valid pathname, then replace `kong_log_format` with the custom value. * fix(config): cover log_format name with hyphen * fix(config): early error when access log format is not defined * fix(config): discard warning or return nil * chore(config): style and comments * chore(*): comments (cherry picked from commit 9ec3494)
1 parent 01a8caf commit 2458993

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
message: "respect custom `proxy_access_log`"
2+
type: bugfix
3+
scope: Configuration

kong/cmd/utils/prefix_handler.lua

+12-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ local function compile_conf(kong_config, conf_template, template_env_inject)
239239
-- computed config properties for templating
240240
local compile_env = {
241241
_escape = ">",
242-
proxy_access_log_enabled = kong_config.proxy_access_log ~= "off",
243242
pairs = pairs,
244243
ipairs = ipairs,
245244
tostring = tostring,
@@ -248,6 +247,18 @@ local function compile_conf(kong_config, conf_template, template_env_inject)
248247
}
249248
}
250249

250+
local kong_proxy_access_log = kong_config.proxy_access_log
251+
if kong_proxy_access_log ~= "off" then
252+
compile_env.proxy_access_log_enabled = true
253+
end
254+
if kong_proxy_access_log then
255+
-- example: proxy_access_log = 'logs/some-file.log apigw_json'
256+
local _, custom_format_name = string.match(kong_proxy_access_log, "^(%S+)%s(%S+)")
257+
if custom_format_name then
258+
compile_env.custom_proxy_access_log = true
259+
end
260+
end
261+
251262
compile_env = pl_tablex.merge(compile_env, template_env_inject or {}, true)
252263

253264
do

kong/templates/nginx_kong.lua

+4
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ server {
8989
lua_kong_error_log_request_id $kong_request_id;
9090
9191
> if proxy_access_log_enabled then
92+
> if custom_proxy_access_log then
93+
access_log ${{PROXY_ACCESS_LOG}};
94+
> else
9295
access_log ${{PROXY_ACCESS_LOG}} kong_log_format;
96+
> end
9397
> else
9498
access_log off;
9599
> end

spec/01-unit/04-prefix_handler_spec.lua

+47-9
Original file line numberDiff line numberDiff line change
@@ -486,34 +486,72 @@ describe("NGINX conf compiler", function()
486486

487487
describe("injected NGINX directives", function()
488488
it("injects proxy_access_log directive", function()
489-
local conf = assert(conf_loader(nil, {
489+
local conf, nginx_conf
490+
conf = assert(conf_loader(nil, {
490491
proxy_access_log = "/dev/stdout",
491492
stream_listen = "0.0.0.0:9100",
492493
nginx_stream_tcp_nodelay = "on",
493494
}))
494-
local nginx_conf = prefix_handler.compile_kong_conf(conf)
495+
nginx_conf = prefix_handler.compile_kong_conf(conf)
495496
assert.matches("access_log%s/dev/stdout%skong_log_format;", nginx_conf)
496-
local nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
497+
nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
497498
assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf)
498499

499-
local conf = assert(conf_loader(nil, {
500+
conf = assert(conf_loader(nil, {
500501
proxy_access_log = "off",
501502
stream_listen = "0.0.0.0:9100",
502503
nginx_stream_tcp_nodelay = "on",
503504
}))
504-
local nginx_conf = prefix_handler.compile_kong_conf(conf)
505+
nginx_conf = prefix_handler.compile_kong_conf(conf)
505506
assert.matches("access_log%soff;", nginx_conf)
506-
local nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
507+
nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
507508
assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf)
508509

509-
local conf = assert(conf_loader(nil, {
510+
conf = assert(conf_loader(nil, {
511+
proxy_access_log = "/dev/stdout apigw-json",
512+
nginx_http_log_format = 'apigw-json "$kong_request_id"',
513+
stream_listen = "0.0.0.0:9100",
514+
nginx_stream_tcp_nodelay = "on",
515+
}))
516+
nginx_conf = prefix_handler.compile_kong_conf(conf)
517+
assert.matches("access_log%s/dev/stdout%sapigw%-json;", nginx_conf)
518+
nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
519+
assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf)
520+
521+
-- configure an undefined log format will error
522+
-- on kong start. This is expected
523+
conf = assert(conf_loader(nil, {
524+
proxy_access_log = "/dev/stdout not-exist",
525+
nginx_http_log_format = 'apigw-json "$kong_request_id"',
526+
stream_listen = "0.0.0.0:9100",
527+
nginx_stream_tcp_nodelay = "on",
528+
}))
529+
nginx_conf = prefix_handler.compile_kong_conf(conf)
530+
assert.matches("access_log%s/dev/stdout%snot%-exist;", nginx_conf)
531+
nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
532+
assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf)
533+
534+
conf = assert(conf_loader(nil, {
535+
proxy_access_log = "/tmp/not-exist.log",
536+
stream_listen = "0.0.0.0:9100",
537+
nginx_stream_tcp_nodelay = "on",
538+
}))
539+
nginx_conf = prefix_handler.compile_kong_conf(conf)
540+
assert.matches("access_log%s/tmp/not%-exist.log%skong_log_format;", nginx_conf)
541+
nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
542+
assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf)
543+
544+
conf = assert(conf_loader(nil, {
545+
prefix = "servroot_tmp",
546+
nginx_stream_log_format = "custom '$protocol $status'",
510547
proxy_stream_access_log = "/dev/stdout custom",
511548
stream_listen = "0.0.0.0:9100",
512549
nginx_stream_tcp_nodelay = "on",
513550
}))
514-
local nginx_conf = prefix_handler.compile_kong_conf(conf)
551+
assert(prefix_handler.prepare_prefix(conf))
552+
nginx_conf = prefix_handler.compile_kong_conf(conf)
515553
assert.matches("access_log%slogs/access.log%skong_log_format;", nginx_conf)
516-
local nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
554+
nginx_conf = prefix_handler.compile_kong_stream_conf(conf)
517555
assert.matches("access_log%s/dev/stdout%scustom;", nginx_conf)
518556
end)
519557

0 commit comments

Comments
 (0)