LunarVim/lua/lvim/core/log.lua
LostNeophyte ccb80e41ee
refactor: migrate to lazy.nvim (#3647)
* refactor: convert plugins spec to lazy

* refactor(lazy): remove impatient

* fix(telescope): no more errors if theme is nil

* refactor(lazy): use lazy in plugin_loader

* refactor(lazy): pin plugins with packer's snapshot

* fix: add plugins to rtp before config:init

* fix: fs_stat nil check

* feat: lazy cache

* feat(lazy): reloading

* refactor(lazy): plugin-loader functions

* feat(lazy): cache reset

* refactor: set runtimepath manually

* fix: runtimepath

* refactor(rtp)

* refactor(lazy): packer -> lazy in various places

* fix(lazy): disable tree-sitter ensure installed

* refactor(lazy): restore order to bootstrap

* refactor(lazy): remove unused impatient profiler

* small fixes

* `lvim.plugins` deprecation handling

* fix: deprecation of `requires` in plugin specs

* feat: core plugins pinning

* refactor(lazy): plugin loader tests

* refactor(lazy): use lazy in scripts

* refactor(lazy): which-key keybinds

* chore: format

* fix: installer

* fix: first time setup

* feat: changes required for packaging

commit 951ac2b7c01b5200b973660c967852d1706cce28
Author: LostNeophyte <lostneophyte@tuta.io>
Date:   Wed Dec 28 13:49:44 2022 +0100

    fix: clean folder before copying plugins

commit 64e9afa44b8e528ba527e0510d0d8c2d2237a095
Author: LostNeophyte <lostneophyte@tuta.io>
Date:   Wed Dec 28 13:35:41 2022 +0100

    feat: copy core plugins on first run

commit 2d8e72090c7624f68c09a9aa6582223373a810c1
Author: LostNeophyte <lostneophyte@tuta.io>
Date:   Wed Dec 28 13:11:22 2022 +0100

    feat(utils): fs_copy

commit 85c1f025a6ba13183e85141f75f60e2eefc77bb5
Author: LostNeophyte <lostneophyte@tuta.io>
Date:   Wed Dec 28 13:04:38 2022 +0100

    fix: copy correct example config

* fix: packer specs deprecation handling

* fix: plugin specs deprecation

* feat: pin lazy's version

* fix: remove plugins form rtp before loading lazy

* fix: plugin-loader test

* feat(lazy): add keymappings for profile, log, and debug (#3665)

* feat(lazy): Add keymappings for profile, log, and debug

* feat(lazy): Add keymap for cleaning

* chore: format

* pref: lazy load many plugins

Co-authored-by: Uzair Aftab <uzaaft@outlook.com>

* fix: bootstrap correct version of lazy

* fix: also use CmdLineEnter event for cmp

* fix: don't use lazy's modules before it's set up

* perf: (hack) enable lazy's cache before loading lazy

* fix: plugins.lua

* fix: plugins bump script

* chore: remove debug print

* feat: add rounded border for `:Lazy`

* fix: bufferline flashing

* fix: don't close lazy on startup

* fix: load breadcrumbs on startup

* fix: don't lazy load bufferline

* chore: bump lazy's version

* fix: remove site from rtp (fixes treesitter issues)

* revert default config copying changes

* fix(bootstrap): actually remove plugins dir on windows

* chore: bump lazy's version

* chore: bump lazy's version

Co-authored-by: kylo252 <59826753+kylo252@users.noreply.github.com>
Co-authored-by: Uzair Aftab <48220549+Uzaaft@users.noreply.github.com>
Co-authored-by: Uzair Aftab <uzaaft@outlook.com>
Co-authored-by: opalmay <opal.mizrahi2@gmail.com>
2023-01-10 21:18:17 +01:00

211 lines
5.3 KiB
Lua

local Log = {}
Log.levels = {
TRACE = 1,
DEBUG = 2,
INFO = 3,
WARN = 4,
ERROR = 5,
}
vim.tbl_add_reverse_lookup(Log.levels)
local notify_opts = {}
function Log:set_level(level)
local logger_ok, logger = pcall(function()
return require("structlog").get_logger "lvim"
end)
local log_level = Log.levels[level:upper()]
if logger_ok and logger and log_level then
for _, s in ipairs(logger.sinks) do
s.level = log_level
end
end
end
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, self:get_path(), {
processors = {
structlog.processors.Namer(),
structlog.processors.StackWriter({ "line", "file" }, { max_parents = 3, stack_level = 2 }),
structlog.processors.Timestamper "%F %H:%M:%S",
},
formatter = structlog.formatters.Format( --
"%s [%-5s] %s: %-30s",
{ "timestamp", "level", "logger_name", "msg" }
),
}),
},
},
}
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"
-- 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
self:add_entry(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 level integer [same as vim.log.levels]
---@param msg any
---@param event any
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()
local logger_ok, logger = pcall(function()
return require("structlog").get_logger "lvim"
end)
if logger_ok and logger then
return logger
end
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 string.format("%s/%s.log", get_cache_dir(), "lvim")
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