When writing a Neovim configuration or plugin, it sometimes can become annoying when trying to debug an issue. The
available debug utilities are either print()
or vim.notify()
. In both cases (by default vim.notify()
is the same
as print()
), the log messes are written to :messages
, which does not allow to further select or process the printed
text (as far as I'm aware).
Setup
However, thanks to the flexibility of Neovim we can write our own logging function, which writes the output to a temporary logging buffer. A rough outline of such a function could be
This can directly be tested via
:lua require("myFileName").log("Some log message", vim.log.levels.INFO, {namespace="myNameSpace"})
which should create a new buffer called LOG-myNameSpace
with the content [INFO] Some log message
.
The above is just a rough idea, and could be customized further. I.e. rather than having different buffers per
namespace, it could log to a single one and prefix the messages with the namespace. Or use print()
if a namespace is
not set. You get the idea.
How to use
There are two ways on how to use this:
- use the log function directly in our plugin
- or replace the existing
vim.notify()
function with our logging function.
The first option is straight forward and ensures that no other plugin will use the custom logging function:
The second option can be done by assigning the log function
vim.notify = require("myFileName").log
somewhere during the initialization of your lua dotfiles. It has the interesting side effect that other plugins using
vim.notify()
now also log into the temporary buffer.