var consoleArray = [];
var serverlogconfig = {
"debug": false,
"error": true,
"info": true,
"warn": true,
"log": true
};
var createLogger = function(o,m) {
console.log("CREATING LOGGER FOR",o,m);
return function() {
o.apply(console, arguments);
//sendit bro
var result = {};
result.method = m;
var rc = Array.prototype.map.call(arguments, function(x) {
return JSON.stringify(x);
});
result.message = rc.join(" ");
consoleArray.push(result);
};
}
for (method in serverlogconfig) {
if (serverlogconfig[method]) {
var oldMethod = console[method];
console[method] = createLogger(oldMethod,method);
}
}
var app = angular.module('demo', []);
app.controller('DemoCtrl', function($scope, $log) {
$scope.console = consoleArray;
var oldLog = console.log;
try {
for (method in serverlogconfig) {
if (serverlogconfig[method]) {
var oldMethod = console[method];
console[method] = (function(oldMethod) {
return function() {
var argsBro = arguments;
$scope.$apply(function() {
oldMethod.apply(null, argsBro);
});
};
})(oldMethod)
}
}
} catch (e) {}
});
setTimeout(function() {
console.error("You Can Console.whatever at whenever..and it will show up in html.")
})
<!doctype html>
<html ng-app="demo">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
<script src="script.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div ng-controller="DemoCtrl">
<h1>Console Emulator</h1>
<h6>Any console.logs in javascript are displayed in HTML</h6>
<ul>
<li ng-repeat="line in console">
<b>{{line.method}}</b>:
<span>{{line.message}}</span>
</li>
</ul>
</div>
<script>
console.debug("This should not show up");
console.log("This should show up");
console.info("So, Should this.");
console.log("Example 1:")
</script>
</body>
</html>