Skip to content

Commit 21ad795

Browse files
committed
Hook into the server on_stdout and on_stderr to log server logs.
This is still a little clunky but I think it is valuable to get the server logs as well as client-side logs
1 parent 60c6afa commit 21ad795

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

lua/eca/logger.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ function M.log(message, level, opts)
105105

106106
local timestamp = os.date("%Y-%m-%d %H:%M:%S")
107107
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)
108+
local prefix = opts.server and "[SERVER]" or ""
109+
local formatted = string.format("[%s] %-5s %s %s\n", timestamp, level_name, prefix, message)
110110

111111
uv.fs_open(log_path, "a", 420, function(err, fd)
112112
if err or not fd then
@@ -162,6 +162,7 @@ function M.notify(message, level, opts)
162162
})
163163
end
164164

165+
165166
--- Get log file statistics
166167
---@return table|nil
167168
function M.get_log_stats()

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, { server = true })
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, { server = true })
127125
end
128126
end
129127
end,

tests/test_logging.lua

Lines changed: 7 additions & 11 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,22 +378,20 @@ 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()
381+
T["integration"]["server logging with prefix"] = function()
384382
local contents = log_and_read(
385383
"server_integration.log",
386384
[[
387-
Logger.info('server message 1', { title = 'SERVER' })
388-
Logger.warn('server warning', { title = 'SERVER' })
389-
Logger.error('server error', { title = 'SERVER' })
390-
385+
Logger.log('server stdout message', vim.log.levels.INFO, { server = true })
386+
Logger.log('server stderr message', vim.log.levels.WARN, { server = true })
391387
Logger.info('client message')
392388
]]
393389
)
394390

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")
391+
expect_match(contents, "%[SERVER%] server stdout message")
392+
expect_match(contents, "%[SERVER%] server stderr message")
393+
expect_match(contents, "client message")
394+
expect_no_match(contents, "%[SERVER%] client message")
399395
end
400396

401397
T["integration"]["EcaLogs command behavior"] = function()

0 commit comments

Comments
 (0)