75 lines
2.2 KiB
Plaintext
75 lines
2.2 KiB
Plaintext
log_by_lua_block {
|
|
local class = require "middleclass"
|
|
local clogger = require "bunkerweb.logger"
|
|
local helpers = require "bunkerweb.helpers"
|
|
local cdatastore = require "bunkerweb.datastore"
|
|
local cjson = require "cjson"
|
|
|
|
-- Start log phase
|
|
local logger = clogger:new("LOG")
|
|
local datastore = cdatastore:new()
|
|
logger:log(ngx.INFO, "log phase started")
|
|
|
|
-- Fill ctx
|
|
logger:log(ngx.INFO, "filling ngx.ctx ...")
|
|
local ok, ret, errors, ctx = helpers.fill_ctx()
|
|
if not ok then
|
|
logger:log(ngx.ERR, "fill_ctx() failed : " .. ret)
|
|
elseif errors then
|
|
for i, error in ipairs(errors) do
|
|
logger:log(ngx.ERR, "fill_ctx() error " .. tostring(i) .. " : " .. error)
|
|
end
|
|
end
|
|
logger:log(ngx.INFO, "ngx.ctx filled (ret = " .. ret .. ")")
|
|
|
|
-- Get plugins order
|
|
local order, err = datastore:get("plugins_order", true)
|
|
if not order then
|
|
logger:log(ngx.ERR, "can't get plugins order from datastore : " .. err)
|
|
return
|
|
end
|
|
|
|
-- Call log() methods
|
|
logger:log(ngx.INFO, "calling log() methods of plugins ...")
|
|
for i, plugin_id in ipairs(order.log) do
|
|
-- Require call
|
|
local plugin_lua, err = helpers.require_plugin(plugin_id)
|
|
if plugin_lua == false then
|
|
logger:log(ngx.ERR, err)
|
|
elseif plugin_lua == nil then
|
|
logger:log(ngx.INFO, err)
|
|
else
|
|
-- Check if plugin has log method
|
|
if plugin_lua.log ~= nil then
|
|
-- New call
|
|
local ok, plugin_obj = helpers.new_plugin(plugin_lua, ctx)
|
|
if not ok then
|
|
logger:log(ngx.ERR, plugin_obj)
|
|
else
|
|
local ok, ret = helpers.call_plugin(plugin_obj, "log")
|
|
if not ok then
|
|
logger:log(ngx.ERR, ret)
|
|
elseif not ret.ret then
|
|
logger:log(ngx.ERR, plugin_id .. ":log() call failed : " .. ret.msg)
|
|
else
|
|
logger:log(ngx.INFO, plugin_id .. ":log() call successful : " .. ret.msg)
|
|
end
|
|
end
|
|
else
|
|
logger:log(ngx.INFO, "skipped execution of " .. plugin_id .. " because method log() is not defined")
|
|
end
|
|
end
|
|
end
|
|
logger:log(ngx.INFO, "called log() methods of plugins")
|
|
|
|
-- Display reason at info level
|
|
if ctx.reason then
|
|
logger:log(ngx.INFO, "client was denied with reason : " .. ctx.reason)
|
|
end
|
|
|
|
-- Save ctx
|
|
ngx.ctx = ctx
|
|
|
|
logger:log(ngx.INFO, "log phase ended")
|
|
}
|