From 21ad795c8f471ff6a295355c931d06fada8e6825 Mon Sep 17 00:00:00 2001 From: Tom George Date: Wed, 6 Aug 2025 12:01:37 -0400 Subject: [PATCH 1/2] 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 --- lua/eca/logger.lua | 5 +++-- lua/eca/server.lua | 6 ++---- tests/test_logging.lua | 18 +++++++----------- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/lua/eca/logger.lua b/lua/eca/logger.lua index 8ccf9d5..d4b8a5c 100644 --- a/lua/eca/logger.lua +++ b/lua/eca/logger.lua @@ -105,8 +105,8 @@ function M.log(message, level, opts) local timestamp = os.date("%Y-%m-%d %H:%M:%S") local level_name = get_level_name(level) - local prefix = opts.title == "SERVER" and "SERVER" or "CLIENT" - local formatted = string.format("[%s] %-5s [%s] %s\n", timestamp, level_name, prefix, message) + local prefix = opts.server and "[SERVER]" or "" + local formatted = string.format("[%s] %-5s %s %s\n", timestamp, level_name, prefix, message) uv.fs_open(log_path, "a", 420, function(err, fd) if err or not fd then @@ -162,6 +162,7 @@ function M.notify(message, level, opts) }) end + --- Get log file statistics ---@return table|nil function M.get_log_stats() diff --git a/lua/eca/server.lua b/lua/eca/server.lua index bfa7b80..3ef76dd 100644 --- a/lua/eca/server.lua +++ b/lua/eca/server.lua @@ -100,11 +100,10 @@ function M:start() -- Use vim.fn.jobstart for interactive processes local job_opts = { on_stdout = function(_, data, _) - Logger.debug("Server stdout received: " .. vim.inspect(data)) if data and self._rpc then local output = table.concat(data, "\n") if output and output ~= "" and output ~= "\n" then - Logger.debug("Processing stdout: " .. output) + Logger.log(output, vim.log.levels.INFO, { server = true }) self._rpc:_handle_stdout(output) end end @@ -122,8 +121,7 @@ function M:start() if #meaningful_lines > 0 then local error_output = table.concat(meaningful_lines, "\n") - Logger.debug("ECA server stderr: " .. error_output) - -- Capture logs for the logs buffer + Logger.log(error_output, vim.log.levels.WARN, { server = true }) end end end, diff --git a/tests/test_logging.lua b/tests/test_logging.lua index bb9a93f..61afd8c 100644 --- a/tests/test_logging.lua +++ b/tests/test_logging.lua @@ -228,7 +228,6 @@ T["file_logging"]["writes to file with correct format"] = function() expect_match(contents, "%[%d%d%d%d%-%d%d%-%d%d %d%d:%d%d:%d%d%]") expect_match(contents, "INFO") - expect_match(contents, "%[CLIENT%]") expect_match(contents, "test message") end @@ -303,7 +302,6 @@ T["notifications"]["Logger.notify writes to log file"] = function() expect_match(contents, "WARN") expect_match(contents, "test notification message") - expect_match(contents, "%[CLIENT%]") local notifications = child.lua_get("_G.captured_notifications") eq(#notifications, 1) @@ -380,22 +378,20 @@ T["integration"]["logger functions work correctly"] = function() expect_match(contents, "error message") end -T["integration"]["server logging with SERVER prefix"] = function() +T["integration"]["server logging with prefix"] = function() local contents = log_and_read( "server_integration.log", [[ - Logger.info('server message 1', { title = 'SERVER' }) - Logger.warn('server warning', { title = 'SERVER' }) - Logger.error('server error', { title = 'SERVER' }) - + Logger.log('server stdout message', vim.log.levels.INFO, { server = true }) + Logger.log('server stderr message', vim.log.levels.WARN, { server = true }) Logger.info('client message') ]] ) - expect_match(contents, "%[SERVER%] server message 1") - expect_match(contents, "%[SERVER%] server warning") - expect_match(contents, "%[SERVER%] server error") - expect_match(contents, "%[CLIENT%] client message") + expect_match(contents, "%[SERVER%] server stdout message") + expect_match(contents, "%[SERVER%] server stderr message") + expect_match(contents, "client message") + expect_no_match(contents, "%[SERVER%] client message") end T["integration"]["EcaLogs command behavior"] = function() From c66835fc78e82095fb77f82ab28c830b4e3ffb08 Mon Sep 17 00:00:00 2001 From: Tom George Date: Fri, 8 Aug 2025 11:18:02 -0400 Subject: [PATCH 2/2] Remove client/server logging distinction --- lua/eca/logger.lua | 34 ++++++++++++---------------------- lua/eca/server.lua | 4 ++-- tests/test_logging.lua | 16 ---------------- 3 files changed, 14 insertions(+), 40 deletions(-) diff --git a/lua/eca/logger.lua b/lua/eca/logger.lua index d4b8a5c..7340ebd 100644 --- a/lua/eca/logger.lua +++ b/lua/eca/logger.lua @@ -88,10 +88,7 @@ end --- Log a message to the log file ---@param message string ---@param level integer ----@param opts? {title?: string} -function M.log(message, level, opts) - opts = opts or {} - +function M.log(message, level) if not M.config then return end @@ -105,8 +102,7 @@ function M.log(message, level, opts) local timestamp = os.date("%Y-%m-%d %H:%M:%S") local level_name = get_level_name(level) - local prefix = opts.server and "[SERVER]" or "" - local formatted = string.format("[%s] %-5s %s %s\n", timestamp, level_name, prefix, message) + local formatted = string.format("[%s] %-5s %s\n", timestamp, level_name, message) uv.fs_open(log_path, "a", 420, function(err, fd) if err or not fd then @@ -120,49 +116,43 @@ end --- Log debug message ---@param message string ----@param opts? {title?: string} -function M.debug(message, opts) - M.log(message, vim.log.levels.DEBUG, opts) +function M.debug(message) + M.log(message, vim.log.levels.DEBUG) end --- Log info message ---@param message string ----@param opts? {title?: string} -function M.info(message, opts) - M.log(message, vim.log.levels.INFO, opts) +function M.info(message) + M.log(message, vim.log.levels.INFO) end --- Log warn message ---@param message string ----@param opts? {title?: string} -function M.warn(message, opts) - M.log(message, vim.log.levels.WARN, opts) +function M.warn(message) + M.log(message, vim.log.levels.WARN) end --- Log error message ---@param message string ----@param opts? {title?: string} -function M.error(message, opts) - M.log(message, vim.log.levels.ERROR, opts) +function M.error(message) + M.log(message, vim.log.levels.ERROR) end --- Send notification to user via vim.notify ---@param message string ---@param level? integer vim.log.levels (default: INFO) ----@param opts? {title?: string, once?: boolean} +---@param opts? {title?: string} function M.notify(message, level, opts) level = level or vim.log.levels.INFO opts = opts or {} - M.log(message, level, opts) + M.log(message, level) vim.notify(message, level, { title = opts.title or "ECA", - once = opts.once, }) end - --- Get log file statistics ---@return table|nil function M.get_log_stats() diff --git a/lua/eca/server.lua b/lua/eca/server.lua index 3ef76dd..11ec5c5 100644 --- a/lua/eca/server.lua +++ b/lua/eca/server.lua @@ -103,7 +103,7 @@ function M:start() if data and self._rpc then local output = table.concat(data, "\n") if output and output ~= "" and output ~= "\n" then - Logger.log(output, vim.log.levels.INFO, { server = true }) + Logger.log(output, vim.log.levels.INFO) self._rpc:_handle_stdout(output) end end @@ -121,7 +121,7 @@ function M:start() if #meaningful_lines > 0 then local error_output = table.concat(meaningful_lines, "\n") - Logger.log(error_output, vim.log.levels.WARN, { server = true }) + Logger.log(error_output, vim.log.levels.WARN) end end end, diff --git a/tests/test_logging.lua b/tests/test_logging.lua index 61afd8c..189d78a 100644 --- a/tests/test_logging.lua +++ b/tests/test_logging.lua @@ -378,22 +378,6 @@ T["integration"]["logger functions work correctly"] = function() expect_match(contents, "error message") end -T["integration"]["server logging with prefix"] = function() - local contents = log_and_read( - "server_integration.log", - [[ - Logger.log('server stdout message', vim.log.levels.INFO, { server = true }) - Logger.log('server stderr message', vim.log.levels.WARN, { server = true }) - Logger.info('client message') - ]] - ) - - expect_match(contents, "%[SERVER%] server stdout message") - expect_match(contents, "%[SERVER%] server stderr message") - expect_match(contents, "client message") - expect_no_match(contents, "%[SERVER%] client message") -end - T["integration"]["EcaLogs command behavior"] = function() setup_logger_with_file("eca_logs_test.log")