src/ln: change formatter argument to a table with formatter

This commit is contained in:
Cadey Ratio 2019-12-25 18:37:02 +00:00
parent 2440dbc926
commit 3664cbb504
2 changed files with 61 additions and 52 deletions

View File

@ -1,62 +1,72 @@
local ln = require "ln"
describe("ln", function()
describe("exports", function()
it("has Logger", function()
assert.truthy(ln.Logger)
end)
describe("exports", function()
it("has Logger", function()
assert.truthy(ln.Logger)
end)
it("has default_logger", function()
assert.truthy(ln.default_logger)
end)
it("has default_logger", function()
assert.truthy(ln.default_logger)
end)
it("has LogfmtFormatter", function()
assert.truthy(ln.LogfmtFormatter)
end)
it("has LogfmtFormatter", function()
assert.truthy(ln.LogfmtFormatter)
end)
it("has JSONFormatter", function()
assert.truthy(ln.JSONFormatter)
end)
it("has JSONFormatter", function()
assert.truthy(ln.JSONFormatter)
end)
it("has log", function()
assert.truthy(ln.log)
end)
it("has log", function()
assert.truthy(ln.log)
end)
it("has err", function()
assert.truthy(ln.err)
end)
end)
it("has err", function()
assert.truthy(ln.err)
end)
end)
describe("default logger operations", function()
local lgr = ln.default_logger
it("can log", function()
lgr:log {foo = "bar"}
end)
describe("default logger operations", function()
local lgr = ln.default_logger
it("can log", function()
lgr:log {foo = "bar"}
end)
it("can error", function()
lgr:err("vibe check failed", {foo = "bar"})
end)
it("can error", function()
lgr:err("vibe check failed", {foo = "bar"})
end)
it("takes multiple tables", function()
lgr:log({foo = "bar"}, {baz = "boz"})
end)
it("takes multiple tables", function()
lgr:log({foo = "bar"}, {baz = "boz"})
end)
it("lets you override the formatter", function()
lgr.formatter = ln.JSONFormatter:new()
lgr:log {foo = "bar"}
end)
end)
it("lets you override the formatter", function()
lgr.formatter = ln.JSONFormatter:new()
lgr:log {foo = "bar"}
end)
end)
describe("logfmt", function()
local fmtr = ln.LogfmtFormatter:new()
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 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)
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)
describe("logger options", function()
local lgr = ln.Logger:new {
formatter = ln.LogfmtFormatter:new()
}
it("sets the formatter to non-nil", function()
assert.truthy(lgr.formatter)
end)
end)
end)

View File

@ -68,13 +68,12 @@ function Logger:log(...)
print(message)
end
function Logger:new(formatter)
if formatter == nil then
formatter = LogfmtFormatter:new()
function Logger:new(o)
if o == nil then
o = {
formatter = LogfmtFormatter:new()
}
end
local o = {
formatter = formatter,
}
setmetatable(o, self)
self.__index = self
return o