1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
local Log = {}
local logfile = string.format("%s/%s.log", get_cache_dir(), "lvim")
Log.levels = {
TRACE = 1,
DEBUG = 2,
INFO = 3,
WARN = 4,
ERROR = 5,
}
vim.tbl_add_reverse_lookup(Log.levels)
local notify_opts = {}
function Log:init()
local status_ok, structlog = pcall(require, "structlog")
if not status_ok then
return nil
end
local log_level = Log.levels[(lvim.log.level):upper() or "WARN"]
local lvim_log = {
lvim = {
sinks = {
structlog.sinks.Console(log_level, {
async = false,
processors = {
structlog.processors.Namer(),
structlog.processors.StackWriter({ "line", "file" }, { max_parents = 0, stack_level = 2 }),
structlog.processors.Timestamper "%H:%M:%S",
},
formatter = structlog.formatters.FormatColorizer( --
"%s [%-5s] %s: %-30s",
{ "timestamp", "level", "logger_name", "msg" },
{ level = structlog.formatters.FormatColorizer.color_level() }
),
}),
structlog.sinks.File(log_level, logfile, {
processors = {
structlog.processors.Namer(),
structlog.processors.StackWriter({ "line", "file" }, { max_parents = 3, stack_level = 2 }),
structlog.processors.Timestamper "%H:%M:%S",
},
formatter = structlog.formatters.Format( --
"%s [%-5s] %s: %-30s",
{ "timestamp", "level", "logger_name", "msg" }
),
}),
},
},
}
structlog.configure(lvim_log)
local logger = structlog.get_logger "lvim"
-- Overwrite `vim.notify` to use the logger
if lvim.log.override_notify then
vim.notify = function(msg, vim_log_level, opts)
notify_opts = opts or {}
-- vim_log_level can be omitted
if vim_log_level == nil then
vim_log_level = Log.levels["INFO"]
elseif type(vim_log_level) == "string" then
vim_log_level = Log.levels[(vim_log_level):upper()] or Log.levels["INFO"]
else
-- https://github.com/neovim/neovim/blob/685cf398130c61c158401b992a1893c2405cd7d2/runtime/lua/vim/lsp/log.lua#L5
vim_log_level = vim_log_level + 1
end
logger:log(vim_log_level, msg)
end
end
return logger
end
--- Configure the sink in charge of logging notifications
---@param notif_handle table The implementation used by the sink for displaying the notifications
function Log:configure_notifications(notif_handle)
local status_ok, structlog = pcall(require, "structlog")
if not status_ok then
return
end
local default_namer = function(logger, entry)
entry["title"] = logger.name
return entry
end
local notify_opts_injecter = function(_, entry)
for key, value in pairs(notify_opts) do
entry[key] = value
end
notify_opts = {}
return entry
end
local sink = structlog.sinks.NvimNotify(Log.levels.INFO, {
processors = {
default_namer,
notify_opts_injecter,
},
formatter = structlog.formatters.Format( --
"%s",
{ "msg" },
{ blacklist_all = true }
),
-- This should probably not be hard-coded
params_map = {
icon = "icon",
keep = "keep",
on_open = "on_open",
on_close = "on_close",
timeout = "timeout",
title = "title",
},
impl = notif_handle,
})
table.insert(self.__handle.sinks, sink)
end
--- Adds a log entry using Plenary.log
---@param msg any
---@param level string [same as vim.log.log_levels]
function Log:add_entry(level, msg, event)
local logger = self:get_logger()
if not logger then
return
end
logger:log(level, vim.inspect(msg), event)
end
---Retrieves the handle of the logger object
---@return table|nil logger handle if found
function Log:get_logger()
if self.__handle then
return self.__handle
end
local logger = self:init()
if not logger then
return
end
self.__handle = logger
return logger
end
---Retrieves the path of the logfile
---@return string path of the logfile
function Log:get_path()
return logfile
end
---Add a log entry at TRACE level
---@param msg any
---@param event any
function Log:trace(msg, event)
self:add_entry(self.levels.TRACE, msg, event)
end
---Add a log entry at DEBUG level
---@param msg any
---@param event any
function Log:debug(msg, event)
self:add_entry(self.levels.DEBUG, msg, event)
end
---Add a log entry at INFO level
---@param msg any
---@param event any
function Log:info(msg, event)
self:add_entry(self.levels.INFO, msg, event)
end
---Add a log entry at WARN level
---@param msg any
---@param event any
function Log:warn(msg, event)
self:add_entry(self.levels.WARN, msg, event)
end
---Add a log entry at ERROR level
---@param msg any
---@param event any
function Log:error(msg, event)
self:add_entry(self.levels.ERROR, msg, event)
end
setmetatable({}, Log)
return Log
|