Skip to content

Instantly share code, notes, and snippets.

@troy
Last active January 12, 2017 21:36
Show Gist options
  • Save troy/55442ad0d2502f9ac0a7 to your computer and use it in GitHub Desktop.
Save troy/55442ad0d2502f9ac0a7 to your computer and use it in GitHub Desktop.
Papertrail JSON formatter
// ==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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
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);
})
});
@daurnimator
Copy link

the tampermonkey script doesn't fire on 'back' button navigation
random guess: you're using history.pushState, so on("papertail:loaded") doesn't fire

@chmac
Copy link

chmac commented Dec 23, 2014

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...

@timdp
Copy link

timdp commented Apr 14, 2016

@chmac For what it's worth, my fork addresses this issue.

@sc0ttkclark
Copy link

sc0ttkclark commented Jun 29, 2016

Please add this to the header for Tampermonkey compatibility plz :)

// @grant GM_addStyle

Related: http://stackoverflow.com/questions/18043088/greasemonkey-script-does-not-load-external-js-and-css-file/18049969#18049969

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment