Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local log = { _version = "0.1.0" }
log.usecolor = true
log.outfile = nil
log.level = "trace"

log.format = "txt"

local modes = {
{ name = "trace", color = "\27[34m", },
Expand Down Expand Up @@ -66,20 +66,39 @@ for i, x in ipairs(modes) do
local lineinfo = info.short_src .. ":" .. info.currentline

-- Output to console
print(string.format("%s[%-6s%s]%s %s: %s",
log.usecolor and x.color or "",
nameupper,
os.date("%H:%M:%S"),
log.usecolor and "\27[0m" or "",
lineinfo,
msg))
if log.format == "json" then
output = string.format(
"{\"message\": \"%s\", \"level\": \"%s\", \"pathname\": \"%s\", \"lineno\": \"%s\", \"asctime\": \"%s\", \"created\": %s}",
msg,
nameupper,
info.short_src,
info.currentline,
os.date("!%Y-%m-%d %H:%M:%S"),
os.time()
)
else
output = string.format("%s[%-6s%s]%s %s: %s",
log.usecolor and x.color or "",
nameupper,
os.date("%H:%M:%S"),
log.usecolor and "\27[0m" or "",
lineinfo,
msg)
end

print(output)

-- Output to log file
if log.outfile then
local fp = io.open(log.outfile, "a")
local str = string.format("[%-6s%s] %s: %s\n",
if log.format == "json" then
out_str = output.."\n"
else
out_str = string.format("[%-6s%s] %s: %s\n",
nameupper, os.date(), lineinfo, msg)
fp:write(str)
end

fp:write(out_str)
fp:close()
end

Expand Down