Skip to content

Commit

Permalink
Normalize various stack traces and try and keep full stack traces fro…
Browse files Browse the repository at this point in the history
…m being displayed in a notification. Also fix #729.
  • Loading branch information
gashcrumb committed Nov 6, 2013
1 parent 3668072 commit 19b1e06
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 16 deletions.
1 change: 1 addition & 0 deletions hawtio-web/src/main/d.ts/logger.d.ts
Expand Up @@ -17,6 +17,7 @@ declare module Logging {
info(...arguments:any[]):void;
warn(...arguments:any[]):void;
error(...arguments:any[]):void;
formatStackTraceString(stack:string):string;
}

export interface LoggerStatic extends Logger {
Expand Down
7 changes: 3 additions & 4 deletions hawtio-web/src/main/webapp/app/core/js/helpers.ts
Expand Up @@ -158,16 +158,15 @@ function onSuccess(fn, options = {}) {
if (stacktrace) {
var silent = options['silent'];
if (!silent) {
console.log(stacktrace);
if (stacktrace.indexOf("javax.management.InstanceNotFoundException") >= 0 ||
stacktrace.indexOf("javax.management.AttributeNotFoundException") >= 0 ||
stacktrace.indexOf(" java.lang.IllegalArgumentException: No operation") >= 0) {
stacktrace.indexOf("java.lang.IllegalArgumentException: No operation") >= 0) {
// ignore these errors as they can happen on timing issues
// such as its been removed
// or if we run against older containers
} else {
notification("error", "Operation failed due to: " + stacktrace);
console.log("Jolokia request failed: " + response.error);
Core.log.warn("Operation ", response['request']['operation'], " failed due to: ", response['error']);
Core.log.info("Stack trace: ", Logger.formatStackTraceString(response['stacktrace']));
}
}
}
Expand Down
112 changes: 101 additions & 11 deletions hawtio-web/src/main/webapp/app/core/js/loggingInit.js
Expand Up @@ -59,26 +59,90 @@ if ('console' in window) {
};
}

var isArrayOrObject = function(o) {
return (!!o) && (o.constructor === Array || o.constructor === Object);
};

function getType(obj) {
return Object.prototype.toString.call(obj).slice(8, -1);
}

function isError(obj) {
return obj && getType(obj) === 'Error';
}

function isArray(obj) {
return obj && getType(obj) === 'Array';
}

function isObject(obj) {
return obj && getType(obj) === 'Object';
}

function isString(obj) {
return obj && getType(obj) === 'String';
}

window['logInterceptors'] = [];

Logger.formatStackTraceString = function(stack) {
var lines = stack.split("\n");
var stackTrace = "<div class=\"log-stack-trace\">\n";

for (var j = 0; j < lines.length; j++) {
var line = lines[j];
if (line.trim().length === 0) {
continue;
}
line = line.replace(/\s/, "&nbsp;");
stackTrace = stackTrace + "<p>" + line + "</p>\n";
}
stackTrace = stackTrace + "</div>\n";
return stackTrace;
};


Logger.setHandler(function(messages, context) {
// MyConsole.log("context: ", context);
// MyConsole.log("messages: ", messages);
var container = document.getElementById("log-panel");
var panel = document.getElementById("log-panel-statements");

var node = document.createElement("li");

var text = ""
var text = "";
var postLog = [];

// try and catch errors logged via console.error(e.toString) and reformat
if (context['level'].name === 'ERROR' && messages.length === 1) {
if (isString(messages[0])) {
var message = messages[0];
var messageSplit = message.split(/\n/);
if (messageSplit.length > 1) {

// we may have more cases that require normalizing, so a more flexible solution
// may be needed
var lookFor = "Error: Jolokia-Error: ";
if (messageSplit[0].search(lookFor) == 0) {
var msg = messageSplit[0].slice(lookFor.length);
window['JSConsole'].info("msg: ", msg);
try {
var errorObject = JSON.parse(msg);
var error = new Error();
error.message = errorObject['error'];
error.stack = errorObject['stacktrace'].replace("\\t", "&nbsp;&nbsp").replace("\\n", "\n");
messages = [error];
} catch (e) {
// we'll just bail and let it get logged as a string...
}
} else {
var error = new Error();
error.message = messageSplit[0];
error.stack = message;
messages = [error];
}
}
}
}

for (var i = 0; i < messages.length; i++) {
var message = messages[i];
if (isArrayOrObject(message)) {

if (isArray(message) || isObject(message)) {
var obj = "" ;
try {
obj = '<pre data-language="javascript">' + JSON.stringify(message, null, 2) + '</pre>';
Expand All @@ -87,6 +151,21 @@ Logger.setHandler(function(messages, context) {
// silently ignore, could be a circular object...
}
text = text + obj;
} else if (isError(message)) {

if ('message' in message) {
text = text + message['message'];
}
if ('stack' in message) {
postLog.push(function() {
var stackTrace = Logger.formatStackTraceString(message['stack']);
var logger = Logger;
if (context.name) {
logger = Logger.get(context['name']);
}
logger.info("Stack trace: ", stackTrace);
});
}
} else {
text = text + message;
}
Expand Down Expand Up @@ -128,6 +207,8 @@ Logger.setHandler(function(messages, context) {

onAdd();

postLog.forEach(function (func) { func(); });

/*
try {
Rainbow.color(node, onAdd);
Expand All @@ -141,9 +222,18 @@ Logger.setHandler(function(messages, context) {
});

// Catch uncaught exceptions and stuff so we can log them
window.onerror = function(msg, url, line) {
Logger.error(msg, ' (<a href="' + url + ':' + line + '">' + url + ':' + line + '</a>)');
// supress error alert
window.onerror = function(msg, url, line, column, errorObject) {
if (errorObject) {
Logger.get("Window").error(errorObject);
} else {
var href = ' (<a href="' + url + ':' + line + '">' + url + ':' + line;

if (column) {
href = href + ':' + column;
}
href = href + '</a>)';
Logger.get("Window").error(msg, href);
}
return true;
};

Expand Down
2 changes: 1 addition & 1 deletion hawtio-web/src/main/webapp/app/jmx/js/helpers.ts
Expand Up @@ -195,4 +195,4 @@ module Jmx {
}
}
}
}
}
14 changes: 14 additions & 0 deletions hawtio-web/src/main/webapp/css/site-base.css
Expand Up @@ -119,6 +119,16 @@ small table tbody tr td.property-name {
opacity: 1;
}

div.log-stack-trace {
font-family: monospace;
}

div.log-stack-trace p {
font-family: monospace;
line-height: 14px;
margin-bottom: 2px;
}

#canvas {
display: inline-block;
}
Expand Down Expand Up @@ -3083,3 +3093,7 @@ tabset > .tabbable > .tab-content > .nav.nav-tabs > li.active {
tabset > .tabbable > .tab-content > .nav.nav-tabs > li.disabled {
opacity: 0.3;
}

.toast.toast-warning * {
color: black;
}
16 changes: 16 additions & 0 deletions hawtio-web/src/main/webapp/index.html
Expand Up @@ -52,12 +52,28 @@
<script type="text/ng-template" id="logClipboardTemplate">
<!--
<style type="text/css">
* {
font-family: monospace;
}
ul li {
list-style-type: none;
}
ul li:nth-child(odd) {
background: #f3f3f3;
}
span.green {
color: green;
}
div.log-stack-trace p {
line-height: 14px;
margin-bottom: 2px;
}
div.log-stack-trace p:nth-child(even) {
background: white;
}
div.log-stack-trace p:nth-child(odd) {
background: #f3f3f3;
}
</style>
<ul>
-->
Expand Down

0 comments on commit 19b1e06

Please sign in to comment.