Skip to content

Commit beed598

Browse files
authored
Merge pull request #20 from tomgeorge/fix/server-logs
Hook into the server on_stdout and on_stderr to log server logs
2 parents ae6a853 + c66835f commit beed598

File tree

3 files changed

+14
-45
lines changed

3 files changed

+14
-45
lines changed

lua/eca/logger.lua

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,7 @@ end
8888
--- Log a message to the log file
8989
---@param message string
9090
---@param level integer
91-
---@param opts? {title?: string}
92-
function M.log(message, level, opts)
93-
opts = opts or {}
94-
91+
function M.log(message, level)
9592
if not M.config then
9693
return
9794
end
@@ -105,8 +102,7 @@ function M.log(message, level, opts)
105102

106103
local timestamp = os.date("%Y-%m-%d %H:%M:%S")
107104
local level_name = get_level_name(level)
108-
local prefix = opts.title == "SERVER" and "SERVER" or "CLIENT"
109-
local formatted = string.format("[%s] %-5s [%s] %s\n", timestamp, level_name, prefix, message)
105+
local formatted = string.format("[%s] %-5s %s\n", timestamp, level_name, message)
110106

111107
uv.fs_open(log_path, "a", 420, function(err, fd)
112108
if err or not fd then
@@ -120,45 +116,40 @@ end
120116

121117
--- Log debug message
122118
---@param message string
123-
---@param opts? {title?: string}
124-
function M.debug(message, opts)
125-
M.log(message, vim.log.levels.DEBUG, opts)
119+
function M.debug(message)
120+
M.log(message, vim.log.levels.DEBUG)
126121
end
127122

128123
--- Log info message
129124
---@param message string
130-
---@param opts? {title?: string}
131-
function M.info(message, opts)
132-
M.log(message, vim.log.levels.INFO, opts)
125+
function M.info(message)
126+
M.log(message, vim.log.levels.INFO)
133127
end
134128

135129
--- Log warn message
136130
---@param message string
137-
---@param opts? {title?: string}
138-
function M.warn(message, opts)
139-
M.log(message, vim.log.levels.WARN, opts)
131+
function M.warn(message)
132+
M.log(message, vim.log.levels.WARN)
140133
end
141134

142135
--- Log error message
143136
---@param message string
144-
---@param opts? {title?: string}
145-
function M.error(message, opts)
146-
M.log(message, vim.log.levels.ERROR, opts)
137+
function M.error(message)
138+
M.log(message, vim.log.levels.ERROR)
147139
end
148140

149141
--- Send notification to user via vim.notify
150142
---@param message string
151143
---@param level? integer vim.log.levels (default: INFO)
152-
---@param opts? {title?: string, once?: boolean}
144+
---@param opts? {title?: string}
153145
function M.notify(message, level, opts)
154146
level = level or vim.log.levels.INFO
155147
opts = opts or {}
156148

157-
M.log(message, level, opts)
149+
M.log(message, level)
158150

159151
vim.notify(message, level, {
160152
title = opts.title or "ECA",
161-
once = opts.once,
162153
})
163154
end
164155

lua/eca/server.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,10 @@ function M:start()
100100
-- Use vim.fn.jobstart for interactive processes
101101
local job_opts = {
102102
on_stdout = function(_, data, _)
103-
Logger.debug("Server stdout received: " .. vim.inspect(data))
104103
if data and self._rpc then
105104
local output = table.concat(data, "\n")
106105
if output and output ~= "" and output ~= "\n" then
107-
Logger.debug("Processing stdout: " .. output)
106+
Logger.log(output, vim.log.levels.INFO)
108107
self._rpc:_handle_stdout(output)
109108
end
110109
end
@@ -122,8 +121,7 @@ function M:start()
122121

123122
if #meaningful_lines > 0 then
124123
local error_output = table.concat(meaningful_lines, "\n")
125-
Logger.debug("ECA server stderr: " .. error_output)
126-
-- Capture logs for the logs buffer
124+
Logger.log(error_output, vim.log.levels.WARN)
127125
end
128126
end
129127
end,

tests/test_logging.lua

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ T["file_logging"]["writes to file with correct format"] = function()
228228

229229
expect_match(contents, "%[%d%d%d%d%-%d%d%-%d%d %d%d:%d%d:%d%d%]")
230230
expect_match(contents, "INFO")
231-
expect_match(contents, "%[CLIENT%]")
232231
expect_match(contents, "test message")
233232
end
234233

@@ -303,7 +302,6 @@ T["notifications"]["Logger.notify writes to log file"] = function()
303302

304303
expect_match(contents, "WARN")
305304
expect_match(contents, "test notification message")
306-
expect_match(contents, "%[CLIENT%]")
307305

308306
local notifications = child.lua_get("_G.captured_notifications")
309307
eq(#notifications, 1)
@@ -380,24 +378,6 @@ T["integration"]["logger functions work correctly"] = function()
380378
expect_match(contents, "error message")
381379
end
382380

383-
T["integration"]["server logging with SERVER prefix"] = function()
384-
local contents = log_and_read(
385-
"server_integration.log",
386-
[[
387-
Logger.info('server message 1', { title = 'SERVER' })
388-
Logger.warn('server warning', { title = 'SERVER' })
389-
Logger.error('server error', { title = 'SERVER' })
390-
391-
Logger.info('client message')
392-
]]
393-
)
394-
395-
expect_match(contents, "%[SERVER%] server message 1")
396-
expect_match(contents, "%[SERVER%] server warning")
397-
expect_match(contents, "%[SERVER%] server error")
398-
expect_match(contents, "%[CLIENT%] client message")
399-
end
400-
401381
T["integration"]["EcaLogs command behavior"] = function()
402382
setup_logger_with_file("eca_logs_test.log")
403383

0 commit comments

Comments
 (0)