2019-12-25 13:32:59 +00:00
|
|
|
local ln = require "ln"
|
|
|
|
|
|
|
|
describe("ln", function()
|
2019-12-25 18:37:02 +00:00
|
|
|
describe("exports", function()
|
|
|
|
it("has Logger", function()
|
|
|
|
assert.truthy(ln.Logger)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("has default_logger", function()
|
|
|
|
assert.truthy(ln.default_logger)
|
2019-12-25 18:53:37 +00:00
|
|
|
assert.truthy(ln.default_logger.formatter)
|
|
|
|
assert.truthy(ln.default_logger.filters)
|
2019-12-25 18:37:02 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("has LogfmtFormatter", function()
|
|
|
|
assert.truthy(ln.LogfmtFormatter)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("has JSONFormatter", function()
|
|
|
|
assert.truthy(ln.JSONFormatter)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("has log", function()
|
|
|
|
assert.truthy(ln.log)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("has err", function()
|
|
|
|
assert.truthy(ln.err)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe("default logger operations", function()
|
|
|
|
local lgr = ln.default_logger
|
2019-12-25 18:53:37 +00:00
|
|
|
local msg
|
|
|
|
lgr.filters = {
|
|
|
|
function(message) msg = message end,
|
|
|
|
}
|
|
|
|
|
2019-12-25 18:37:02 +00:00
|
|
|
it("can log", function()
|
|
|
|
lgr:log {foo = "bar"}
|
2019-12-25 18:53:37 +00:00
|
|
|
assert.truthy(string.find(msg, 'foo=bar'))
|
2019-12-25 18:37:02 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("can error", function()
|
2019-12-25 18:53:37 +00:00
|
|
|
lgr:err("vibe check failed", {foo = "baz"})
|
|
|
|
assert.truthy(string.find(msg, 'foo=baz'))
|
|
|
|
assert.truthy(string.find(msg, 'vibe check failed'))
|
2019-12-25 18:37:02 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("takes multiple tables", function()
|
|
|
|
lgr:log({foo = "bar"}, {baz = "boz"})
|
2019-12-25 18:53:37 +00:00
|
|
|
assert.truthy(string.find(msg, 'foo=bar'))
|
|
|
|
assert.truthy(string.find(msg, 'baz=boz'))
|
2019-12-25 18:37:02 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("lets you override the formatter", function()
|
|
|
|
lgr.formatter = ln.JSONFormatter:new()
|
|
|
|
lgr:log {foo = "bar"}
|
2019-12-25 18:53:37 +00:00
|
|
|
assert.truthy(string.find(msg, "{"))
|
2019-12-25 18:37:02 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe("logfmt", function()
|
|
|
|
local fmtr = ln.LogfmtFormatter:new()
|
|
|
|
|
|
|
|
it("should return a non-nil message", function()
|
|
|
|
assert.truthy(fmtr:format({foo = "bar"}))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should have spaces when the input does", function()
|
|
|
|
local msg = fmtr:format {foo = "bar with spaces"}
|
|
|
|
assert.truthy(string.find(msg, 'foo="bar with spaces"'))
|
|
|
|
end)
|
|
|
|
end)
|
2019-12-25 18:53:37 +00:00
|
|
|
|
2019-12-25 18:37:02 +00:00
|
|
|
describe("logger options", function()
|
2019-12-25 18:53:37 +00:00
|
|
|
local msg
|
|
|
|
local filter_func = function(message) msg = message end
|
2019-12-25 18:37:02 +00:00
|
|
|
local lgr = ln.Logger:new {
|
2019-12-25 18:53:37 +00:00
|
|
|
formatter = ln.LogfmtFormatter:new(),
|
|
|
|
filters = {
|
|
|
|
filter_func,
|
|
|
|
}
|
2019-12-25 18:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
it("sets the formatter to non-nil", function()
|
|
|
|
assert.truthy(lgr.formatter)
|
|
|
|
end)
|
2019-12-25 18:53:37 +00:00
|
|
|
|
|
|
|
it("respects filters", function()
|
|
|
|
lgr:log {foo = "bar"}
|
|
|
|
assert.truthy(string.find(msg, 'foo=bar'))
|
|
|
|
end)
|
2019-12-25 19:02:17 +00:00
|
|
|
|
|
|
|
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)
|
2019-12-25 18:37:02 +00:00
|
|
|
end)
|
2019-12-25 13:32:59 +00:00
|
|
|
end)
|