robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
3.6 KiB131 linesraw
1--- @brief
2--- The `vim.lsp.log` module provides logging for the Nvim LSP client.
3---
4--- When debugging language servers, it is helpful to enable extra-verbose logging of the LSP client
5--- RPC events. Example:
6--- ```lua
7--- vim.lsp.log.set_level 'trace'
8--- ```
9---
10--- Then try to run the language server, and open the log with:
11--- ```vim
12--- :log lsp
13--- ```
14---
15--- Note:
16--- - Remember to DISABLE verbose logging ("debug" or "trace" level), else you may encounter
17---   performance issues.
18--- - "ERROR" messages containing "stderr" only indicate that the log was sent to stderr. Many
19---   servers send harmless messages via stderr.
20
21local log_levels = vim.log.levels
22
23local M = {
24  TRACE = log_levels.TRACE,
25  DEBUG = log_levels.DEBUG,
26  INFO = log_levels.INFO,
27  WARN = log_levels.WARN,
28  ERROR = log_levels.ERROR,
29  OFF = log_levels.OFF,
30}
31
32local protocol = require('vim.lsp.protocol')
33
34--- Log level dictionary with reverse lookup as well.
35---
36--- Can be used to lookup the number from the name or the name from the number.
37--- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF"
38--- Level numbers begin with "TRACE" at 0
39--- @type table<string,integer> | table<integer, string>
40--- @nodoc
41M.levels = vim.deepcopy(log_levels)
42
43local log = vim.log.new({
44  name = 'LSP',
45})
46
47M._self = log
48
49--- Returns the log filename.
50---@return string log filename
51function M.get_filename()
52  ---@diagnostic disable-next-line: access-invisible
53  return log.filename
54end
55
56for level, levelnr in pairs(log_levels) do
57  -- Add a reverse lookup.
58  M.levels[levelnr] = level
59end
60
61-- If called without arguments, it will check whether the log level is
62-- greater than or equal to this one. When called with arguments, it will
63-- log at that level (if applicable, it is checked either way).
64
65--- @nodoc
66M.debug = log.debug
67
68--- @nodoc
69M.error = log.error
70
71--- @nodoc
72M.info = log.info
73
74--- @nodoc
75M.trace = log.trace
76
77--- @nodoc
78M.warn = log.warn
79
80--- Sets the current log level.
81---@param level (string|integer) One of |vim.log.levels|
82function M.set_level(level)
83  vim.validate('level', level, { 'string', 'number' })
84
85  if type(level) == 'string' then
86    level = assert(M.levels[level:upper()], string.format('Invalid log level: %q', level))
87  end ---@cast level vim.log.levels
88  log:set_level(level)
89end
90
91--- Gets the current log level.
92---@return integer current log level
93function M.get_level()
94  return log:get_level()
95end
96
97--- Sets the formatting function used to format logs. If the formatting function returns nil, the entry won't
98--- be written to the log file.
99---@param handle fun(level:string, ...): string? Function to apply to log entries. The default will log the level,
100---date, source and line number of the caller, followed by the arguments.
101function M.set_format_func(handle)
102  log:set_format_func(function(_, level, ...)
103    return handle(assert(M.levels[level]), ...)
104  end)
105end
106
107--- Checks whether the level is sufficient for logging.
108---@deprecated
109---@param level integer log level
110---@return boolean : true if would log, false if not
111function M.should_log(level)
112  vim.deprecate('vim.lsp.log.should_log', 'vim.lsp.log.set_format_func', '0.13')
113  return level >= vim.log.get_level(log)
114end
115
116--- Convert LSP MessageType to vim.log.levels
117---
118---@param message_type lsp.MessageType
119function M._from_lsp_level(message_type)
120  if message_type == protocol.MessageType.Error then
121    return log_levels.ERROR
122  elseif message_type == protocol.MessageType.Warning then
123    return log_levels.WARN
124  elseif message_type == protocol.MessageType.Info or message_type == protocol.MessageType.Log then
125    return log_levels.INFO
126  else
127    return log_levels.DEBUG
128  end
129end
130
131return M