feat(lsp)!: pass entire entry to format function (#34166)

* feat(lsp)!: pass entire entry to format function

* refactor(lsp): deprecate `vim.lsp.log.should_log()`
This commit is contained in:
Maria José Solano
2025-05-31 08:47:32 -07:00
committed by GitHub
parent 2763d06100
commit f72c13341a
4 changed files with 49 additions and 41 deletions

View File

@@ -33,6 +33,8 @@ LSP
• *vim.lsp.client_is_stopped()* Use |vim.lsp.get_client_by_id()| instead. • *vim.lsp.client_is_stopped()* Use |vim.lsp.get_client_by_id()| instead.
• *vim.lsp.util.stylize_markdown()* Use |vim.treesitter.start()| with • *vim.lsp.util.stylize_markdown()* Use |vim.treesitter.start()| with
`vim.wo.conceallevel = 2`. `vim.wo.conceallevel = 2`.
• *vim.lsp.log.should_log()* Use |vim.lsp.log.set_format_func()| instead
and return `nil` to omit entries from the logfile.
LUA LUA

View File

@@ -2656,11 +2656,13 @@ get_level() *vim.lsp.log.get_level()*
(`integer`) current log level (`integer`) current log level
set_format_func({handle}) *vim.lsp.log.set_format_func()* set_format_func({handle}) *vim.lsp.log.set_format_func()*
Sets formatting function used to format logs Sets the formatting function used to format logs. If the formatting
function returns nil, the entry won't be written to the log file.
Parameters: ~ Parameters: ~
• {handle} (`function`) function to apply to logging arguments, pass • {handle} (`fun(level:string, ...): string?`) Function to apply to log
vim.inspect for multi-line formatting entries. The default will log the level, date, source and
line number of the caller, followed by the arguments.
set_level({level}) *vim.lsp.log.set_level()* set_level({level}) *vim.lsp.log.set_level()*
Sets the current log level. Sets the current log level.
@@ -2668,15 +2670,6 @@ set_level({level}) *vim.lsp.log.set_level()*
Parameters: ~ Parameters: ~
• {level} (`string|integer`) One of |vim.log.levels| • {level} (`string|integer`) One of |vim.log.levels|
should_log({level}) *vim.lsp.log.should_log()*
Checks whether the level is sufficient for logging.
Parameters: ~
• {level} (`integer`) log level
Return: ~
(`boolean`) true if would log, false if not
============================================================================== ==============================================================================
Lua module: vim.lsp.rpc *lsp-rpc* Lua module: vim.lsp.rpc *lsp-rpc*

View File

@@ -71,6 +71,8 @@ HIGHLIGHTS
LSP LSP
• `root_markers` in |vim.lsp.Config| can now be ordered by priority. • `root_markers` in |vim.lsp.Config| can now be ordered by priority.
• The function set with |vim.lsp.log.set_format_func()| is now given all
arguments corresponding to a log entry instead of the individual arguments.
LUA LUA

View File

@@ -39,8 +39,28 @@ local current_log_level = log_levels.WARN
local log_date_format = '%F %H:%M:%S' local log_date_format = '%F %H:%M:%S'
local function format_func(arg) --- Default formatting function.
return vim.inspect(arg, { newline = ' ', indent = '' }) --- @param level? string
local function format_func(level, ...)
if log_levels[level] < current_log_level then
return nil
end
local info = debug.getinfo(2, 'Sl')
local header = string.format(
'[%s][%s] %s:%s',
level,
os.date(log_date_format),
info.short_src,
info.currentline
)
local parts = { header }
local argc = select('#', ...)
for i = 1, argc do
local arg = select(i, ...)
table.insert(parts, arg == nil and 'nil' or vim.inspect(arg, { newline = ' ', indent = '' }))
end
return table.concat(parts, '\t') .. '\n'
end end
local function notify(msg, level) local function notify(msg, level)
@@ -116,13 +136,9 @@ for level, levelnr in pairs(log_levels) do
end end
--- @param level string --- @param level string
--- @param levelnr integer
--- @return fun(...:any): boolean? --- @return fun(...:any): boolean?
local function create_logger(level, levelnr) local function create_logger(level)
return function(...) return function(...)
if not log.should_log(levelnr) then
return false
end
local argc = select('#', ...) local argc = select('#', ...)
if argc == 0 then if argc == 0 then
return true return true
@@ -130,22 +146,12 @@ local function create_logger(level, levelnr)
if not open_logfile() then if not open_logfile() then
return false return false
end end
local info = debug.getinfo(2, 'Sl') local message = format_func(level, ...)
local header = string.format( if message then
'[%s][%s] %s:%s', assert(logfile)
level, logfile:write(message)
os.date(log_date_format), logfile:flush()
info.short_src,
info.currentline
)
local parts = { header }
for i = 1, argc do
local arg = select(i, ...)
table.insert(parts, arg == nil and 'nil' or format_func(arg))
end end
assert(logfile)
logfile:write(table.concat(parts, '\t'), '\n')
logfile:flush()
end end
end end
@@ -154,19 +160,19 @@ end
-- log at that level (if applicable, it is checked either way). -- log at that level (if applicable, it is checked either way).
--- @nodoc --- @nodoc
log.debug = create_logger('DEBUG', log_levels.DEBUG) log.debug = create_logger('DEBUG')
--- @nodoc --- @nodoc
log.error = create_logger('ERROR', log_levels.ERROR) log.error = create_logger('ERROR')
--- @nodoc --- @nodoc
log.info = create_logger('INFO', log_levels.INFO) log.info = create_logger('INFO')
--- @nodoc --- @nodoc
log.trace = create_logger('TRACE', log_levels.TRACE) log.trace = create_logger('TRACE')
--- @nodoc --- @nodoc
log.warn = create_logger('WARN', log_levels.WARN) log.warn = create_logger('WARN')
--- Sets the current log level. --- Sets the current log level.
---@param level (string|integer) One of |vim.log.levels| ---@param level (string|integer) One of |vim.log.levels|
@@ -188,8 +194,10 @@ function log.get_level()
return current_log_level return current_log_level
end end
--- Sets formatting function used to format logs --- Sets the formatting function used to format logs. If the formatting function returns nil, the entry won't
---@param handle function function to apply to logging arguments, pass vim.inspect for multi-line formatting --- be written to the log file.
---@param handle fun(level:string, ...): string? Function to apply to log entries. The default will log the level,
---date, source and line number of the caller, followed by the arguments.
function log.set_format_func(handle) function log.set_format_func(handle)
vim.validate('handle', handle, function(h) vim.validate('handle', handle, function(h)
return type(h) == 'function' or h == vim.inspect return type(h) == 'function' or h == vim.inspect
@@ -199,9 +207,12 @@ function log.set_format_func(handle)
end end
--- Checks whether the level is sufficient for logging. --- Checks whether the level is sufficient for logging.
---@deprecated
---@param level integer log level ---@param level integer log level
---@return boolean : true if would log, false if not ---@return boolean : true if would log, false if not
function log.should_log(level) function log.should_log(level)
vim.deprecate('vim.lsp.log.should_log', 'vim.lsp.log.set_format_func', '0.13')
vim.validate('level', level, 'number') vim.validate('level', level, 'number')
return level >= current_log_level return level >= current_log_level