diff --git a/README.md b/README.md index 74ec74d..04b044b 100644 --- a/README.md +++ b/README.md @@ -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"`. \ No newline at end of file diff --git a/spec/ln_spec.lua b/spec/ln_spec.lua index eb1527c..66e2e66 100644 --- a/spec/ln_spec.lua +++ b/spec/ln_spec.lua @@ -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) diff --git a/src/ln.lua b/src/ln.lua index 70933dc..7317068 100644 --- a/src/ln.lua +++ b/src/ln.lua @@ -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