0

While writing/debugging my nvim lua, I would like to have a running log file that I can monitor with tail -f in a side terminal.

I couldn't find a good answer to this question via Google. Adding what I came up with here for others to find (and improve).

1 Answer 1

0

Here's what I came up with. I'm a Lua-noob, so please help improve if needed. I'm playing around with creating my own Nvim config to share with my work-group (bad working name: nvimide).

To monitor:

tail -f /tmp/nvim.USER/nvimide.log

init.lua
nvimide = {
    debug_restart = false,
    debug = true,
    log_fh = nil,
    log = function(...)
        if nvimide.debug then
            local objects = {}
            for i = 1, select('#', ...) do
                local v = select(i, ...)
                table.insert(objects, vim.inspect(v))
            end
            nvimide.log_fh:write(table.concat(objects, '\n') .. '\n')
            nvimide.log_fh:flush()
        end
    end,
    script_path = function()
        return debug.getinfo(2, "S").source:sub(2)
    end,
}
if nvimide.debug then
    nvimide.log_fh = io.open("/tmp/nvim." .. os.getenv("USER") .. "/nvimide.log", nvimide.debug_restart and 'w' or 'a')
end
nvimide.log("Enter " .. nvimide.script_path())
vim.g.mapleader = " "
require("config.lazy")
require("config.keymaps")
require("config.autocmds")
nvimide.log("Leave " .. nvimide.script_path())
2
  • E5113: Error while calling lua chunk: ...: attempt to index field 'log_fh' (a nil value)
    – rofrol
    Dec 27, 2023 at 12:42
  • I just tried and it works. You can see that when nvimide.debug is set to true, I define nvimide.log_fh. Jan 15 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.