Skip to content

Commit

Permalink
(refactor) formatlogjson
Browse files Browse the repository at this point in the history
  • Loading branch information
h4ckm1n-dev committed Jun 16, 2024
1 parent 37a15b2 commit e962413
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions lua/kube-utils-nvim/formatjson.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ local module_patterns = {
"[%w_/%.]+%.%a+:%d+", -- e.g., "path/to/file.ext:123"
}

local patterns = {
local timestamp_patterns = {
"%d%d%d%d%-%d%d%-%d%d[T ]%d%d:%d%d:%d%d[%.%d]*[+-]%d%d:?%d%d", -- ISO 8601 with milliseconds and timezone
"%d%d%d%d%-%d%d%-%d%d[T ]%d%d:%d%d:%d%d[%.%d]*Z?", -- ISO 8601 with optional milliseconds and 'T'
"%d%d%d%d%-%d%d%-%d%d[T ]%d%d:%d%d:%d%d[+-]%d%d:?%d%d", -- ISO 8601 with time zone
Expand All @@ -43,7 +43,7 @@ local patterns = {
}

local function parseTimestamp(line)
for _, pattern in ipairs(patterns) do
for _, pattern in ipairs(timestamp_patterns) do
local timestamp = line:match(pattern)
if timestamp then
return timestamp
Expand All @@ -59,60 +59,89 @@ local function parseLogLevel(line)
{ level = "WARNING", pattern = "%[%s*warning%s*%]" },
{ level = "INFO", pattern = "%[%s*info%s*%]" },
{ level = "DEBUG", pattern = "%[%s*debug%s*%]" },
{ level = "LEVEL(-2)", pattern = "level%(%-2%)" }, -- Adjusted pattern for LEVEL(-2)
{ level = "LEVEL", pattern = "level%((%-?%d+)%)" }, -- Adjusted pattern for LEVEL(-n)
}
local lower_line = line:lower()
for _, log_level in ipairs(log_level_patterns) do
if lower_line:find(log_level.pattern) then
return log_level.level
if log_level.level == "LEVEL" then
local number = lower_line:match("level%((%-?%d+)%)")
return "LEVEL(" .. number .. ")"
else
return log_level.level
end
end
end
return "INFO"
end

-- Enhanced function to parse log metadata, ensuring modules are not mistaken for timestamps
-- Function to parse log metadata, ensuring modules are not mistaken for timestamps
local function parseLogMetadata(line)
local log_level = parseLogLevel(line)
local message = line
local module = "unknown"
local json_start = line:find("{")
local structured_data = nil
local message = line
local plain_text_message = line

-- Attempt to parse structured JSON data if present
-- Parse structured JSON data if present
if json_start then
local json_possible = line:sub(json_start)
local status, json_data = pcall(vim.fn.json_decode, json_possible)
if status then
structured_data = json_data
message = "Structured JSON data present"
plain_text_message = line:sub(1, json_start - 1)
end
end

-- Try to match module patterns, ensuring no common timestamp formats are matched
-- Extract the timestamp first
local timestamp = parseTimestamp(line)

-- Remove the timestamp from the line to prevent it from being matched as a module
if timestamp ~= "unknown" then
line = line:gsub(timestamp, "")
end

-- Match module patterns, ensuring no common timestamp formats are matched
local parsed = false
for _, pattern in ipairs(module_patterns) do
local matched_module = line:match(pattern)
if matched_module and not matched_module:match("%d%d:%d%d:%d%d") then -- Avoid matching timestamps
module = matched_module
parsed = true
break
end
end

return log_level, module, message, structured_data
-- If nothing could be parsed, keep the full message
if not parsed then
message = line
end

return log_level, module, message, structured_data, plain_text_message
end

local function FormatJsonLogs()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local formatted_lines = { "[" }
for i, line in ipairs(lines) do
local timestamp = parseTimestamp(line) or "unknown"
local log_level, module, message, structured_data = parseLogMetadata(line)
local log_level, module, message, structured_data, plain_text_message = parseLogMetadata(line)
local log_entry = {
timestamp = timestamp,
log_level = log_level,
module = module or "unknown",
message = message,
structured_data = structured_data,
plain_text_message = plain_text_message,
}
if structured_data then
log_entry.structured_data = structured_data
end
-- Remove plain_text_message if it is the same as message
if log_entry.message == log_entry.plain_text_message then
log_entry.plain_text_message = nil
end
local json_text = vim.fn.json_encode(log_entry)
table.insert(formatted_lines, "\t" .. json_text .. (i < #lines and "," or ""))
end
Expand Down

0 comments on commit e962413

Please sign in to comment.