src/ln: document filters, let them reject lines
This commit is contained in:
parent
ba8df8cb33
commit
1b74fa386d
19
README.md
19
README.md
|
@ -55,3 +55,22 @@ local lgr = ln.Logger:new()
|
||||||
lgr:log {foo = "bar"}
|
lgr:log {foo = "bar"}
|
||||||
-- time="2019-12-25T13:27:32" 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"`.
|
|
@ -91,5 +91,16 @@ describe("ln", function()
|
||||||
lgr:log {foo = "bar"}
|
lgr:log {foo = "bar"}
|
||||||
assert.truthy(string.find(msg, 'foo=bar'))
|
assert.truthy(string.find(msg, 'foo=bar'))
|
||||||
end)
|
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)
|
||||||
end)
|
end)
|
||||||
|
|
|
@ -65,7 +65,9 @@ function Logger:log(...)
|
||||||
|
|
||||||
local message = self.formatter:format(result)
|
local message = self.formatter:format(result)
|
||||||
for _, v in pairs(self.filters) do
|
for _, v in pairs(self.filters) do
|
||||||
v(message)
|
if v(message) ~= nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue