Created
June 26, 2018 17:24
-
-
Save icelander/56594c232f16f4fb46098d5c351bc227 to your computer and use it in GitHub Desktop.
A log viewer script for Mattermost logs with error highlighting and an easier to read format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'colorize' | |
require 'json' | |
require 'time' | |
require 'erb' | |
require 'ostruct' | |
file_path = ARGV[0] | |
def process_json_line(line) | |
line = JSON.parse(line) | |
line = line.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} | |
line[:time] = Time.at(line[:ts]) | |
return line | |
end | |
def process_text_line(line) | |
line_array = line.split(/\[(.+)\] \[(.+)\] (.+)/) | |
# For some reason there's an empty string at the beginning | |
line_array.shift() | |
event_time = Time.parse(line_array[0]) | |
case line_array[1] | |
when 'EROR' | |
event_level = 'error' | |
when 'DEBG' | |
event_level = 'debug' | |
when 'INFO' | |
event_level = 'info' | |
end | |
event_msg = line_array[2] | |
return { | |
time: event_time, | |
level: event_level, | |
caller: nil, | |
source: nil, | |
msg: event_msg | |
} | |
end | |
def output_line(line_hash) | |
line_hash[:time] = line_hash[:time].strftime('%Y-%m-%d %H:%M:%S.%3N') | |
line_format = <<~DOC | |
<%= time %> <% if !level.nil? %><%= level.upcase %><% if [email protected]? %><% end %> | |
- Caller: <%= @caller %><% end %><% if !source.nil? %> | |
- Source: <%= source %><% end %> | |
- Message <%= msg %> | |
DOC | |
output = erb(line_format, line_hash) | |
case line_hash[:level] | |
when 'error' | |
puts output.red | |
when 'debug' | |
puts output.yellow | |
when 'info' | |
puts output.blue | |
else | |
puts output | |
end | |
end | |
def erb(template, vars) | |
ERB.new(template).result(OpenStruct.new(vars).instance_eval { binding }) | |
end | |
File.readlines(file_path).each do |line| | |
case line | |
when /^{/ | |
line = process_json_line(line) | |
else | |
line = process_text_line(line) | |
end | |
output_line line | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment