summaryrefslogtreecommitdiff
path: root/lua/core/log.lua
diff options
context:
space:
mode:
authorkylo252 <[email protected]>2021-08-26 12:49:29 +0200
committerGitHub <[email protected]>2021-08-26 12:49:29 +0200
commit5b94e3cee2c4405e98c9c0e8769670723a1f4bae (patch)
treebeb76ee514df69240d6e954b25372412a0316b2a /lua/core/log.lua
parentcfefddde9e9376e68ad8fcad3b1cf8cf139456e6 (diff)
fix logging when plenary is not available (#1390)
Diffstat (limited to 'lua/core/log.lua')
-rw-r--r--lua/core/log.lua43
1 files changed, 37 insertions, 6 deletions
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