aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/lsp/log.lua
diff options
context:
space:
mode:
authorMichael Lingelbach <m.j.lbach@gmail.com>2021-09-15 11:35:04 -0700
committerJustin M. Keyes <justinkz@gmail.com>2021-09-26 11:28:28 -0700
commit7b1315fe61131f787d9351ecd444346b95f5e49c (patch)
treedcb7355549dcdebdf40b18bbddc3af48cdefcb90 /runtime/lua/vim/lsp/log.lua
parent27bac13be66946e2cb2912a44185d18b0f856a32 (diff)
downloadrneovim-7b1315fe61131f787d9351ecd444346b95f5e49c.tar.gz
rneovim-7b1315fe61131f787d9351ecd444346b95f5e49c.tar.bz2
rneovim-7b1315fe61131f787d9351ecd444346b95f5e49c.zip
feat(lsp): improve logging (#15636)
* Simplify rpc encode/decode messages to rpc.send/rcp.receive * Make missing handlers message throw a warning * Clean up formatting style in log * Move all non-RPC loop messages to trace instead of debug * Add format func option to log to allow newlines in per log entry
Diffstat (limited to 'runtime/lua/vim/lsp/log.lua')
-rw-r--r--runtime/lua/vim/lsp/log.lua23
1 files changed, 18 insertions, 5 deletions
diff --git a/runtime/lua/vim/lsp/log.lua b/runtime/lua/vim/lsp/log.lua
index 73fafb9715..2848b380be 100644
--- a/runtime/lua/vim/lsp/log.lua
+++ b/runtime/lua/vim/lsp/log.lua
@@ -14,7 +14,8 @@ log.levels = vim.deepcopy(vim.log.levels)
-- Default log level is warn.
local current_log_level = log.levels.WARN
-local log_date_format = "%FT%H:%M:%S%z"
+local log_date_format = "%F %H:%M:%S"
+local format_func = function(arg) return vim.inspect(arg, {newline=''}) end
do
local path_sep = vim.loop.os_uname().version:match("Windows") and "\\" or "/"
@@ -33,7 +34,7 @@ do
vim.fn.mkdir(vim.fn.stdpath('cache'), "p")
local logfile = assert(io.open(logfilename, "a+"))
-- 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)))
for level, levelnr in pairs(log.levels) do
-- Also export the log level on the root object.
log[level] = levelnr
@@ -56,14 +57,14 @@ do
if levelnr < current_log_level then return false end
if argc == 0 then return true end
local info = debug.getinfo(2, "Sl")
- local fileinfo = string.format("%s:%s", info.short_src, info.currentline)
- local parts = { table.concat({"[", level, "]", os.date(log_date_format), "]", fileinfo, "]"}, " ") }
+ 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 }
for i = 1, argc do
local arg = select(i, ...)
if arg == nil then
table.insert(parts, "nil")
else
- table.insert(parts, vim.inspect(arg, {newline=''}))
+ table.insert(parts, format_func(arg))
end
end
logfile:write(table.concat(parts, '\t'), "\n")
@@ -88,6 +89,18 @@ function log.set_level(level)
end
end
+--- Gets the current log level.
+function log.get_level()
+ return current_log_level
+end
+
+--- Sets formatting function used to format logs
+---@param handle function function to apply to logging arguments, pass vim.inspect for multi-line formatting
+function log.set_format_func(handle)
+ assert(handle == vim.inspect or type(handle) == 'function', "handle must be a function")
+ format_func = handle
+end
+
--- Checks whether the level is sufficient for logging.
--@param level number log level
--@returns (bool) true if would log, false if not