-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(log): add possibility for debug logging to diagnose issues
The log can be enabled by specifying it in the yazi.nvim config: ```lua -- ... other config options here ---@type YaziConfig opts = { log_level = vim.log.levels.DEBUG, }, ``` Only DEBUG level is supported for now. You can find out the location of the log file by running the following command: ```vim :checkhealth yazi ```
- Loading branch information
1 parent
2cf6057
commit db4ca7b
Showing
5 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
local plenary_path = require('plenary.path') | ||
|
||
---@class (exact) yazi.Log | ||
---@field level yazi.LogLevel | ||
---@field path string | ||
local Log = {} | ||
|
||
--- The log levels are the same as for vim.log.levels | ||
---@enum yazi.LogLevel | ||
local log_levels = { | ||
TRACE = 0, | ||
DEBUG = 1, | ||
INFO = 2, | ||
WARN = 3, | ||
ERROR = 4, | ||
OFF = 5, | ||
} | ||
|
||
---@type yazi.LogLevel | ||
Log.level = log_levels.OFF | ||
|
||
---@return string | ||
function Log:get_logfile_path() | ||
local ok, stdpath = pcall(vim.fn.stdpath, 'log') | ||
if not ok then | ||
stdpath = vim.fn.stdpath('cache') | ||
end | ||
return plenary_path:new(stdpath, 'yazi.log'):absolute() | ||
end | ||
|
||
Log.path = Log:get_logfile_path() | ||
|
||
---@type file*? # the file handle for the log file | ||
local file = nil | ||
|
||
---@param level string | ||
---@param message string | ||
function Log:write_message(level, message) | ||
-- initialize if needed | ||
if not file then | ||
local logfile, err = io.open(self.path, 'a+') | ||
file = logfile | ||
|
||
if not file then | ||
local err_msg = | ||
string.format('yazi.nvim: Failed to open log file at "%s"', err) | ||
vim.notify(err_msg, vim.log.levels.ERROR) | ||
end | ||
end | ||
|
||
if file ~= nil then | ||
local timestamp = os.date('%Y-%m-%d %H:%M:%S') | ||
local msg = string.format('[%s] %s %s', timestamp, level, message) | ||
file:write(msg .. '\n') | ||
file:flush() | ||
end | ||
end | ||
|
||
---@param message string | ||
function Log:debug(message) | ||
if self.level and self.level >= log_levels.DEBUG then | ||
self:write_message('DEBUG', message) | ||
end | ||
end | ||
|
||
return Log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters