-
-
Save troy/55442ad0d2502f9ac0a7 to your computer and use it in GitHub Desktop.
Papertrail JSON formatter
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
// ==UserScript== | |
// @name Papertrail JSON formatter | |
// @namespace https://papertrailapp.com/ | |
// @version 0.1 | |
// @description Format and colorize JSON log messages in Papertrail | |
// @match https://papertrailapp.com/*events* | |
// @copyright 2014+, Papertrail (http://wiki.creativecommons.org/Public_domain) | |
// ==/UserScript== | |
GM_addStyle(".string { color: #6c71c4; } .number { color: #2aa198; } .boolean { color: #859900; } .null { color: #586e75; } .key { color: #d33682; }"); | |
function syntaxHighlight(json) { | |
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); | |
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) { | |
var cls = 'number'; | |
if (/^"/.test(match)) { | |
if (/:$/.test(match)) { | |
cls = 'key'; | |
} else { | |
cls = 'string'; | |
} | |
} else if (/true|false/.test(match)) { | |
cls = 'boolean'; | |
} else if (/null/.test(match)) { | |
cls = 'null'; | |
} | |
return '<span class="' + cls + '">' + match + '</span>'; | |
}); | |
} | |
$('ul#event_list').on('papertrail:loaded', function() { | |
$('span.message').each(function(m) { | |
console.log("Parsing: " + $(this).text()); | |
if ($(this).text().substring(0, 1) != '{') { | |
return; | |
} | |
try { | |
var formattedString = JSON.stringify(jQuery.parseJSON($(this).text()), undefined, 4); | |
} catch(err) { | |
return; | |
} | |
console.log(formattedString); | |
formattedString = syntaxHighlight(formattedString); | |
formattedString = formattedString.replace(/^{+/g, '').replace(/}+$/g, ''); | |
formatted = document.createElement('pre'); | |
formatted = formattedString.replace(/\n /mg, "\n "); | |
console.log(formattedString); | |
formatted.innerHTML = formattedString; | |
$(this).html(formatted); | |
}) | |
}); |
This script runs continuously and spams my logs non stop with one line per log line on my screen. :-(
Also, it only parses json which appears at the beginning of the log line. We're using winston to push logs, so we push a message plus a json body, the message itself is not inside the JSON. We have log lines that look like:
Awesome log message {"some":{"wicked":"json here"}}
I guess we could work on the script to parse out these messages, but feels like it's going to be a bit of a hassle...
@chmac For what it's worth, my fork addresses this issue.
Please add this to the header for Tampermonkey compatibility plz :)
// @grant GM_addStyle
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the tampermonkey script doesn't fire on 'back' button navigation
random guess: you're using history.pushState, so on("papertail:loaded") doesn't fire