Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions lua/eca/logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.title == "SERVER" and "SERVER" or "CLIENT"
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
Expand All @@ -120,45 +116,40 @@ 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

Expand Down
6 changes: 2 additions & 4 deletions lua/eca/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
self._rpc:_handle_stdout(output)
end
end
Expand All @@ -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)
end
end
end,
Expand Down
20 changes: 0 additions & 20 deletions tests/test_logging.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -380,24 +378,6 @@ T["integration"]["logger functions work correctly"] = function()
expect_match(contents, "error message")
end

T["integration"]["server logging with SERVER 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.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")
end

T["integration"]["EcaLogs command behavior"] = function()
setup_logger_with_file("eca_logs_test.log")

Expand Down