log.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. local Log = {}
  2. Log.levels = {
  3. TRACE = 1,
  4. DEBUG = 2,
  5. INFO = 3,
  6. WARN = 4,
  7. ERROR = 5,
  8. }
  9. local notify_opts = {}
  10. local log_notify_as_notification = false
  11. function Log:set_level(level)
  12. if
  13. not pcall(function()
  14. local logger_ok, logger = pcall(function()
  15. return require("structlog").get_logger "lvim"
  16. end)
  17. local log_level = Log.levels[level:upper()]
  18. if logger_ok and logger and log_level then
  19. for _, pipeline in ipairs(logger.pipelines) do
  20. pipeline.level = log_level
  21. end
  22. end
  23. end)
  24. then
  25. vim.notify "structlog version too old, run `:Lazy sync`"
  26. end
  27. end
  28. function Log:init()
  29. local status_ok, structlog = pcall(require, "structlog")
  30. if not status_ok then
  31. return nil
  32. end
  33. local log_level = Log.levels[(lvim.log.level):upper() or "WARN"]
  34. structlog.configure {
  35. lvim = {
  36. pipelines = {
  37. {
  38. level = log_level,
  39. processors = {
  40. structlog.processors.StackWriter({ "line", "file" }, { max_parents = 0, stack_level = 2 }),
  41. structlog.processors.Timestamper "%H:%M:%S",
  42. },
  43. formatter = structlog.formatters.FormatColorizer(
  44. "%s [%-5s] %s: %-30s",
  45. { "timestamp", "level", "logger_name", "msg" },
  46. { level = structlog.formatters.FormatColorizer.color_level() }
  47. ),
  48. sink = structlog.sinks.Console(false), -- async=false
  49. },
  50. {
  51. level = log_level,
  52. processors = {
  53. structlog.processors.StackWriter({ "line", "file" }, { max_parents = 3, stack_level = 2 }),
  54. structlog.processors.Timestamper "%F %H:%M:%S",
  55. },
  56. formatter = structlog.formatters.Format(
  57. "%s [%-5s] %s: %-30s",
  58. { "timestamp", "level", "logger_name", "msg" }
  59. ),
  60. sink = structlog.sinks.File(self:get_path()),
  61. },
  62. },
  63. },
  64. }
  65. local logger = structlog.get_logger "lvim"
  66. -- Overwrite `vim.notify` to use the logger
  67. if lvim.log.override_notify then
  68. vim.notify = function(msg, vim_log_level, opts)
  69. notify_opts = opts or {}
  70. -- vim_log_level can be omitted
  71. if vim_log_level == nil then
  72. vim_log_level = Log.levels["INFO"]
  73. elseif type(vim_log_level) == "string" then
  74. vim_log_level = Log.levels[(vim_log_level):upper()] or Log.levels["INFO"]
  75. else
  76. -- https://github.com/neovim/neovim/blob/685cf398130c61c158401b992a1893c2405cd7d2/runtime/lua/vim/lsp/log.lua#L5
  77. vim_log_level = vim_log_level + 1
  78. end
  79. self:add_entry(vim_log_level, msg)
  80. end
  81. end
  82. return logger
  83. end
  84. --- Configure the sink in charge of logging notifications
  85. ---@param nvim_notify table The nvim-notify instance
  86. function Log:configure_notifications(nvim_notify)
  87. local status_ok, structlog = pcall(require, "structlog")
  88. if not status_ok then
  89. return
  90. end
  91. local function log_writer(log)
  92. local opts = { title = log.logger_name }
  93. opts = vim.tbl_deep_extend("force", opts, notify_opts)
  94. notify_opts = {}
  95. if log_notify_as_notification then
  96. nvim_notify(log.msg, log.level, opts)
  97. end
  98. end
  99. local notif_pipeline = structlog.Pipeline(
  100. structlog.level.INFO,
  101. {},
  102. structlog.formatters.Format("%s", { "msg" }, { blacklist_all = true }),
  103. structlog.sinks.Adapter(log_writer)
  104. )
  105. self.__handle:add_pipeline(notif_pipeline)
  106. end
  107. --- Adds a log entry using Plenary.log
  108. ---@param level integer [same as vim.log.levels]
  109. ---@param msg any
  110. ---@param event any
  111. function Log:add_entry(level, msg, event)
  112. if
  113. not pcall(function()
  114. local logger = self:get_logger()
  115. if not logger then
  116. return
  117. end
  118. logger:log(level, vim.inspect(msg), event)
  119. end)
  120. then
  121. vim.notify "structlog version too old, run `:Lazy sync`"
  122. end
  123. end
  124. ---Retrieves the handle of the logger object
  125. ---@return table|nil logger handle if found
  126. function Log:get_logger()
  127. local logger_ok, logger = pcall(function()
  128. return require("structlog").get_logger "lvim"
  129. end)
  130. if logger_ok and logger then
  131. return logger
  132. end
  133. logger = self:init()
  134. if not logger then
  135. return
  136. end
  137. self.__handle = logger
  138. return logger
  139. end
  140. ---Retrieves the path of the logfile
  141. ---@return string path of the logfile
  142. function Log:get_path()
  143. return string.format("%s/%s.log", get_cache_dir(), "lvim")
  144. end
  145. ---Add a log entry at TRACE level
  146. ---@param msg any
  147. ---@param event any
  148. function Log:trace(msg, event)
  149. self:add_entry(self.levels.TRACE, msg, event)
  150. end
  151. ---Add a log entry at DEBUG level
  152. ---@param msg any
  153. ---@param event any
  154. function Log:debug(msg, event)
  155. self:add_entry(self.levels.DEBUG, msg, event)
  156. end
  157. ---Add a log entry at INFO level
  158. ---@param msg any
  159. ---@param event any
  160. function Log:info(msg, event)
  161. self:add_entry(self.levels.INFO, msg, event)
  162. end
  163. ---Add a log entry at WARN level
  164. ---@param msg any
  165. ---@param event any
  166. function Log:warn(msg, event)
  167. self:add_entry(self.levels.WARN, msg, event)
  168. end
  169. ---Add a log entry at ERROR level
  170. ---@param msg any
  171. ---@param event any
  172. function Log:error(msg, event)
  173. self:add_entry(self.levels.ERROR, msg, event)
  174. end
  175. setmetatable({}, Log)
  176. return Log