feat(lsp): add logging level "OFF" (#18379)

This commit is contained in:
ii14 2022-05-03 16:49:23 +02:00 committed by GitHub
parent 73741e9486
commit 70e2c5d10d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 31 deletions

View File

@ -750,7 +750,9 @@ omnifunc({findstart}, {base}) *vim.lsp.omnifunc()*
set_log_level({level}) *vim.lsp.set_log_level()* set_log_level({level}) *vim.lsp.set_log_level()*
Sets the global log level for LSP logging. Sets the global log level for LSP logging.
Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR" Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR",
"OFF"
Level numbers begin with "TRACE" at 0 Level numbers begin with "TRACE" at 0
Use `lsp.log_levels` for reverse lookup. Use `lsp.log_levels` for reverse lookup.

View File

@ -1012,6 +1012,7 @@ Log levels are one of the values defined in `vim.log.levels`:
vim.log.levels.INFO vim.log.levels.INFO
vim.log.levels.TRACE vim.log.levels.TRACE
vim.log.levels.WARN vim.log.levels.WARN
vim.log.levels.OFF
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
LUA-VIMSCRIPT BRIDGE *lua-vimscript* LUA-VIMSCRIPT BRIDGE *lua-vimscript*

View File

@ -58,6 +58,7 @@ vim.log = {
INFO = 2; INFO = 2;
WARN = 3; WARN = 3;
ERROR = 4; ERROR = 4;
OFF = 5;
} }
} }

View File

@ -1790,13 +1790,14 @@ end
-- --
-- Can be used to lookup the number from the name or the -- Can be used to lookup the number from the name or the
-- name from the number. -- name from the number.
-- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR" -- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF"
-- Level numbers begin with "TRACE" at 0 -- Level numbers begin with "TRACE" at 0
lsp.log_levels = log.levels lsp.log_levels = log.levels
--- Sets the global log level for LSP logging. --- Sets the global log level for LSP logging.
--- ---
--- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR" --- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF"
---
--- Level numbers begin with "TRACE" at 0 --- Level numbers begin with "TRACE" at 0
--- ---
--- Use `lsp.log_levels` for reverse lookup. --- Use `lsp.log_levels` for reverse lookup.

View File

@ -8,7 +8,7 @@ local log = {}
-- Log level dictionary with reverse lookup as well. -- Log level dictionary with reverse lookup as well.
-- --
-- Can be used to lookup the number from the name or the name from the number. -- Can be used to lookup the number from the name or the name from the number.
-- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR" -- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF"
-- Level numbers begin with "TRACE" at 0 -- Level numbers begin with "TRACE" at 0
log.levels = vim.deepcopy(vim.log.levels) log.levels = vim.deepcopy(vim.log.levels)
@ -25,14 +25,31 @@ do
end end
local logfilename = path_join(vim.fn.stdpath('cache'), 'lsp.log') local logfilename = path_join(vim.fn.stdpath('cache'), 'lsp.log')
-- TODO: Ideally the directory should be created in open_logfile(), right
-- before opening the log file, but open_logfile() can be called from libuv
-- callbacks, where using fn.mkdir() is not allowed.
vim.fn.mkdir(vim.fn.stdpath('cache'), "p")
--- Returns the log filename. --- Returns the log filename.
---@returns (string) log filename ---@returns (string) log filename
function log.get_filename() function log.get_filename()
return logfilename return logfilename
end end
vim.fn.mkdir(vim.fn.stdpath('cache'), "p") local logfile, openerr
local logfile = assert(io.open(logfilename, "a+")) ---@private
--- Opens log file. Returns true if file is open, false on error
local function open_logfile()
-- Try to open file only once
if logfile then return true end
if openerr then return false end
logfile, openerr = io.open(logfilename, "a+")
if not logfile then
local err_msg = string.format("Failed to open LSP client log file: %s", openerr)
vim.notify(err_msg, vim.log.levels.ERROR)
return false
end
local log_info = vim.loop.fs_stat(logfilename) local log_info = vim.loop.fs_stat(logfilename)
if log_info and log_info.size > 1e9 then if log_info and log_info.size > 1e9 then
@ -46,6 +63,9 @@ do
-- Start message for logging -- Start message for logging
logfile:write(string.format("[START][%s] LSP logging initiated\n", os.date(log_date_format))) logfile:write(string.format("[START][%s] LSP logging initiated\n", os.date(log_date_format)))
return true
end
for level, levelnr in pairs(log.levels) do for level, levelnr in pairs(log.levels) do
-- Also export the log level on the root object. -- Also export the log level on the root object.
log[level] = levelnr log[level] = levelnr
@ -63,10 +83,12 @@ do
-- ``` -- ```
-- --
-- This way you can avoid string allocations if the log level isn't high enough. -- This way you can avoid string allocations if the log level isn't high enough.
if level ~= "OFF" then
log[level:lower()] = function(...) log[level:lower()] = function(...)
local argc = select("#", ...) local argc = select("#", ...)
if levelnr < current_log_level then return false end if levelnr < current_log_level then return false end
if argc == 0 then return true end if argc == 0 then return true end
if not open_logfile() then return false end
local info = debug.getinfo(2, "Sl") local info = debug.getinfo(2, "Sl")
local header = string.format("[%s][%s] ...%s:%s", level, os.date(log_date_format), string.sub(info.short_src, #info.short_src - 15), info.currentline) local header = string.format("[%s][%s] ...%s:%s", level, os.date(log_date_format), string.sub(info.short_src, #info.short_src - 15), info.currentline)
local parts = { header } local parts = { header }
@ -82,6 +104,7 @@ do
logfile:flush() logfile:flush()
end end
end end
end
end end
-- This is put here on purpose after the loop above so that it doesn't -- This is put here on purpose after the loop above so that it doesn't