Specify loglevel by sink

This commit is contained in:
Gabriel Tofvesson 2024-11-22 01:17:32 +01:00
parent b0e5877417
commit 609013a9fa
2 changed files with 51 additions and 44 deletions

View File

@ -1,13 +1,12 @@
local Logging = require("logging") local Logging = require("logging")
local LogLevel = Logging.LogLevel
local CACHE_FILE = "/.storage.cache" local CACHE_FILE = "/.storage.cache"
local LOCK_FILE = "/.storage.lock" local LOCK_FILE = "/.storage.lock"
local LOG_FILE_PATH = "latest.log" local LOG_FILE_PATH = "latest.log"
local Logger = Logging.firstLoad( local Logger = Logging.firstLoad(
Logging.LogLevel.TRACE, Logging.OUTPUTS.file(LOG_FILE_PATH),
Logging.OUTPUTS.combine( Logging.OUTPUTS.stdout(LogLevel.CRITICAL)
Logging.OUTPUTS.file(LOG_FILE_PATH)
--Logging.OUTPUTS.stdout
)
) )
local Chest = require("storage.chest") local Chest = require("storage.chest")

View File

@ -9,6 +9,8 @@ local LogLevel = {
ERROR = 6 ERROR = 6
} }
LogLevel._DEFAULT = LogLevel.TRACE
for k,v in pairs(LogLevel) do for k,v in pairs(LogLevel) do
LogLevel[v] = k LogLevel[v] = k
end end
@ -30,18 +32,24 @@ local Logger = {
} }
Logger.__index = Logger Logger.__index = Logger
function Logger:new(o)
local logger = {} local function combineOutputs(...)
local output = print local args = {...}
if type(o) == "table" then if #args == 0 then
if type(o.level) == "number" and LogLevel.isValid(o.level) then return function() end
logger.level = o.level elseif #args == 1 then
end return args[1]
if type(o.output) == "function" then end
output = o.output return function(text, level)
for _,v in ipairs(args) do
v(text, level)
end end
end end
logger.output = output end
-- TODO: Split logger outputs and specify LogLevel for each
function Logger:new(...)
local logger = { output = combineOutputs(...) or function() end }
setmetatable(logger, self) setmetatable(logger, self)
logger.__index = logger logger.__index = logger
@ -216,13 +224,11 @@ local function deepStringify(value)
end end
function Logger:_doPrint(level, message, ...) function Logger:_doPrint(level, message, ...)
if LogLevel.isGreaterOrEqual(level, self.level) then local result = { tostring(LogLevel[level]), deepStringify(message) }
local result = { tostring(LogLevel[level]), deepStringify(message) } for _,v in ipairs({...}) do
for _,v in ipairs({...}) do table.insert(result, deepStringify(v))
table.insert(result, deepStringify(v))
end
self.output(table.concat(result, " "))
end end
self.output(table.concat(result, " "), level)
end end
function Logger:trace(message, ...) function Logger:trace(message, ...)
@ -249,14 +255,8 @@ function Logger:error(message, ...)
self:_doPrint(LogLevel.ERROR, message, ...) self:_doPrint(LogLevel.ERROR, message, ...)
end end
function Logger:setOutput(output) function Logger:setOutput(...)
self.output = output self.output = combineOutputs(...)
end
function Logger:setLevel(level)
if LogLevel.isValid(level) then
self.level = LogLevel.asNumber(level)
end
end end
function Logger.plain(value, deep) function Logger.plain(value, deep)
@ -268,28 +268,33 @@ Logging.LogLevel = LogLevel
function Logging.getGlobalLogger() function Logging.getGlobalLogger()
if _G._GLOBAL_LOGGER == nil then if _G._GLOBAL_LOGGER == nil then
_G._GLOBAL_LOGGER = Logger:new{ level = LogLevel.DEBUG, output = print } _G._GLOBAL_LOGGER = Logger:new{ output = function() end }
end end
return _G._GLOBAL_LOGGER return _G._GLOBAL_LOGGER
end end
function Logging.firstLoad(level, output) function Logging.firstLoad(...)
Logging.resetAll() Logging.resetAll()
local logger = Logging.getGlobalLogger() local logger = Logging.getGlobalLogger()
logger:setOutput(output) logger:setOutput(...)
logger:setLevel(level)
return logger return logger
end end
Logging.OUTPUTS = {} Logging.OUTPUTS = {}
function Logging.OUTPUTS.file(name) function Logging.OUTPUTS.file(name, logLevel)
logLevel = logLevel or LogLevel._DEFAULT
local doWrite = function(file, text) local doWrite = function(file, text)
file.write(text) file.write(text)
file.write("\n") file.write("\n")
file.flush() file.flush()
end end
local file = fs.open(name, "w+") local file = fs.open(name, "w+")
return function(text) return function(text, level)
if level < logLevel then
return
end
if not pcall(doWrite, file, text) then if not pcall(doWrite, file, text) then
local dRes, dRet = pcall(fs.delete, name) local dRes, dRet = pcall(fs.delete, name)
if not dRes then if not dRes then
@ -304,17 +309,20 @@ function Logging.OUTPUTS.file(name)
end end
end end
end end
Logging.OUTPUTS.stdout = print
function Logging.OUTPUTS.combine(...) function Logging.OUTPUTS.transmit(modem, channel, logLevel)
local args = {...} logLevel = logLevel or LogLevel._DEFAULT
if #args == 0 then end
return function() end
end function Logging.OUTPUTS.stdout(logLevel)
return function(text) logLevel = logLevel or LogLevel._DEFAULT
for _,v in ipairs(args) do
v(text) return function(text, level)
if level < logLevel then
return
end end
print(text)
end end
end end