src/ln: support filters

This commit is contained in:
Cadey Ratio 2019-12-25 18:53:37 +00:00
parent 3664cbb504
commit ba8df8cb33
2 changed files with 40 additions and 9 deletions

View File

@ -8,6 +8,8 @@ describe("ln", function()
it("has default_logger", function() it("has default_logger", function()
assert.truthy(ln.default_logger) assert.truthy(ln.default_logger)
assert.truthy(ln.default_logger.formatter)
assert.truthy(ln.default_logger.filters)
end) end)
it("has LogfmtFormatter", function() it("has LogfmtFormatter", function()
@ -29,21 +31,32 @@ describe("ln", function()
describe("default logger operations", function() describe("default logger operations", function()
local lgr = ln.default_logger local lgr = ln.default_logger
local msg
lgr.filters = {
function(message) msg = message end,
}
it("can log", function() it("can log", function()
lgr:log {foo = "bar"} lgr:log {foo = "bar"}
assert.truthy(string.find(msg, 'foo=bar'))
end) end)
it("can error", function() it("can error", function()
lgr:err("vibe check failed", {foo = "bar"}) lgr:err("vibe check failed", {foo = "baz"})
assert.truthy(string.find(msg, 'foo=baz'))
assert.truthy(string.find(msg, 'vibe check failed'))
end) end)
it("takes multiple tables", function() it("takes multiple tables", function()
lgr:log({foo = "bar"}, {baz = "boz"}) lgr:log({foo = "bar"}, {baz = "boz"})
assert.truthy(string.find(msg, 'foo=bar'))
assert.truthy(string.find(msg, 'baz=boz'))
end) end)
it("lets you override the formatter", function() it("lets you override the formatter", function()
lgr.formatter = ln.JSONFormatter:new() lgr.formatter = ln.JSONFormatter:new()
lgr:log {foo = "bar"} lgr:log {foo = "bar"}
assert.truthy(string.find(msg, "{"))
end) end)
end) end)
@ -59,14 +72,24 @@ describe("ln", function()
assert.truthy(string.find(msg, 'foo="bar with spaces"')) assert.truthy(string.find(msg, 'foo="bar with spaces"'))
end) end)
end) end)
describe("logger options", function() describe("logger options", function()
local msg
local filter_func = function(message) msg = message end
local lgr = ln.Logger:new { local lgr = ln.Logger:new {
formatter = ln.LogfmtFormatter:new() formatter = ln.LogfmtFormatter:new(),
filters = {
filter_func,
}
} }
it("sets the formatter to non-nil", function() it("sets the formatter to non-nil", function()
assert.truthy(lgr.formatter) assert.truthy(lgr.formatter)
end) end)
it("respects filters", function()
lgr:log {foo = "bar"}
assert.truthy(string.find(msg, 'foo=bar'))
end)
end) end)
end) end)

View File

@ -63,17 +63,25 @@ function Logger:log(...)
end end
end end
message = self.formatter:format(result) local message = self.formatter:format(result)
for _, v in pairs(self.filters) do
print(message) v(message)
end
end end
function Logger:new(o) function Logger:new(o)
if o == nil then if o == nil then
o = { o = {}
formatter = LogfmtFormatter:new()
}
end end
if o.formatter == nil then
o.formatter = LogfmtFormatter:new()
end
if o.filters == nil then
o.filters = {print}
end
setmetatable(o, self) setmetatable(o, self)
self.__index = self self.__index = self
return o return o