refactor(logger): adapt to new changes upstream (#3695)

This commit is contained in:
Luc Sinet 2023-01-19 14:54:30 +01:00 committed by GitHub
parent edc5e0e93f
commit e7d7aa9273
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 45 deletions

View file

@ -10,6 +10,7 @@ Log.levels = {
vim.tbl_add_reverse_lookup(Log.levels) vim.tbl_add_reverse_lookup(Log.levels)
local notify_opts = {} local notify_opts = {}
local log_notify_as_notification = false
function Log:set_level(level) function Log:set_level(level)
local logger_ok, logger = pcall(function() local logger_ok, logger = pcall(function()
@ -17,8 +18,8 @@ function Log:set_level(level)
end) end)
local log_level = Log.levels[level:upper()] local log_level = Log.levels[level:upper()]
if logger_ok and logger and log_level then if logger_ok and logger and log_level then
for _, s in ipairs(logger.sinks) do for _, pipeline in ipairs(logger.pipelines) do
s.level = log_level pipeline.level = log_level
end end
end end
end end
@ -30,13 +31,12 @@ function Log:init()
end end
local log_level = Log.levels[(lvim.log.level):upper() or "WARN"] local log_level = Log.levels[(lvim.log.level):upper() or "WARN"]
local lvim_log = { structlog.configure {
lvim = { lvim = {
sinks = { pipelines = {
structlog.sinks.Console(log_level, { {
async = false, level = log_level,
processors = { processors = {
structlog.processors.Namer(),
structlog.processors.StackWriter({ "line", "file" }, { max_parents = 0, stack_level = 2 }), structlog.processors.StackWriter({ "line", "file" }, { max_parents = 0, stack_level = 2 }),
structlog.processors.Timestamper "%H:%M:%S", structlog.processors.Timestamper "%H:%M:%S",
}, },
@ -45,10 +45,11 @@ function Log:init()
{ "timestamp", "level", "logger_name", "msg" }, { "timestamp", "level", "logger_name", "msg" },
{ level = structlog.formatters.FormatColorizer.color_level() } { level = structlog.formatters.FormatColorizer.color_level() }
), ),
}), sink = structlog.sinks.Console(false), -- async=false
structlog.sinks.File(log_level, self:get_path(), { },
{
level = log_level,
processors = { processors = {
structlog.processors.Namer(),
structlog.processors.StackWriter({ "line", "file" }, { max_parents = 3, stack_level = 2 }), structlog.processors.StackWriter({ "line", "file" }, { max_parents = 3, stack_level = 2 }),
structlog.processors.Timestamper "%F %H:%M:%S", structlog.processors.Timestamper "%F %H:%M:%S",
}, },
@ -56,13 +57,12 @@ function Log:init()
"%s [%-5s] %s: %-30s", "%s [%-5s] %s: %-30s",
{ "timestamp", "level", "logger_name", "msg" } { "timestamp", "level", "logger_name", "msg" }
), ),
}), sink = structlog.sinks.File(self:get_path()),
},
}, },
}, },
} }
lvim_log.lvim.sinks[1].async = false -- HACK: Bug in structlog prevents setting async to false
structlog.configure(lvim_log)
local logger = structlog.get_logger "lvim" local logger = structlog.get_logger "lvim"
-- Overwrite `vim.notify` to use the logger -- Overwrite `vim.notify` to use the logger
@ -88,49 +88,34 @@ function Log:init()
end end
--- Configure the sink in charge of logging notifications --- Configure the sink in charge of logging notifications
---@param notif_handle table The implementation used by the sink for displaying the notifications ---@param nvim_notify table The nvim-notify instance
function Log:configure_notifications(notif_handle) function Log:configure_notifications(nvim_notify)
local status_ok, structlog = pcall(require, "structlog") local status_ok, structlog = pcall(require, "structlog")
if not status_ok then if not status_ok then
return return
end end
local default_namer = function(logger, entry) local function log_writer(log)
entry["title"] = logger.name local opts = { title = log.logger_name }
return entry opts = vim.tbl_deep_extend("force", opts, notify_opts)
end
local notify_opts_injecter = function(_, entry)
for key, value in pairs(notify_opts) do
entry[key] = value
end
notify_opts = {} notify_opts = {}
return entry
if log_notify_as_notification then
nvim_notify(log.msg, log.level, opts)
end
end end
local sink = structlog.sinks.NvimNotify(Log.levels.INFO, { local notif_pipeline = structlog.Pipeline(
processors = { structlog.level.INFO,
default_namer, {},
notify_opts_injecter, structlog.formatters.Format( --
},
formatter = structlog.formatters.Format( --
"%s", "%s",
{ "msg" }, { "msg" },
{ blacklist_all = true } { blacklist_all = true }
), ),
-- This should probably not be hard-coded structlog.sinks.Adapter(log_writer)
params_map = { )
icon = "icon", self.__handle:add_pipeline(notif_pipeline)
keep = "keep",
on_open = "on_open",
on_close = "on_close",
timeout = "timeout",
title = "title",
},
impl = notif_handle,
})
table.insert(self.__handle.sinks, sink)
end end
--- Adds a log entry using Plenary.log --- Adds a log entry using Plenary.log

View file

@ -108,7 +108,7 @@
"commit": "eee175f" "commit": "eee175f"
}, },
"structlog.nvim": { "structlog.nvim": {
"commit": "232a8e2" "commit": "45b26a2"
}, },
"telescope-fzf-native.nvim": { "telescope-fzf-native.nvim": {
"commit": "fab3e22" "commit": "fab3e22"