Skip to content

Commit

Permalink
add timesatamps parsing in formatjson function
Browse files Browse the repository at this point in the history
  • Loading branch information
h4ckm1n-dev committed Jun 16, 2024
1 parent fb30776 commit 7627bef
Showing 1 changed file with 53 additions and 43 deletions.
96 changes: 53 additions & 43 deletions lua/kube-utils-nvim/formatjson.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,73 @@
local M = {}

local function parseTimestamp(line)
-- Extendable timestamp patterns
local patterns = {
"%d%d%d%d%-%d%d%-%d%d[%sT]%d%d:%d%d:%d%d[%.%d]*Z?", -- ISO 8601 with optional milliseconds and 'T'
"%d%d%d%d/%d%d/%d%d %d%d:%d%d:%d%d", -- Alternative format e.g., YYYY/MM/DD HH:MM:SS
}
for _, pattern in ipairs(patterns) do
local timestamp = line:match("^" .. pattern)
if timestamp then return timestamp end
end
return nil
-- Extendable timestamp patterns, covering common and Kubernetes-specific patterns
local patterns = {
"%d%d%d%d%-%d%d%-%d%d[%sT]%d%d:%d%d:%d%d[%.%d]*Z?", -- ISO 8601 with optional milliseconds and 'T', standard in Kubernetes logs
"%d%d%d%d%-%d%d%-%d%d[%sT]%d%d:%d%d:%d%d[+-]%d%d:?%d%d", -- ISO 8601 with time zone
"%d+", -- Unix timestamp (seconds since Unix epoch), sometimes used in logs for simplicity
"%d%d/%d%d/%d%d%d%d %d%d:%d%d:%d%d", -- U.S. style date-time e.g., MM/DD/YYYY HH:MM:SS, less common but possible
"%d%d%-%d%d%-%d%d%d%d %d%d:%d%d:%d%d", -- U.S. style date-time with dashes e.g., MM-DD-YYYY HH:MM:SS
"%a%a%a %d%d %d%d:%d%d:%d%d %d%d%d%d", -- RFC 2822 style e.g., Jan 01 23:59:59 2021, seen in some system logs
"%a%a%a %a%a%a %d%d %d%d:%d%d:%d%d %d%d%d%d", -- Full RFC 2822 with day and month names e.g., Mon Jan 01 23:59:59 2021
"%d%d%a%a%a%d%d%d%d %d%d:%d%d:%d%d", -- Kubernetes logs special format e.g., 01Jan2021 23:59:59
}
for _, pattern in ipairs(patterns) do
local timestamp = line:match("^" .. pattern)
if timestamp then
return timestamp
end
end
return nil
end

local function parseLogMetadata(line, timestamp)
local log_level, module, message
if timestamp then
local pattern = "^" .. timestamp:gsub("([%.%-:%/%s])", "%%%1") .. "%s+([A-Z]+)%s+([%w%.%-_]+)%s+(.*)"
_, _, log_level, module, message = line:find(pattern)
end
return log_level, module, message
local log_level, module, message
if timestamp then
local pattern = "^" .. timestamp:gsub("([%.%-:%/%s])", "%%%1") .. "%s+([A-Z]+)%s+([%w%.%-_]+)%s+(.*)"
_, _, log_level, module, message = line:find(pattern)
end
return log_level, module, message
end

local function extractJsonPart(line)
local json_part = line:match("{.*}") -- Extract JSON if present
if json_part then
local success, json_data = pcall(vim.fn.json_decode, json_part)
if success then return json_data end
end
return {}
local json_part = line:match("{.*}") -- Extract JSON if present
if json_part then
local success, json_data = pcall(vim.fn.json_decode, json_part)
if success then
return json_data
end
end
return {}
end

local function FormatJsonLogs()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local formatted_lines = {"["}
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local formatted_lines = { "[" }

for i, line in ipairs(lines) do
local json_data = extractJsonPart(line)
local timestamp = parseTimestamp(line) or "unknown"
local log_level, module, message = parseLogMetadata(line, timestamp)
for i, line in ipairs(lines) do
local json_data = extractJsonPart(line)
local timestamp = parseTimestamp(line) or "unknown"
local log_level, module, message = parseLogMetadata(line, timestamp)

local log_entry = {
timestamp = timestamp,
log_level = log_level or "INFO",
module = module,
message = message or line -- Use the entire line as message if specific parsing fails
}
local log_entry = {
timestamp = timestamp,
log_level = log_level or "INFO",
module = module,
message = message or line, -- Use the entire line as message if specific parsing fails
}

-- Merge structured JSON data with extracted data
for key, value in pairs(json_data) do
log_entry[key] = value
end
-- Merge structured JSON data with extracted data
for key, value in pairs(json_data) do
log_entry[key] = value
end

local json_text = vim.fn.json_encode(log_entry)
table.insert(formatted_lines, "\t" .. json_text .. (i < #lines and "," or ""))
end
local json_text = vim.fn.json_encode(log_entry)
table.insert(formatted_lines, "\t" .. json_text .. (i < #lines and "," or ""))
end

table.insert(formatted_lines, "]")
vim.api.nvim_buf_set_lines(0, 0, -1, false, formatted_lines)
table.insert(formatted_lines, "]")
vim.api.nvim_buf_set_lines(0, 0, -1, false, formatted_lines)
end

M.format = FormatJsonLogs
Expand Down

0 comments on commit 7627bef

Please sign in to comment.