Skip to content

Commit 64f3a74

Browse files
committed
perf(logging): construct Template at init time
1 parent 0daafc4 commit 64f3a74

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

gateway/src/apicast/policy/logging/logging.lua

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,23 @@ function _M.new(config)
4040
end
4141

4242
self.enable_access_logs_val = val_for_ngx_var[enable_access_logs]
43-
self.custom_logging = config.custom_logging
4443
self.enable_json_logs = config.enable_json_logs
45-
self.json_object_config = config.json_object_config or {}
44+
45+
if config.custom_logging then
46+
self.custom_logging_template = TemplateString.new(config.custom_logging, "liquid")
47+
end
48+
49+
if self.enable_json_logs then
50+
self.json_object_config = {}
51+
52+
for _, entry in ipairs(config.json_object_config or {}) do
53+
self.json_object_config[#self.json_object_config+1] = {
54+
key = entry.key,
55+
template = TemplateString.new(
56+
entry.value, entry.value_type or default_template_type)
57+
}
58+
end
59+
end
4660

4761
self:load_condition(config)
4862

@@ -99,8 +113,8 @@ end
99113
--- log_dump_json: returns an string with the json output.
100114
local function log_dump_json(self, extended_context)
101115
local result = {}
102-
for _, value in ipairs(self.json_object_config) do
103-
result[value.key] = TemplateString.new(value.value, value.value_type or default_template_type):render(extended_context)
116+
for _, entry in ipairs(self.json_object_config) do
117+
result[entry.key] = entry.template:render(extended_context)
104118
end
105119

106120
local status, data = pcall(cjson.encode, result)
@@ -116,8 +130,7 @@ end
116130

117131
-- log_dump_line: render the liquid custom_logging value and return it.
118132
local function log_dump_line(self, extended_context)
119-
local tmpl = TemplateString.new(self.custom_logging, "liquid")
120-
return tmpl:render(extended_context)
133+
return self.custom_logging_template:render(extended_context)
121134
end
122135

123136
-- get_log_line return the log line based on the kind of log defined in the
@@ -132,7 +145,7 @@ end
132145

133146

134147
function _M:use_default_access_logs()
135-
return not (self.custom_logging or self.enable_json_logs)
148+
return not (self.custom_logging_template or self.enable_json_logs)
136149
end
137150

138151
function _M:log(context)

0 commit comments

Comments
 (0)