-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathlogger.lua
More file actions
153 lines (135 loc) · 4.27 KB
/
logger.lua
File metadata and controls
153 lines (135 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
---@brief Centralized logger for Claude Code Neovim integration.
-- Provides level-based logging.
local M = {}
M.levels = {
ERROR = 1,
WARN = 2,
INFO = 3,
DEBUG = 4,
TRACE = 5,
}
local level_values = {
error = M.levels.ERROR,
warn = M.levels.WARN,
info = M.levels.INFO,
debug = M.levels.DEBUG,
trace = M.levels.TRACE,
}
local current_log_level_value = M.levels.INFO
--- @param plugin_config table The configuration table (e.g., from claudecode.init.state.config).
function M.setup(plugin_config)
local conf = plugin_config
if conf and conf.log_level and level_values[conf.log_level] then
current_log_level_value = level_values[conf.log_level]
else
vim.notify(
"ClaudeCode Logger: Invalid or missing log_level in configuration (received: "
.. tostring(conf and conf.log_level)
.. "). Defaulting to INFO.",
vim.log.levels.WARN
)
current_log_level_value = M.levels.INFO
end
end
local function log(level, component, message_parts)
if level > current_log_level_value then
return
end
local prefix = "[ClaudeCode]"
if component then
prefix = prefix .. " [" .. component .. "]"
end
local level_name = "UNKNOWN"
for name, val in pairs(M.levels) do
if val == level then
level_name = name
break
end
end
prefix = prefix .. " [" .. level_name .. "]"
local message = ""
for i, part in ipairs(message_parts) do
if i > 1 then
message = message .. " "
end
if type(part) == "table" or type(part) == "boolean" then
message = message .. vim.inspect(part)
else
message = message .. tostring(part)
end
end
if level == M.levels.ERROR then
vim.schedule(function()
vim.notify(prefix .. " " .. message, vim.log.levels.ERROR, { title = "ClaudeCode Error" })
end)
elseif level == M.levels.WARN then
vim.schedule(function()
vim.notify(prefix .. " " .. message, vim.log.levels.WARN, { title = "ClaudeCode Warning" })
end)
else
-- For INFO, DEBUG, TRACE, use nvim_echo to avoid flooding notifications,
-- to make them appear in :messages, and wrap in vim.schedule
-- to avoid "nvim_echo must not be called in a fast event context".
vim.schedule(function()
vim.api.nvim_echo({ { prefix .. " " .. message, "Normal" } }, true, {})
end)
end
end
--- @param component string|nil Optional component/module name.
-- @param ... any Varargs representing parts of the message.
function M.error(component, ...)
if type(component) ~= "string" then
log(M.levels.ERROR, nil, { component, ... })
else
log(M.levels.ERROR, component, { ... })
end
end
--- @param component string|nil Optional component/module name.
-- @param ... any Varargs representing parts of the message.
function M.warn(component, ...)
if type(component) ~= "string" then
log(M.levels.WARN, nil, { component, ... })
else
log(M.levels.WARN, component, { ... })
end
end
--- @param component string|nil Optional component/module name.
-- @param ... any Varargs representing parts of the message.
function M.info(component, ...)
if type(component) ~= "string" then
log(M.levels.INFO, nil, { component, ... })
else
log(M.levels.INFO, component, { ... })
end
end
--- Check if a specific log level is enabled
-- @param level_name string The level name ("error", "warn", "info", "debug", "trace")
-- @return boolean Whether the level is enabled
function M.is_level_enabled(level_name)
local level_value = level_values[level_name]
if not level_value then
return false
end
return level_value <= current_log_level_value
end
--- @param component string|nil Optional component/module name.
-- @param ... any Varargs representing parts of the message.
function M.debug(component, ...)
if type(component) ~= "string" then
log(M.levels.DEBUG, nil, { component, ... })
else
log(M.levels.DEBUG, component, { ... })
end
end
--- @param component string|nil Optional component/module name.
-- @param ... any Varargs representing parts of the message.
function M.trace(component, ...)
if type(component) ~= "string" then
log(M.levels.TRACE, nil, { component, ... })
else
log(M.levels.TRACE, component, { ... })
end
end
local default_config_for_initial_setup = require("claudecode.config").defaults
M.setup(default_config_for_initial_setup)
return M