From 5b94e3cee2c4405e98c9c0e8769670723a1f4bae Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Thu, 26 Aug 2021 12:49:29 +0200 Subject: fix logging when plenary is not available (#1390) --- lua/core/log.lua | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) (limited to 'lua/core/log.lua') diff --git a/lua/core/log.lua b/lua/core/log.lua index 5dd5622e..54620625 100644 --- a/lua/core/log.lua +++ b/lua/core/log.lua @@ -4,26 +4,57 @@ local Log = {} ---@param opts these are passed verbatim to Plenary.log ---@return log handle function Log:new(opts) - local status_ok, _ = pcall(require, "plenary.log") + local status_ok, handle = pcall(require, "plenary.log") if not status_ok then - return nil + vim.notify("Plenary.log is not available. Logging to console only", vim.log.levels.DEBUG) end - local obj = require("plenary.log").new(opts) + self.__handle = handle + local path = string.format("%s/%s.log", vim.api.nvim_call_function("stdpath", { "cache" }), opts.plugin) - obj.get_path = function() + self.get_path = function() return path end - return obj + setmetatable({}, Log) + return self +end + +function Log:add_entry(msg, level) + local status_ok, _ = pcall(require, "plenary.log") + if not status_ok then + return vim.notify(msg, vim.log.levels[level]) + end + -- plenary uses lower-case log levels + return self.__handle[level:lower()](msg) end --- Creates or retrieves a log handle for the default logfile --- based on Plenary.log ---@return log handle -function Log:get_default() +function Log:new_default() return Log:new { plugin = "lunarvim", level = lvim.log.level } end +function Log:trace(msg) + self:add_entry(msg, "TRACE") +end + +function Log:debug(msg) + self:add_entry(msg, "DEBUG") +end + +function Log:info(msg) + self:add_entry(msg, "INFO") +end + +function Log:warn(msg) + self:add_entry(msg, "TRACE") +end + +function Log:error(msg) + self:add_entry(msg, "TRACE") +end + return Log -- cgit v1.2.3 From 27679f988fe187f9831ba7895c9c3a7ce2dd14f4 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Thu, 26 Aug 2021 20:32:16 +0200 Subject: [Refactor]: only allow a single logger (#1405) --- lua/core/log.lua | 65 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 33 deletions(-) (limited to 'lua/core/log.lua') diff --git a/lua/core/log.lua b/lua/core/log.lua index 54620625..1eb786ba 100644 --- a/lua/core/log.lua +++ b/lua/core/log.lua @@ -1,60 +1,59 @@ local Log = {} ---- Creates a log handle based on Plenary.log ----@param opts these are passed verbatim to Plenary.log ----@return log handle -function Log:new(opts) - local status_ok, handle = pcall(require, "plenary.log") - if not status_ok then - vim.notify("Plenary.log is not available. Logging to console only", vim.log.levels.DEBUG) - end - - self.__handle = handle - - local path = string.format("%s/%s.log", vim.api.nvim_call_function("stdpath", { "cache" }), opts.plugin) - - self.get_path = function() - return path - end - - setmetatable({}, Log) - return self -end - +--- Adds a log entry using Plenary.log +---@param msg any +---@param level string [same as vim.log.log_levels] function Log:add_entry(msg, level) - local status_ok, _ = pcall(require, "plenary.log") - if not status_ok then - return vim.notify(msg, vim.log.levels[level]) + assert(type(level) == "string") + if self.__handle then + -- plenary uses lower-case log levels + self.__handle[level:lower()](msg) + end + local status_ok, plenary = pcall(require, "plenary") + if status_ok then + local default_opts = { plugin = "lunarvim", level = lvim.log.level } + local handle = plenary.log.new(default_opts) + handle[level:lower()](msg) + self.__handle = handle end - -- plenary uses lower-case log levels - return self.__handle[level:lower()](msg) + -- don't do anything if plenary is not available end ---- Creates or retrieves a log handle for the default logfile ---- based on Plenary.log ----@return log handle -function Log:new_default() - return Log:new { plugin = "lunarvim", level = lvim.log.level } +---Retrieves the path of the logfile +---@return string path of the logfile +function Log:get_path() + return string.format("%s/%s.log", vim.fn.stdpath "cache", "lunarvim") end +---Add a log entry at TRACE level +---@param msg any function Log:trace(msg) self:add_entry(msg, "TRACE") end +---Add a log entry at DEBUG level +---@param msg any function Log:debug(msg) self:add_entry(msg, "DEBUG") end +---Add a log entry at INFO level +---@param msg any function Log:info(msg) self:add_entry(msg, "INFO") end +---Add a log entry at WARN level +---@param msg any function Log:warn(msg) - self:add_entry(msg, "TRACE") + self:add_entry(msg, "WARN") end +---Add a log entry at ERROR level +---@param msg any function Log:error(msg) - self:add_entry(msg, "TRACE") + self:add_entry(msg, "ERROR") end +setmetatable({}, Log) return Log -- cgit v1.2.3