Compare commits
11 Commits
Author | SHA1 | Date |
---|---|---|
|
3ef4d6557e | |
|
448aab6781 | |
|
a5cda0400b | |
|
c6a03a8d57 | |
|
e8e56dc1d1 | |
|
6be57f3199 | |
|
91faa7b5ef | |
|
1b74fa386d | |
|
ba8df8cb33 | |
|
3664cbb504 | |
|
2440dbc926 |
|
@ -0,0 +1,9 @@
|
||||||
|
kind: pipeline
|
||||||
|
name: default
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: test
|
||||||
|
image: "xena/lua:5.3"
|
||||||
|
commands:
|
||||||
|
- luarocks-5.3 install --only-deps ./ln-0.2.0-1.rockspec
|
||||||
|
- busted-5.3
|
|
@ -0,0 +1 @@
|
||||||
|
result
|
|
@ -0,0 +1,7 @@
|
||||||
|
run = (cmd) ->
|
||||||
|
status = os.execute cmd
|
||||||
|
unless status
|
||||||
|
error string.format "got nonzero exit status %d", status
|
||||||
|
|
||||||
|
run "./test.sh"
|
||||||
|
run "nix-build"
|
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"`.
|
|
@ -0,0 +1,23 @@
|
||||||
|
{ pkgs ? import <nixpkgs> { }, lua ? pkgs.lua5_3
|
||||||
|
, luaPackages ? pkgs.lua53Packages }:
|
||||||
|
|
||||||
|
with pkgs;
|
||||||
|
with luaPackages;
|
||||||
|
|
||||||
|
buildLuarocksPackage {
|
||||||
|
pname = "ln";
|
||||||
|
version = "0.2.1-2";
|
||||||
|
|
||||||
|
src = fetchurl {
|
||||||
|
url = "https://luarocks.org/ln-0.2.1-2.src.rock";
|
||||||
|
sha256 = "1hb1vw68w407y05nprp352c0di6iybz4gjdhc0p521cpif4l8i1l";
|
||||||
|
};
|
||||||
|
propagatedBuildInputs = [ lua dkjson ];
|
||||||
|
|
||||||
|
meta = with lib; {
|
||||||
|
homepage = "https://tulpa.dev/cadey/lua-ln";
|
||||||
|
description = "The natural log function";
|
||||||
|
license.fullName = "0bsd";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
package = "ln"
|
||||||
|
version = "0.1.0-1"
|
||||||
|
source = {
|
||||||
|
url = "git+https://tulpa.dev/cadey/lua-ln",
|
||||||
|
tag = "v0.1.0"
|
||||||
|
}
|
||||||
|
description = {
|
||||||
|
summary = "The natural log function",
|
||||||
|
homepage = "https://tulpa.dev/cadey/lua-ln",
|
||||||
|
license = "0bsd"
|
||||||
|
}
|
||||||
|
dependencies = {
|
||||||
|
"lua ~> 5.3",
|
||||||
|
"dkjson"
|
||||||
|
}
|
||||||
|
build = {
|
||||||
|
type = "builtin",
|
||||||
|
modules = {
|
||||||
|
ln = "src/ln.lua"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package = "ln"
|
||||||
|
version = "0.2.0-1"
|
||||||
|
source = {
|
||||||
|
url = "git+https://tulpa.dev/cadey/lua-ln",
|
||||||
|
tag = "v0.2.0"
|
||||||
|
}
|
||||||
|
description = {
|
||||||
|
summary = "The natural log function",
|
||||||
|
homepage = "https://tulpa.dev/cadey/lua-ln",
|
||||||
|
license = "0bsd"
|
||||||
|
}
|
||||||
|
dependencies = {
|
||||||
|
"lua ~> 5.3",
|
||||||
|
"dkjson"
|
||||||
|
}
|
||||||
|
build = {
|
||||||
|
type = "builtin",
|
||||||
|
modules = {
|
||||||
|
ln = "src/ln.lua"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package = "ln"
|
||||||
|
version = "0.2.1-1"
|
||||||
|
source = {
|
||||||
|
url = "git+https://tulpa.dev/cadey/lua-ln"
|
||||||
|
}
|
||||||
|
description = {
|
||||||
|
homepage = "https://tulpa.dev/cadey/lua-ln",
|
||||||
|
license = "0bsd",
|
||||||
|
summary = "The natural log function"
|
||||||
|
}
|
||||||
|
dependencies = {
|
||||||
|
"lua ~> 5.1",
|
||||||
|
"dkjson"
|
||||||
|
}
|
||||||
|
build = {
|
||||||
|
type = "builtin",
|
||||||
|
modules = {
|
||||||
|
ln = "src/ln.lua"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package = "ln"
|
||||||
|
version = "0.2.1-2"
|
||||||
|
source = {
|
||||||
|
url = "git+https://tulpa.dev/cadey/lua-ln"
|
||||||
|
}
|
||||||
|
description = {
|
||||||
|
homepage = "https://tulpa.dev/cadey/lua-ln",
|
||||||
|
license = "0bsd",
|
||||||
|
summary = "The natural log function"
|
||||||
|
}
|
||||||
|
dependencies = {
|
||||||
|
"lua",
|
||||||
|
"dkjson"
|
||||||
|
}
|
||||||
|
build = {
|
||||||
|
type = "builtin",
|
||||||
|
modules = {
|
||||||
|
ln = "src/ln.lua"
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,7 +9,7 @@ description = {
|
||||||
summary = "The natural log function"
|
summary = "The natural log function"
|
||||||
}
|
}
|
||||||
dependencies = {
|
dependencies = {
|
||||||
"lua ~> 5.3",
|
"lua ~> 5.1",
|
||||||
"dkjson"
|
"dkjson"
|
||||||
}
|
}
|
||||||
build = {
|
build = {
|
|
@ -0,0 +1,13 @@
|
||||||
|
{ pkgs ? import <nixpkgs> { } }:
|
||||||
|
|
||||||
|
let lua = pkgs.lua5_3.withPackages (ps: with ps; [ dkjson ]);
|
||||||
|
|
||||||
|
in pkgs.mkShell {
|
||||||
|
buildInputs = with pkgs;
|
||||||
|
with lua53Packages; [
|
||||||
|
luarocks-nix
|
||||||
|
lua
|
||||||
|
busted
|
||||||
|
moonscript
|
||||||
|
];
|
||||||
|
}
|
136
spec/ln_spec.lua
136
spec/ln_spec.lua
|
@ -1,62 +1,106 @@
|
||||||
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)
|
assert.truthy(ln.default_logger.formatter)
|
||||||
|
assert.truthy(ln.default_logger.filters)
|
||||||
|
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()
|
local msg
|
||||||
lgr:log {foo = "bar"}
|
lgr.filters = {
|
||||||
end)
|
function(message) msg = message end,
|
||||||
|
}
|
||||||
|
|
||||||
|
it("can log", function()
|
||||||
|
lgr:log {foo = "bar"}
|
||||||
|
assert.truthy(string.find(msg, 'foo=bar'))
|
||||||
|
end)
|
||||||
|
|
||||||
it("can error", function()
|
it("can error", function()
|
||||||
lgr:err("vibe check failed", {foo = "bar"})
|
lgr:err("vibe check failed", {foo = "baz"})
|
||||||
end)
|
assert.truthy(string.find(msg, 'foo=baz'))
|
||||||
|
assert.truthy(string.find(msg, 'vibe check failed'))
|
||||||
|
end)
|
||||||
|
|
||||||
it("takes multiple tables", function()
|
it("takes multiple tables", function()
|
||||||
lgr:log({foo = "bar"}, {baz = "boz"})
|
lgr:log({foo = "bar"}, {baz = "boz"})
|
||||||
end)
|
assert.truthy(string.find(msg, 'foo=bar'))
|
||||||
|
assert.truthy(string.find(msg, 'baz=boz'))
|
||||||
|
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)
|
assert.truthy(string.find(msg, "{"))
|
||||||
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 msg
|
||||||
|
local filter_func = function(message) msg = message end
|
||||||
|
local lgr = ln.Logger:new {
|
||||||
|
formatter = ln.LogfmtFormatter:new(),
|
||||||
|
filters = {
|
||||||
|
filter_func,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
it("sets the formatter to non-nil", function()
|
||||||
|
assert.truthy(lgr.formatter)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("respects filters", 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)
|
end)
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
ln = require "ln"
|
||||||
|
|
||||||
|
describe "moonscript tests", ->
|
||||||
|
lgr = ln.default_logger
|
||||||
|
msg = ""
|
||||||
|
lgr.filters = {
|
||||||
|
(message) -> msg = message,
|
||||||
|
}
|
||||||
|
|
||||||
|
it "can log", ->
|
||||||
|
ln.log {foo: "bar"}
|
||||||
|
assert.truthy string.find msg, "foo=bar"
|
27
src/ln.lua
27
src/ln.lua
|
@ -63,18 +63,27 @@ function Logger:log(...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
message = self.formatter:format(result)
|
local message = self.formatter:format(result)
|
||||||
|
for _, v in pairs(self.filters) do
|
||||||
print(message)
|
if v(message) ~= nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Logger:new(formatter)
|
function Logger:new(o)
|
||||||
if formatter == nil then
|
if o == nil then
|
||||||
formatter = LogfmtFormatter:new()
|
o = {}
|
||||||
end
|
end
|
||||||
local o = {
|
|
||||||
formatter = formatter,
|
if o.formatter == nil then
|
||||||
}
|
o.formatter = LogfmtFormatter:new()
|
||||||
|
end
|
||||||
|
|
||||||
|
if o.filters == nil then
|
||||||
|
o.filters = {print}
|
||||||
|
end
|
||||||
|
|
||||||
setmetatable(o, self)
|
setmetatable(o, self)
|
||||||
self.__index = self
|
self.__index = self
|
||||||
return o
|
return o
|
||||||
|
|
11
test.sh
11
test.sh
|
@ -1,3 +1,10 @@
|
||||||
#!/bin/sh
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
set -x
|
||||||
|
|
||||||
|
nix-shell -p lua5_1 -p lua51Packages.busted -p lua51Packages.moonscript --run "busted"
|
||||||
|
nix-shell -p lua5_2 -p lua52Packages.busted -p lua52Packages.moonscript --run "busted"
|
||||||
|
nix-shell -p lua5_3 -p lua53Packages.busted -p lua53Packages.moonscript --run "busted"
|
||||||
|
nix-shell -p luajit -p luajitPackages.busted -p luajitPackages.moonscript --run "busted"
|
||||||
|
|
||||||
busted --defer-print
|
|
||||||
|
|
Loading…
Reference in New Issue