src/ln: change formatter argument to a table with formatter
This commit is contained in:
parent
2440dbc926
commit
3664cbb504
102
spec/ln_spec.lua
102
spec/ln_spec.lua
|
@ -1,62 +1,72 @@
|
||||||
local ln = require "ln"
|
local ln = require "ln"
|
||||||
|
|
||||||
describe("ln", function()
|
describe("ln", function()
|
||||||
describe("exports", function()
|
describe("exports", function()
|
||||||
it("has Logger", function()
|
it("has Logger", function()
|
||||||
assert.truthy(ln.Logger)
|
assert.truthy(ln.Logger)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("has default_logger", function()
|
it("has default_logger", function()
|
||||||
assert.truthy(ln.default_logger)
|
assert.truthy(ln.default_logger)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("has LogfmtFormatter", function()
|
it("has LogfmtFormatter", function()
|
||||||
assert.truthy(ln.LogfmtFormatter)
|
assert.truthy(ln.LogfmtFormatter)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("has JSONFormatter", function()
|
it("has JSONFormatter", function()
|
||||||
assert.truthy(ln.JSONFormatter)
|
assert.truthy(ln.JSONFormatter)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("has log", function()
|
it("has log", function()
|
||||||
assert.truthy(ln.log)
|
assert.truthy(ln.log)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("has err", function()
|
it("has err", function()
|
||||||
assert.truthy(ln.err)
|
assert.truthy(ln.err)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("default logger operations", function()
|
describe("default logger operations", function()
|
||||||
local lgr = ln.default_logger
|
local lgr = ln.default_logger
|
||||||
it("can log", function()
|
it("can log", function()
|
||||||
lgr:log {foo = "bar"}
|
lgr:log {foo = "bar"}
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("can error", function()
|
it("can error", function()
|
||||||
lgr:err("vibe check failed", {foo = "bar"})
|
lgr:err("vibe check failed", {foo = "bar"})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("takes multiple tables", function()
|
it("takes multiple tables", function()
|
||||||
lgr:log({foo = "bar"}, {baz = "boz"})
|
lgr:log({foo = "bar"}, {baz = "boz"})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("lets you override the formatter", function()
|
it("lets you override the formatter", function()
|
||||||
lgr.formatter = ln.JSONFormatter:new()
|
lgr.formatter = ln.JSONFormatter:new()
|
||||||
lgr:log {foo = "bar"}
|
lgr:log {foo = "bar"}
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("logfmt", function()
|
describe("logfmt", function()
|
||||||
local fmtr = ln.LogfmtFormatter:new()
|
local fmtr = ln.LogfmtFormatter:new()
|
||||||
|
|
||||||
it("should return a non-nil message", function()
|
it("should return a non-nil message", function()
|
||||||
assert.truthy(fmtr:format({foo = "bar"}))
|
assert.truthy(fmtr:format({foo = "bar"}))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("should have spaces when the input does", function()
|
it("should have spaces when the input does", function()
|
||||||
local msg = fmtr:format {foo = "bar with spaces"}
|
local msg = fmtr:format {foo = "bar with spaces"}
|
||||||
assert.truthy(string.find(msg, 'foo="bar with spaces"'))
|
assert.truthy(string.find(msg, 'foo="bar with spaces"'))
|
||||||
end)
|
end)
|
||||||
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)
|
end)
|
||||||
|
|
11
src/ln.lua
11
src/ln.lua
|
@ -68,13 +68,12 @@ function Logger:log(...)
|
||||||
print(message)
|
print(message)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Logger:new(formatter)
|
function Logger:new(o)
|
||||||
if formatter == nil then
|
if o == nil then
|
||||||
formatter = LogfmtFormatter:new()
|
o = {
|
||||||
|
formatter = LogfmtFormatter:new()
|
||||||
|
}
|
||||||
end
|
end
|
||||||
local o = {
|
|
||||||
formatter = formatter,
|
|
||||||
}
|
|
||||||
setmetatable(o, self)
|
setmetatable(o, self)
|
||||||
self.__index = self
|
self.__index = self
|
||||||
return o
|
return o
|
||||||
|
|
Loading…
Reference in New Issue