Skip to content

Commit

Permalink
Add log level for console and file
Browse files Browse the repository at this point in the history
  • Loading branch information
erichlf committed Jun 6, 2024
1 parent 3416897 commit ec3c466
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions lua/devcontainer-cli/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ local default_config = {
-- Should write to a file
use_file = true,

-- Any messages above this level will be logged.
level = "debug",
-- Any messages above this level will be logged to log file.
log_level = "debug",
-- Any messages above this level will be logged to console.
console_level = "info",

-- Level configuration
modes = {
Expand Down Expand Up @@ -85,18 +87,14 @@ log.new = function(config, standalone)


local log_at_level = function(level, level_config, message_maker, ...)
-- Return early if we're below the config.level
if level < levels[config.level] then
return
end
local nameupper = level_config.name:upper()

local msg = message_maker(...)
local info = debug.getinfo(2, "Sl")
local lineinfo = info.short_src .. ":" .. info.currentline

-- Output to console
if config.use_console then
if config.use_console and level < levels[config.console_level] then
local console_string = string.format(
"[%-6s%s] %s: %s",
nameupper,
Expand All @@ -120,7 +118,7 @@ log.new = function(config, standalone)
end

-- Output to log file
if config.use_file then
if config.use_file and level < levels[config.log_level] then
local fp = io.open(outfile, "a")
local str = string.format("[%-6s%s] %s: %s\n",
nameupper, os.date(), lineinfo, msg)
Expand Down

0 comments on commit ec3c466

Please sign in to comment.