Enhance logging with timestamps and filtering.

This commit is contained in:
Andy Phillipson 2017-07-14 15:21:15 -04:00
commit b7cfd86f22
No known key found for this signature in database
GPG key ID: D813A67D567D6C88
14 changed files with 257 additions and 13 deletions

View file

@ -11,6 +11,7 @@ var modules = [
'ngLodash',
'ngCsv',
'angular-md5',
'rzModule',
'bwcModule',
'bitauthModule',
'copayApp.filters',

View file

@ -3,17 +3,51 @@
angular.module('copayApp.controllers').controller('preferencesLogs',
function($scope, historicLog, platformInfo) {
var logLevels = historicLog.getLevels();
var logFilterWeight = historicLog.getDefaultLevel().weight;
// Log level slider setup.
var logLevelSliderInitialValue = logFilterWeight;
var logLevelSliderCeil = logFilterWeight;
var logLevelSliderStepsArray = [];
for (var i = 0; i < logLevels.length; i++) {
logLevelSliderStepsArray.push({value: logLevels[i].weight, legend: logLevels[i].label});
}
$scope.logOptionsTitle = 'Filter log';
$scope.logOptions = {
logLevelSlider: {
value: logLevelSliderInitialValue,
opts: {
floor: 0,
ceil: logLevelSliderCeil,
step: 1,
hideLimitLabels: true,
hidePointerLabels: true,
showTicks: true,
showTicksValues: false,
showSelectionBar: true,
stepsArray: logLevelSliderStepsArray,
onEnd: function(sliderId, modelValue, highValue, pointerType) {
$scope.filteredLogs = historicLog.get(modelValue);
}
}
}
};
$scope.$on("$ionicView.beforeEnter", function(event, data) {
$scope.isCordova = platformInfo.isCordova;
});
$scope.$on("$ionicView.enter", function(event, data) {
$scope.logs = historicLog.get();
$scope.allLogs = historicLog.get();
$scope.filteredLogs = historicLog.get(logFilterWeight);
$scope.prepare = function() {
var log = 'Copay Session Logs\n Be careful, this could contain sensitive private data\n\n';
log += '\n\n';
log += $scope.logs.map(function(v) {
log += $scope.allLogs.map(function(v) {
return v.msg;
}).join('\n');
@ -35,5 +69,8 @@ angular.module('copayApp.controllers').controller('preferencesLogs',
);
};
$scope.showOptionsMenu = function() {
$scope.showOptions = true;
};
});
});

View file

@ -0,0 +1,20 @@
'use strict';
angular.module('copayApp.directives')
.directive('logOptions', function($timeout) {
return {
restrict: 'E',
templateUrl: 'views/includes/logOptions.html',
transclude: true,
scope: {
show: '=logOptionsShow',
options: '=logOptions',
title: '=logOptionsTitle'
},
link: function(scope, element, attrs) {
scope.hide = function() {
scope.show = false;
};
}
};
});

View file

@ -44,7 +44,8 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
function($delegate, platformInfo) {
var historicLog = historicLogProvider.$get();
['debug', 'info', 'warn', 'error', 'log'].forEach(function(level) {
historicLog.getLevels().forEach(function(levelDesc) {
var level = levelDesc.level;
if (platformInfo.isDevel && level == 'error') return;
var orig = $delegate[level];
@ -75,7 +76,9 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
console.log('Error at log decorator:', e);
v = 'undefined';
}
return v;
var ts = '[' + new Date().toISOString() + ']';
var lvl = '[' + level + '] ';
return ts + lvl + v;
});
try {

View file

@ -1,9 +1,32 @@
'use strict';
var logs = [];
angular.module('copayApp.services')
.factory('historicLog', function historicLog() {
.factory('historicLog', function historicLog(lodash) {
var root = {};
var levels = [
{ level: 'info', weight: 0, label: 'Info'},
{ level: 'warn', weight: 1, label: 'Warning'},
{ level: 'error', weight: 2, label: 'Error'},
{ level: 'debug', weight: 3, label: 'Debug', default: true}
];
// Create an array of level weights for performant filtering.
var weight = {};
for (var i = 0; i < levels.length; i++) {
weight[levels[i].level] = levels[i].weight;
}
root.getLevels = function() {
return levels;
};
root.getDefaultLevel = function() {
return lodash.find(levels, function(l) {
return l.default;
});
};
root.add = function(level, msg) {
logs.push({
level: level,
@ -11,8 +34,14 @@ angular.module('copayApp.services')
});
};
root.get = function() {
return logs;
root.get = function(filterWeight) {
var filteredLogs = logs;
if (filterWeight != undefined) {
filteredLogs = lodash.filter(logs, function(l) {
return weight[l.level] <= filterWeight;
});
}
return filteredLogs;
};
return root;

View file

@ -134,7 +134,7 @@ angular.module('copayApp.services')
wallet.setNotificationsInterval(UPDATE_PERIOD);
wallet.openWallet(function(err) {
if (wallet.status !== true)
$log.log('Wallet + ' + walletId + ' status:' + wallet.status)
$log.debug('Wallet + ' + walletId + ' status:' + wallet.status)
});
});