Skip to content

Commit d55bc2a

Browse files
committed
Set defaults, fix tests
1 parent d8697de commit d55bc2a

5 files changed

Lines changed: 97 additions & 73 deletions

File tree

lua/eca/config.lua

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,20 @@ M._defaults = {
1111
---@type string
1212
usage_string_format = "{messageCost} / {sessionCost}",
1313
log = {
14-
level = "info", -- Log level as string: "trace", "debug", "info", "warn", "error"
15-
notify = true, -- Enable vim.notify logging
16-
file = false, -- false = no file logging, true = default XDG path, string = custom path
14+
---@type "info"|"warn"|"error"|"debug"|"trace|off"
15+
--- Log level as string.
16+
--- Default: "info".
17+
--- See vim.log.levels for a list of log levels
18+
level = "info",
19+
---@type boolean
20+
---Use vim.notify for log messages
21+
---Default: false
22+
notify = false,
23+
---@type boolean|string
24+
--- If true, logs to $XDG_STATE_DIR/nvim/eca.log (see vim.fn.stdpath)
25+
--- If a string, log to that file path
26+
--- Default: true
27+
file = true,
1728
},
1829
behaviour = {
1930
auto_set_keymaps = true,

lua/eca/logger.lua

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local uv = vim.uv or vim.loop
33
---@class eca.Logger
44
local M = {}
55

6-
local config = nil
6+
M.config = nil
77

88
local LEVEL_NAMES = {
99
[vim.log.levels.TRACE] = "TRACE",
@@ -77,7 +77,14 @@ end
7777
--- Initialize logger with configuration
7878
---@param user_config table
7979
function M.setup(user_config)
80-
config = user_config
80+
-- Extract log config and set defaults
81+
local log_config = user_config and user_config.log or {}
82+
83+
M.config = {
84+
level = log_config.level or "info",
85+
notify = log_config.notify ~= nil and log_config.notify or false,
86+
file = log_config.file ~= nil and log_config.file or true,
87+
}
8188

8289
-- Check log file size on startup and notify if too large
8390
if M.is_file_logging_enabled() then
@@ -91,22 +98,28 @@ end
9198
--- Check if file logging is enabled
9299
---@return boolean
93100
function M.is_file_logging_enabled()
94-
return config and config.log and config.log.file and resolve_log_path(config.log.file) ~= nil
101+
if not M.config then
102+
return false
103+
end
104+
105+
-- If file is true, we use default path, if it's a string, we use custom path
106+
-- If file is false, file logging is disabled
107+
return M.config.file == true or type(M.config.file) == "string"
95108
end
96109

97110
--- Check if notifications are enabled
98111
---@return boolean
99112
function M.is_notify_enabled()
100-
return config and config.log and config.log.notify or false
113+
return M.config and M.config.notify or false
101114
end
102115

103116
--- Get current log file path
104117
---@return string|nil
105118
function M.get_log_path()
106-
if not config or not config.log then
119+
if not M.config then
107120
return nil
108121
end
109-
return resolve_log_path(config.log.file)
122+
return resolve_log_path(M.config.file)
110123
end
111124

112125
--- Main logging function
@@ -116,25 +129,29 @@ end
116129
function M.log(message, level, opts)
117130
opts = opts or {}
118131

119-
-- Early return if no config or logging disabled
120-
if not config or not config.log then
132+
-- Early return if no config
133+
if not M.config then
121134
return
122135
end
123136

124-
local min_level = get_log_level(config.log.level)
137+
local min_level = get_log_level(M.config.level)
125138
if level < min_level then
126139
return
127140
end
128141

129-
if opts.notify then
142+
-- Determine if we should notify
143+
local should_notify = opts.notify == true or (opts.notify == nil and opts.file == nil and M.config.notify)
144+
if should_notify then
130145
vim.notify(message, level, {
131146
title = opts.title or "ECA",
132147
once = opts.once,
133148
})
134149
end
135150

136-
if opts.file then
137-
local log_path = resolve_log_path(config.log.file)
151+
-- Determine if we should log to file
152+
local should_log_to_file = opts.file == true or (opts.file == nil and opts.notify == nil and M.config.file)
153+
if should_log_to_file then
154+
local log_path = resolve_log_path(M.config.file)
138155
if log_path then
139156
-- Ensure parent directory exists
140157
vim.fn.mkdir(vim.fn.fnamemodify(log_path, ":h"), "p")

0 commit comments

Comments
 (0)