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` The log is stored in `<vim.fn.stdpath("log")>/luasnip.log`
(`<vim.fn.stdpath("cache")>/luasnip.log` for Neovim versions where (`<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 The loglevel (granularity of reported events) can be adjusted by calling
`ls.log.set_loglevel("error"|"warn"|"info"|"debug")`. `"debug"` has the highest `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 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 renamed to `luasnip.log.old`, and a new, empty log created in its place. If

View File

@ -54,19 +54,24 @@ end
local M = {} 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 = { local log = {
error = function(msg) error = make_log_level('ERROR'),
log_line_append("ERROR | " .. msg) warn = make_log_level('WARN'),
end, info = make_log_level('INFO'),
warn = function(msg) debug = make_log_level('DEBUG'),
log_line_append("WARN | " .. msg)
end,
info = function(msg)
log_line_append("INFO | " .. msg)
end,
debug = function(msg)
log_line_append("DEBUG | " .. msg)
end,
} }
-- functions copied directly by deepcopy. -- functions copied directly by deepcopy.