Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: write logs to adapter-specific file #239

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
>
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lua/neotest-golang/lib/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/neotest-golang/lib/find.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
39 changes: 31 additions & 8 deletions lua/neotest-golang/logging.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
1 change: 1 addition & 0 deletions lua/neotest-golang/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 0 additions & 39 deletions lua/neotest-golang/utils/buffer.lua

This file was deleted.

5 changes: 0 additions & 5 deletions lua/neotest-golang/utils/init.lua

This file was deleted.

3 changes: 3 additions & 0 deletions tests/unit/options_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
Loading