Skip to content

Instantly share code, notes, and snippets.

@tstack
Created September 16, 2020 03:35
Show Gist options
  • Save tstack/bced94c4d1761e03e4b33738263e3c47 to your computer and use it in GitHub Desktop.
Save tstack/bced94c4d1761e03e4b33738263e3c47 to your computer and use it in GitHub Desktop.
lnav script to mark error messages and an immediately preceding "good" message
#
# @synopsis: mark-error "GoodMsg" "BadMsg"
# @description: Find instances of "BadMsg", then bookmark it and the preceding "GoodMsg"
#
;WITH badlines AS (
-- Select the bad and good line numbers from the for the "BadMsg" log messages
-- that were found
SELECT badline, goodline FROM (
-- Find the log messages that contain "GoodMsg" or "BadMsg"
SELECT log_body, log_line AS badline,
-- Window function to find the preceding "GoodMsg"
max(log_line)
FILTER (WHERE log_body LIKE '%' || $1 || '%')
OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW)
AS goodline
FROM all_logs
WHERE log_body LIKE '%' || $1 || '%' OR
log_body LIKE '%' || $2 || '%')
WHERE log_body like '%' || $2 || '%')
-- Bookmark the bad and immediately preceding good log messages
UPDATE all_logs SET log_mark = 1
WHERE log_line IN (SELECT badline FROM badlines UNION ALL
SELECT goodline FROM badlines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment