Add log path getter, time format

This commit is contained in:
Diego Augusto S. C 2024-09-12 13:44:36 -03:00 committed by L3MON4D3
parent 45db5addf8
commit 9d08546168
2 changed files with 20 additions and 14 deletions

5
DOC.md
View File

@ -3452,10 +3452,11 @@ hints towards what is going wrong.
The log is stored in `<vim.fn.stdpath("log")>/luasnip.log`
(`<vim.fn.stdpath("cache")>/luasnip.log` for Neovim versions where
`stdpath("log")` does not exist), and can be opened by calling `ls.log.open()`.
`stdpath("log")` does not exist), and can be opened by calling `ls.log.open()`. You can get the log path through `ls.log.log_location()`.
The loglevel (granularity of reported events) can be adjusted by calling
`ls.log.set_loglevel("error"|"warn"|"info"|"debug")`. `"debug"` has the highest
granularity, `"error"` the lowest, the default is `"warn"`.
granularity, `"error"` the lowest, the default is `"warn"`.
You can also adjust the datetime formatting through the `ls.log.time_fmt` variable. By default, it uses the `'%X'` formatting, which results in the full time (hour, minutes and seconds) being shown.
Once this log grows too large (10MiB, currently not adjustable), it will be
renamed to `luasnip.log.old`, and a new, empty log created in its place. If

View File

@ -54,19 +54,24 @@ end
local M = {}
--- The file path we're currently logging into.
function M.log_location()
return logpath
end
--- Time formatting for logs. Defaults to '%X'.
M.time_fmt = '%X'
local function make_log_level(level)
return function(msg)
log_line_append(string.format("%s | %s | %s", level, os.date(M.time_fmt), msg))
end
end
local log = {
error = function(msg)
log_line_append("ERROR | " .. msg)
end,
warn = function(msg)
log_line_append("WARN | " .. msg)
end,
info = function(msg)
log_line_append("INFO | " .. msg)
end,
debug = function(msg)
log_line_append("DEBUG | " .. msg)
end,
error = make_log_level('ERROR'),
warn = make_log_level('WARN'),
info = make_log_level('INFO'),
debug = make_log_level('DEBUG'),
}
-- functions copied directly by deepcopy.