summaryrefslogtreecommitdiff
path: root/lua/core/log.lua
blob: 54620625a49db65cbfc9c9576443d0c2d6c4a735 (plain)
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
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

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: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