From 5eabe6fcde5d4325e5bb5915cad06d2ae8ae5321 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Fri, 27 Dec 2024 22:30:36 +0100 Subject: [PATCH] feat: write logs to adapter-specific file (#239) --- README.md | 41 ++++++++++++++++------------- lua/neotest-golang/lib/cmd.lua | 2 +- lua/neotest-golang/lib/find.lua | 2 +- lua/neotest-golang/logging.lua | 39 +++++++++++++++++++++------ lua/neotest-golang/options.lua | 1 + lua/neotest-golang/utils/buffer.lua | 39 --------------------------- lua/neotest-golang/utils/init.lua | 5 ---- tests/unit/options_spec.lua | 3 +++ 8 files changed, 60 insertions(+), 72 deletions(-) delete mode 100644 lua/neotest-golang/utils/buffer.lua delete mode 100644 lua/neotest-golang/utils/init.lua diff --git a/README.md b/README.md index cdf90cd2..28091f10 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,7 @@ consider setting up neotest and its adapters in a | `colorize_test_output` | `true` | Enable output color for `SUCCESS`, `FAIL`, and `SKIP` tests. | | `warn_test_name_dupes` | `true` | Warn about duplicate test names within the same Go package. | | `warn_test_not_executed` | `true` | Warn if test was not executed. | +| `log_level` | `vim.log.levels.WARN` | Log level. | > [!NOTE] > @@ -556,31 +557,35 @@ return { > [here](https://github.com/fredrikaverpil/neotest-golang/discussions/new?category=configuration). You can also enable logging to further inspect what's going on under the hood. -Neotest-golang piggybacks on the Neotest logger. You can enable it like so: +Neotest-golang piggybacks on the Neotest logger but writes its own file. The +default log level is `WARN` but during troubleshooting you want to increase +this: ```lua --- set debug level after having called require("neotest").setup() -require("neotest.logging"):set_level(vim.log.levels.DEBUG) +local config = { + log_level = vim.log.levels.TRACE, -- set log level +} + +require("neotest").setup({ + adapters = { + require("neotest-golang")(config), -- Apply configuration + }, +}) ``` -> [!WARNING] -> -> Please note that this could cause tests to run slower, so don't forget to -> remove this setting once you have resolved your issue! +The neotest-golang logs can be opened using this convenient vim command: -You can get ahold of the log file's path using -`require("neotest.logging"):get_filename()`, which usually points to your -`~/.local/state/nvim/neotest.log`. +```vim +:exe 'edit' stdpath('log').'/neotest-golang.log' +``` -The logfile tends to be ginormous and if you are only looking for neotest-golang -related entries, you can either search for the `[neotest-golang]` prefix, or -open the log in a Neovim buffer and then filter out only the adapter-related -entries: +This usually corresponds to something like +`~/.local/state/nvim/neotest-golang.log`. -```lua -:edit ~/.local/state/nvim/neotest.log -:lua require("neotest-golang.utils.buffer").filter("[neotest-golang]") -``` +> [!WARNING] +> +> Don't forget to revert back to `WARN` level once you are done troubleshooting, +> as the `TRACE` level can degrade performance. ### Neotest is slowing down Neovim diff --git a/lua/neotest-golang/lib/cmd.lua b/lua/neotest-golang/lib/cmd.lua index b0b4fc89..99eae95e 100644 --- a/lua/neotest-golang/lib/cmd.lua +++ b/lua/neotest-golang/lib/cmd.lua @@ -13,7 +13,7 @@ local M = {} function M.golist_data(cwd) local cmd = M.golist_command() local go_list_command_concat = table.concat(cmd, " ") - logger.debug("Running Go list: " .. go_list_command_concat .. " in " .. cwd) + logger.info("Running Go list: " .. go_list_command_concat .. " in " .. cwd) local result = vim.system(cmd, { cwd = cwd, text = true }):wait() local err = nil diff --git a/lua/neotest-golang/lib/find.lua b/lua/neotest-golang/lib/find.lua index 5b310902..063a3789 100644 --- a/lua/neotest-golang/lib/find.lua +++ b/lua/neotest-golang/lib/find.lua @@ -23,7 +23,7 @@ function M.file_upwards(filename, start_path) local try_path = start_dir .. M.os_path_sep .. filename if vim.fn.filereadable(try_path) == 1 then - logger.debug("Found " .. filename .. " at " .. try_path) + logger.info("Found " .. filename .. " at " .. try_path) return try_path end diff --git a/lua/neotest-golang/logging.lua b/lua/neotest-golang/logging.lua index c9abe9bd..3c1fa066 100644 --- a/lua/neotest-golang/logging.lua +++ b/lua/neotest-golang/logging.lua @@ -1,9 +1,20 @@ local M = {} ----@type neotest.Logger -local logger = require("neotest.logging") +local logger = nil -local prefix = "[neotest-golang] " +local function get_logger() + local options = require("neotest-golang.options") + + if logger == nil then + ---@type neotest.Logger + logger = require("neotest.logging").new( + "neotest-golang", + { level = options.get().log_level } + ) + end + + return logger +end local function clean_output(str) -- Replace escaped newlines and tabs with spaces @@ -35,6 +46,18 @@ local function handle_input(input) end end +---Log the trace information. +---@param msg string|table +function M.trace(msg) + if M.get_level() > vim.log.levels.TRACE then + return + end + if type(msg) ~= "string" then + msg = handle_input(msg) + end + get_logger().trace(msg) +end + ---Log the debug information. ---@param msg string|table function M.debug(msg) @@ -44,7 +67,7 @@ function M.debug(msg) if type(msg) ~= "string" then msg = handle_input(msg) end - logger.debug(prefix .. msg) + get_logger().debug(msg) end ---Log the information. @@ -56,7 +79,7 @@ function M.info(msg) if type(msg) ~= "string" then msg = handle_input(msg) end - logger.info(prefix .. msg) + get_logger().info(msg) end ---Notify and log the warning. @@ -66,7 +89,7 @@ function M.warn(msg) msg = handle_input(msg) end vim.notify(msg, vim.log.levels.WARN) - logger.warn(prefix .. msg) + get_logger().warn(msg) end ---Notify, log and throw error. @@ -76,12 +99,12 @@ function M.error(msg) msg = handle_input(msg) end vim.notify(msg, vim.log.levels.ERROR) - logger.error(prefix .. msg) + get_logger().error(msg) error(msg) end function M.get_level() - return logger._level + return get_logger()._level end return M diff --git a/lua/neotest-golang/options.lua b/lua/neotest-golang/options.lua index 1c85e7b2..8a2bb42e 100644 --- a/lua/neotest-golang/options.lua +++ b/lua/neotest-golang/options.lua @@ -18,6 +18,7 @@ local opts = { colorize_test_output = true, warn_test_name_dupes = true, warn_test_not_executed = true, + log_level = vim.log.levels.WARN, -- experimental, for now undocumented, options dev_notifications = false, diff --git a/lua/neotest-golang/utils/buffer.lua b/lua/neotest-golang/utils/buffer.lua deleted file mode 100644 index 0786c9d6..00000000 --- a/lua/neotest-golang/utils/buffer.lua +++ /dev/null @@ -1,39 +0,0 @@ -local lib = require("neotest-golang.lib") - -local M = {} - -function M.filter(word) - -- Get the current buffer - local bufnr = vim.api.nvim_get_current_buf() - - -- Get all lines in the buffer - local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) - - -- Create a new table to store filtered lines - local new_lines = {} - - -- Flag to track if we're currently in a matching block - local in_matching_block = false - - -- Iterate through all lines - for _, line in ipairs(lines) do - -- Check if the line starts with a log level - local is_log_start = line:match("^%u+%s+|") - - if is_log_start then - -- If it's a new log entry, reset the flag - in_matching_block = false - end - - -- If the line contains the word or we're in a matching block, add it - if line:match(lib.convert.to_lua_pattern(word)) or in_matching_block then - table.insert(new_lines, line) - in_matching_block = true - end - end - - -- Replace the buffer contents with the new lines - vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, new_lines) -end - -return M diff --git a/lua/neotest-golang/utils/init.lua b/lua/neotest-golang/utils/init.lua deleted file mode 100644 index c04bf664..00000000 --- a/lua/neotest-golang/utils/init.lua +++ /dev/null @@ -1,5 +0,0 @@ -local M = {} - -M.buffer = require("neotest-golang.utils.buffer") - -return M diff --git a/tests/unit/options_spec.lua b/tests/unit/options_spec.lua index 9ca2de7b..ce463106 100644 --- a/tests/unit/options_spec.lua +++ b/tests/unit/options_spec.lua @@ -19,6 +19,7 @@ describe("Options are set up", function() colorize_test_output = true, warn_test_name_dupes = true, warn_test_not_executed = true, + log_level = vim.log.levels.WARN, -- experimental dev_notifications = false, @@ -45,6 +46,7 @@ describe("Options are set up", function() colorize_test_output = false, warn_test_name_dupes = true, warn_test_not_executed = true, + log_level = vim.log.levels.WARN, -- experimental dev_notifications = false, @@ -79,6 +81,7 @@ describe("Options are set up", function() colorize_test_output = true, warn_test_name_dupes = true, warn_test_not_executed = true, + log_level = vim.log.levels.WARN, -- experimental runner = "go",