src/ln: document filters, let them reject lines

This commit is contained in:
Cadey Ratio 2019-12-25 19:02:17 +00:00
parent ba8df8cb33
commit 1b74fa386d
3 changed files with 33 additions and 1 deletions

View File

@ -55,3 +55,22 @@ local lgr = ln.Logger:new()
lgr:log {foo = "bar"}
-- time="2019-12-25T13:27:32" foo=bar
```
Or custom filters
```lua
local lgr = ln.Logger:new {
filters = {
function(message)
if string.find(message, "debug=true") and os.getenv("DEBUG") ~= "YES" then
return false
end
end,
print,
}
}
lgr:log {debug = "true", line = "PRIVMSG #foo :bar", dir = "out"}
```
This will make the log line only show up when `DEBUG` is set to `"YES"`.

View File

@ -91,5 +91,16 @@ describe("ln", function()
lgr:log {foo = "bar"}
assert.truthy(string.find(msg, 'foo=bar'))
end)
it("ends if a filter returns non-nil", function()
msg = nil
lgr.filters = {
function(message) return false end,
filter_func,
}
lgr:log {foo = "bar"}
assert.falsy(msg)
end)
end)
end)

View File

@ -65,7 +65,9 @@ function Logger:log(...)
local message = self.formatter:format(result)
for _, v in pairs(self.filters) do
v(message)
if v(message) ~= nil then
return
end
end
end