The natural logger for Lua
lua
 
 
 
 
Go to file
Cadey Ratio 3ef4d6557e happy
Signed-off-by: Christine Dodrill <me@christine.website>
2021-05-09 15:20:34 +00:00
.within happy 2021-05-09 15:20:34 +00:00
rockspec happy 2021-05-09 15:20:34 +00:00
spec open ln up for more versions of lua, test them all 2021-05-09 15:02:46 +00:00
src src/ln: document filters, let them reject lines 2019-12-25 19:02:17 +00:00
.drone.yml drone: install deps 2020-01-19 18:43:45 +00:00
.envrc open ln up for more versions of lua, test them all 2021-05-09 15:02:46 +00:00
.gitignore open ln up for more versions of lua, test them all 2021-05-09 15:02:46 +00:00
LICENSE initial commit 2019-12-25 13:32:59 +00:00
README.md src/ln: document filters, let them reject lines 2019-12-25 19:02:17 +00:00
default.nix happy 2021-05-09 15:20:34 +00:00
shell.nix open ln up for more versions of lua, test them all 2021-05-09 15:02:46 +00:00
test.sh open ln up for more versions of lua, test them all 2021-05-09 15:02:46 +00:00

README.md

ln: The Natural Logger for Lua

This is a clone of ln for Lua. ln works by using key->value pairs to create structured logging output. By default, this outputs logs formatted similar to logfmt.

Example:

local ln = require "ln"

ln.log {foo = "bar"}
-- time="2019-12-25T13:24:00" foo=bar

It also supports multiple tables:

local ln = require "ln"

ln.log({foo = "bar"}, {needs_space = "a string value with spaces"})
-- time="2019-12-25T13:24:00" foo=bar needs_space="a string value with spaces"

And logging errors:

local ln = require "ln"

local result, err = thing_that_could_fail()
if err ~= nil then
  ln.err(err, {tried = "thing_that_could_fail()"})
end
-- time="2019-12-25T13:27:32" err="vibe check failed" tried=thing_that_could_fail()

And outputting logs as JSON:

local ln = require "ln"

ln.default_logger.formatter = ln.JSONFormatter:new()
ln.log {foo = "bar"}
-- {"foo":"bar","time":"2019-12-25T13:27:32"}

Or creating your own logger instance:

local ln = require "ln"

local lgr = ln.Logger:new()
lgr:log {foo = "bar"}
-- time="2019-12-25T13:27:32" foo=bar

Or custom filters

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".