From d55252a3ec8c2f7a319aed364a5c09c10e5cf63c Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Wed, 8 Jul 2026 16:15:42 -0400 Subject: [PATCH] refactor(logging)!: rename current_level #40642 --- runtime/doc/lua.txt | 12 ++++++------ runtime/lua/vim/log.lua | 30 +++++++++++++++--------------- test/functional/lua/log_spec.lua | 18 +++++++++--------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index ade0a61429..5aca77d2e3 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -3954,14 +3954,14 @@ new({opts}) *vim.log.new()* Parameters: ~ • {opts} (`table`) A table with the following fields: - • {current_level}? (`vim.log.levels`, default: - `vim.log.levels.WARN`) Minimum level that will be written. • {format_func}? - (`fun(current_level: vim.log.levels, level: vim.log.levels, ...): string?`) + (`fun(min_level: vim.log.levels, level: vim.log.levels, ...): string?`) Formatter used for each log entry. Receives the logger's - current level, the message level, and the values passed to + minimum level, the message level, and the values passed to the writer. Return a string to write an entry, or `nil` to skip it. + • {level}? (`vim.log.levels`, default: `vim.log.levels.WARN`) + Minimum level that will be written. • {name} (`string`) Display name used in notifications emitted by the logger. @@ -3971,13 +3971,13 @@ new({opts}) *vim.log.new()* set_format_func({log}, {func}) *vim.log.set_format_func()* Sets the formatter used to produce log entries. - The formatter receives the logger's current level, the message level, and + The formatter receives the logger's minimum level, the message level, and the values passed to the writer method. Return a string to write an entry, or `nil` to skip it. Parameters: ~ • {log} (`vim.Log`) See |vim.Log|. - • {func} (`fun(current_level: vim.log.levels, level: vim.log.levels, ...): string?`) + • {func} (`fun(min_level: vim.log.levels, level: vim.log.levels, ...): string?`) set_level({log}, {level}) *vim.log.set_level()* Sets the current log level. diff --git a/runtime/lua/vim/log.lua b/runtime/lua/vim/log.lua index 2f98ff683f..416ff29fa3 100644 --- a/runtime/lua/vim/log.lua +++ b/runtime/lua/vim/log.lua @@ -32,10 +32,10 @@ ---@field private name string --- --- Minimum level that will be written. ----@field private current_level integer +---@field private level integer --- --- Function used to format a log entry. ----@field private format_func fun(current_level: vim.log.levels, level:vim.log.levels, ...): string? +---@field private format_func fun(min_level: vim.log.levels, level: vim.log.levels, ...): string? --- --- Path of the log file. ---@field private filename string @@ -89,11 +89,11 @@ local log_date_format = '%F %H:%M:%S' --- Formats a message as: --- `[LEVEL][YYYY-MM-DD HH:MM:SS] source.lua:linearg1arg2` --- ----@param current_level vim.log.levels +---@param min_level vim.log.levels ---@param level vim.log.levels ---@return string? -local function default_format_func(current_level, level, ...) - if level < current_level then +local function default_format_func(min_level, level, ...) + if level < min_level then return nil end @@ -124,12 +124,12 @@ end --- --- Minimum level that will be written. --- (default: `vim.log.levels.WARN`) ----@field current_level? vim.log.levels +---@field level? vim.log.levels --- --- Formatter used for each log entry. ---- Receives the logger's current level, the message level, and the values passed to the writer. +--- Receives the logger's minimum level, the message level, and the values passed to the writer. --- Return a string to write an entry, or `nil` to skip it. ----@field format_func? fun(current_level: vim.log.levels, level: vim.log.levels, ...): string? +---@field format_func? fun(min_level: vim.log.levels, level: vim.log.levels, ...): string? --- Creates a logger instance. --- @@ -141,7 +141,7 @@ end function M.new(opts) vim.validate('opts', opts, 'table') vim.validate('opts.name', opts.name, 'string') - vim.validate('opts.current_level', opts.current_level, 'number', true) + vim.validate('opts.level', opts.level, 'number', true) vim.validate('opts.format_func', opts.format_func, 'function', true) local filename = vim.fs.joinpath(vim.fn.stdpath('log'), opts.name:lower() .. '.log') @@ -156,7 +156,7 @@ function M.new(opts) local log = setmetatable({ name = opts.name, filename = filename, - current_level = opts.current_level or M.levels.WARN, + level = opts.level or M.levels.WARN, format_func = opts.format_func or default_format_func, }, { __index = M }) log.trace = log:create_writer(M.levels.TRACE) @@ -233,7 +233,7 @@ function M:create_writer(level) if not self:open_file() then return false end - local message = self.format_func(self.current_level, level, ...) + local message = self.format_func(self.level, level, ...) if message then assert(self.logfile) self.logfile:write(message) @@ -250,24 +250,24 @@ end ---@param level vim.log.levels function M.set_level(log, level) vim.validate('level', level, 'number') - log.current_level = level + log.level = level end --- Gets the current log level. ---@param log vim.Log ---@return vim.log.levels function M.get_level(log) - return log.current_level + return log.level end --- Sets the formatter used to produce log entries. --- ---- The formatter receives the logger's current level, the message level, +--- The formatter receives the logger's minimum level, the message level, --- and the values passed to the writer method. --- Return a string to write an entry, or `nil` to skip it. --- ---@param log vim.Log ----@param func fun(current_level: vim.log.levels, level: vim.log.levels, ...): string? +---@param func fun(min_level: vim.log.levels, level: vim.log.levels, ...): string? function M.set_format_func(log, func) vim.validate('func', func, function(f) return type(f) == 'function' or f == vim.inspect diff --git a/test/functional/lua/log_spec.lua b/test/functional/lua/log_spec.lua index 204cb4e4ff..ca5f391132 100644 --- a/test/functional/lua/log_spec.lua +++ b/test/functional/lua/log_spec.lua @@ -60,7 +60,7 @@ describe('vim.log', function() eq( { true, true, true, true, true, false }, exec_lua(function() - local logger = vim.log.new({ name = 'NoArgs', current_level = vim.log.levels.TRACE }) + local logger = vim.log.new({ name = 'NoArgs', level = vim.log.levels.TRACE }) local logfile = vim.fs.joinpath(vim.fn.stdpath('log'), 'noargs.log') return { logger.trace(), @@ -74,13 +74,13 @@ describe('vim.log', function() ) end) - it('new() respects current_level and format_func opts', function() + it('new() respects level and format_func opts', function() exec_lua(function() local logger = vim.log.new({ name = 'CustomFormat', - current_level = vim.log.levels.INFO, - format_func = function(current_level, level, ...) - if level < current_level then + level = vim.log.levels.INFO, + format_func = function(min_level, level, ...) + if level < min_level then return nil end return tostring(select(1, ...)) .. '\n' @@ -126,14 +126,14 @@ describe('vim.log', function() exec_lua(function() local logger = vim.log.new({ name = 'SetFormat', - current_level = vim.log.levels.TRACE, + level = vim.log.levels.TRACE, format_func = function() return 'old\n' end, }) - vim.log.set_format_func(logger, function(current_level, level, ...) - return table.concat({ 'new', current_level, level, tostring(select(1, ...)) }, '|') .. '\n' + vim.log.set_format_func(logger, function(min_level, level, ...) + return table.concat({ 'new', min_level, level, tostring(select(1, ...)) }, '|') .. '\n' end) logger.error('formatted') @@ -156,7 +156,7 @@ describe('vim.log', function() caller_script = t.tmpname(false) .. '.lua' write_file( caller_script, - "local logger = vim.log.new({ name = 'Caller', current_level = vim.log.levels.TRACE })\n" + "local logger = vim.log.new({ name = 'Caller', level = vim.log.levels.TRACE })\n" .. "logger.info('from-script')\n", true )