Skip to content

Commit

Permalink
Format output better and linting (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
astilla authored Feb 18, 2018
1 parent 46aac1c commit 0491912
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 49 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "app-logger-angular",
"version": "2.1.4",
"version": "2.1.5",
"main": "./js/logging.js",
"description": "Client side logging sent to the server",
"repository": {
Expand Down
100 changes: 52 additions & 48 deletions js/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ var loggingModule = angular.module('talis.services.logging', []);
* methods in the global context is not the 'angular way'
*/
loggingModule.factory(
"stacktraceService",
function(){
return({
'stacktraceService',
function () {
return ({
print: printStackTrace
});
}
Expand All @@ -29,23 +29,21 @@ loggingModule.factory(
/**
* Override Angular's built in exception handler, and tell it to use our new exceptionLoggingService
*/
loggingModule.provider(
"$exceptionHandler",{
$get: function(exceptionLoggingService){
return(exceptionLoggingService);
}
loggingModule.provider('$exceptionHandler', {
$get: function (exceptionLoggingService) {
return (exceptionLoggingService);
}
);
});

/**
* Exception Logging Service, currently only used by the $exceptionHandler but logs to the console and uses the
* stacktraceService to generate a browser independent stacktrace which is POSTed to server side clientlogging
* service.
*/
loggingModule.factory(
"exceptionLoggingService",
["$log","$window", "stacktraceService", "LOGGING_CONFIG", function($log, $window, stacktraceService, LOGGING_CONFIG){
function error( exception, cause){
'exceptionLoggingService',
['$log', '$window', 'stacktraceService', 'LOGGING_CONFIG', function ($log, $window, stacktraceService, LOGGING_CONFIG) {
function error(exception, cause) {
if (LOGGING_CONFIG.LOGGING_TYPE !== 'none') {
// preserve default behaviour i.e. dump to browser console
$log.error.apply($log, arguments);
Expand All @@ -56,29 +54,29 @@ loggingModule.factory(
// now log server side.
try {
var errorMessage = exception.toString();
var stackTrace = stacktraceService.print({e: exception});
var stackTrace = stacktraceService.print({ e: exception });

// use AJAX not an angular service because if something has gone wrong
// angular might be fubar'd
$.ajax({
type: "POST",
type: 'POST',
url: LOGGING_CONFIG.REMOTE_LOGGING_ENDPOINT,
contentType: "application/json",
contentType: 'application/json',
data: angular.toJson({
url: $window.location.href,
message: errorMessage,
type: "exception",
type: 'exception',
stackTrace: stackTrace,
cause: ( cause || "")
cause: (cause || '')
})
});
} catch (loggingError) {
$log.warn("Error logging failed");
$log.warn('Error logging failed');
$log.log(loggingError);
}
}
}
return( error );
return (error);
}]
);

Expand All @@ -88,8 +86,8 @@ loggingModule.factory(
* server specifically eg: $http.get().error( function(){ call applicationloggingservice here })
*/
loggingModule.factory(
"applicationLoggingService",
["$log","$window", "LOGGING_CONFIG", function($log, $window, LOGGING_CONFIG){
'applicationLoggingService',
['$log', '$window', 'LOGGING_CONFIG', function ($log, $window, LOGGING_CONFIG) {
var arrLoggingLevels = ['trace', 'debug', 'info', 'warn', 'error'];
var loggingThreshold = LOGGING_CONFIG.LOGGING_THRESHOLD || 'info';
var iLoggingThreshold = arrLoggingLevels.indexOf(loggingThreshold);
Expand All @@ -106,17 +104,17 @@ loggingModule.factory(
*/
var defaultData = null;

var isLoggingEnabledForSeverity = function(severity) {
var isLoggingEnabledForSeverity = function (severity) {
var iRequestedLevel = arrLoggingLevels.indexOf(severity);
if (iRequestedLevel === -1) {
// Invalid level requested
return false;
}

return (iRequestedLevel >= iLoggingThreshold);
}
};

var log = function(severity, message, desc) {
var log = function (severity, message, desc) {
if (!isLoggingEnabledForSeverity(severity)) {
return;
}
Expand All @@ -130,6 +128,7 @@ loggingModule.factory(
}

if (desc) {
desc = JSON.stringify(desc);
$log[angularLogSeverity](message, desc);
} else {
$log[angularLogSeverity](message);
Expand All @@ -139,39 +138,44 @@ loggingModule.factory(
// check if the config says we should log to the remote, and also if a remote endpoint was specified
if (LOGGING_CONFIG.LOGGING_TYPE === 'remote' && LOGGING_CONFIG.REMOTE_LOGGING_ENDPOINT) {
// send server side
var data = {
type: severity,
message: message,
url: $window.location.href,
overrideLoggingThreshold: overrideLoggingThreshold
};
if (desc) {
data.desc = desc;
}
if (defaultData) {
data.defaultData = defaultData;
}
$.ajax({
type: "POST",
type: 'POST',
url: LOGGING_CONFIG.REMOTE_LOGGING_ENDPOINT,
contentType: "application/json",
data: angular.toJson({
type: severity,
url: $window.location.href,
message: message,
desc: desc,
defaultData: defaultData,
overrideLoggingThreshold: overrideLoggingThreshold
})
contentType: 'application/json',
data: angular.toJson(data)
});
}
};

return({
trace: function(message, desc) {
return ({
trace: function (message, desc) {
log('trace', message, desc);
},
debug: function(message, desc) {
debug: function (message, desc) {
log('debug', message, desc);
},
info: function(message, desc) {
info: function (message, desc) {
log('info', message, desc);
},
warn: function(message, desc) {
warn: function (message, desc) {
log('warn', message, desc);
},
error: function(message, desc) {
error: function (message, desc) {
log('error', message, desc);
},
setLoggingThreshold: function(level) {
setLoggingThreshold: function (level) {
/*
* Normally the logger would use the logging threshold passed in on the config hash but an
* application may want to override this dynamically, e.g. to enable a different logging
Expand All @@ -182,18 +186,18 @@ loggingModule.factory(
overrideLoggingThreshold = true;
}
},
setDefaultData: function(data) {
setDefaultData: function (data) {
defaultData = data;
}
});
}]
);

loggingModule.factory(
"userErrorReport",
['$window','$rootScope','LOGGING_CONFIG',function($window,$rootScope,LOGGING_CONFIG) {
return({
send: function(userMessage,error) {
'userErrorReport',
['$window', '$rootScope', 'LOGGING_CONFIG', function ($window, $rootScope, LOGGING_CONFIG) {
return ({
send: function (userMessage, error) {
var payload = {
url: $window.location.href,
systemError: error,
Expand All @@ -204,9 +208,9 @@ loggingModule.factory(
// check if the config says we should log to the remote, and also if a remote endpoint was specified
if (LOGGING_CONFIG.LOGGING_TYPE === 'remote' && LOGGING_CONFIG.REMOTE_ERROR_REPORT_ENDPOINT) {
$.ajax({
type: "POST",
type: 'POST',
url: LOGGING_CONFIG.REMOTE_ERROR_REPORT_ENDPOINT,
contentType: "application/json",
contentType: 'application/json',
data: angular.toJson(payload)
});
}
Expand Down

0 comments on commit 0491912

Please sign in to comment.