Skip to content

Commit 0491912

Browse files
authored
Format output better and linting (#9)
1 parent 46aac1c commit 0491912

File tree

2 files changed

+53
-49
lines changed

2 files changed

+53
-49
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "app-logger-angular",
3-
"version": "2.1.4",
3+
"version": "2.1.5",
44
"main": "./js/logging.js",
55
"description": "Client side logging sent to the server",
66
"repository": {

js/logging.js

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ var loggingModule = angular.module('talis.services.logging', []);
1818
* methods in the global context is not the 'angular way'
1919
*/
2020
loggingModule.factory(
21-
"stacktraceService",
22-
function(){
23-
return({
21+
'stacktraceService',
22+
function () {
23+
return ({
2424
print: printStackTrace
2525
});
2626
}
@@ -29,23 +29,21 @@ loggingModule.factory(
2929
/**
3030
* Override Angular's built in exception handler, and tell it to use our new exceptionLoggingService
3131
*/
32-
loggingModule.provider(
33-
"$exceptionHandler",{
34-
$get: function(exceptionLoggingService){
35-
return(exceptionLoggingService);
36-
}
32+
loggingModule.provider('$exceptionHandler', {
33+
$get: function (exceptionLoggingService) {
34+
return (exceptionLoggingService);
3735
}
38-
);
36+
});
3937

4038
/**
4139
* Exception Logging Service, currently only used by the $exceptionHandler but logs to the console and uses the
4240
* stacktraceService to generate a browser independent stacktrace which is POSTed to server side clientlogging
4341
* service.
4442
*/
4543
loggingModule.factory(
46-
"exceptionLoggingService",
47-
["$log","$window", "stacktraceService", "LOGGING_CONFIG", function($log, $window, stacktraceService, LOGGING_CONFIG){
48-
function error( exception, cause){
44+
'exceptionLoggingService',
45+
['$log', '$window', 'stacktraceService', 'LOGGING_CONFIG', function ($log, $window, stacktraceService, LOGGING_CONFIG) {
46+
function error(exception, cause) {
4947
if (LOGGING_CONFIG.LOGGING_TYPE !== 'none') {
5048
// preserve default behaviour i.e. dump to browser console
5149
$log.error.apply($log, arguments);
@@ -56,29 +54,29 @@ loggingModule.factory(
5654
// now log server side.
5755
try {
5856
var errorMessage = exception.toString();
59-
var stackTrace = stacktraceService.print({e: exception});
57+
var stackTrace = stacktraceService.print({ e: exception });
6058

6159
// use AJAX not an angular service because if something has gone wrong
6260
// angular might be fubar'd
6361
$.ajax({
64-
type: "POST",
62+
type: 'POST',
6563
url: LOGGING_CONFIG.REMOTE_LOGGING_ENDPOINT,
66-
contentType: "application/json",
64+
contentType: 'application/json',
6765
data: angular.toJson({
6866
url: $window.location.href,
6967
message: errorMessage,
70-
type: "exception",
68+
type: 'exception',
7169
stackTrace: stackTrace,
72-
cause: ( cause || "")
70+
cause: (cause || '')
7371
})
7472
});
7573
} catch (loggingError) {
76-
$log.warn("Error logging failed");
74+
$log.warn('Error logging failed');
7775
$log.log(loggingError);
7876
}
7977
}
8078
}
81-
return( error );
79+
return (error);
8280
}]
8381
);
8482

@@ -88,8 +86,8 @@ loggingModule.factory(
8886
* server specifically eg: $http.get().error( function(){ call applicationloggingservice here })
8987
*/
9088
loggingModule.factory(
91-
"applicationLoggingService",
92-
["$log","$window", "LOGGING_CONFIG", function($log, $window, LOGGING_CONFIG){
89+
'applicationLoggingService',
90+
['$log', '$window', 'LOGGING_CONFIG', function ($log, $window, LOGGING_CONFIG) {
9391
var arrLoggingLevels = ['trace', 'debug', 'info', 'warn', 'error'];
9492
var loggingThreshold = LOGGING_CONFIG.LOGGING_THRESHOLD || 'info';
9593
var iLoggingThreshold = arrLoggingLevels.indexOf(loggingThreshold);
@@ -106,17 +104,17 @@ loggingModule.factory(
106104
*/
107105
var defaultData = null;
108106

109-
var isLoggingEnabledForSeverity = function(severity) {
107+
var isLoggingEnabledForSeverity = function (severity) {
110108
var iRequestedLevel = arrLoggingLevels.indexOf(severity);
111109
if (iRequestedLevel === -1) {
112110
// Invalid level requested
113111
return false;
114112
}
115113

116114
return (iRequestedLevel >= iLoggingThreshold);
117-
}
115+
};
118116

119-
var log = function(severity, message, desc) {
117+
var log = function (severity, message, desc) {
120118
if (!isLoggingEnabledForSeverity(severity)) {
121119
return;
122120
}
@@ -130,6 +128,7 @@ loggingModule.factory(
130128
}
131129

132130
if (desc) {
131+
desc = JSON.stringify(desc);
133132
$log[angularLogSeverity](message, desc);
134133
} else {
135134
$log[angularLogSeverity](message);
@@ -139,39 +138,44 @@ loggingModule.factory(
139138
// check if the config says we should log to the remote, and also if a remote endpoint was specified
140139
if (LOGGING_CONFIG.LOGGING_TYPE === 'remote' && LOGGING_CONFIG.REMOTE_LOGGING_ENDPOINT) {
141140
// send server side
141+
var data = {
142+
type: severity,
143+
message: message,
144+
url: $window.location.href,
145+
overrideLoggingThreshold: overrideLoggingThreshold
146+
};
147+
if (desc) {
148+
data.desc = desc;
149+
}
150+
if (defaultData) {
151+
data.defaultData = defaultData;
152+
}
142153
$.ajax({
143-
type: "POST",
154+
type: 'POST',
144155
url: LOGGING_CONFIG.REMOTE_LOGGING_ENDPOINT,
145-
contentType: "application/json",
146-
data: angular.toJson({
147-
type: severity,
148-
url: $window.location.href,
149-
message: message,
150-
desc: desc,
151-
defaultData: defaultData,
152-
overrideLoggingThreshold: overrideLoggingThreshold
153-
})
156+
contentType: 'application/json',
157+
data: angular.toJson(data)
154158
});
155159
}
156160
};
157161

158-
return({
159-
trace: function(message, desc) {
162+
return ({
163+
trace: function (message, desc) {
160164
log('trace', message, desc);
161165
},
162-
debug: function(message, desc) {
166+
debug: function (message, desc) {
163167
log('debug', message, desc);
164168
},
165-
info: function(message, desc) {
169+
info: function (message, desc) {
166170
log('info', message, desc);
167171
},
168-
warn: function(message, desc) {
172+
warn: function (message, desc) {
169173
log('warn', message, desc);
170174
},
171-
error: function(message, desc) {
175+
error: function (message, desc) {
172176
log('error', message, desc);
173177
},
174-
setLoggingThreshold: function(level) {
178+
setLoggingThreshold: function (level) {
175179
/*
176180
* Normally the logger would use the logging threshold passed in on the config hash but an
177181
* application may want to override this dynamically, e.g. to enable a different logging
@@ -182,18 +186,18 @@ loggingModule.factory(
182186
overrideLoggingThreshold = true;
183187
}
184188
},
185-
setDefaultData: function(data) {
189+
setDefaultData: function (data) {
186190
defaultData = data;
187191
}
188192
});
189193
}]
190194
);
191195

192196
loggingModule.factory(
193-
"userErrorReport",
194-
['$window','$rootScope','LOGGING_CONFIG',function($window,$rootScope,LOGGING_CONFIG) {
195-
return({
196-
send: function(userMessage,error) {
197+
'userErrorReport',
198+
['$window', '$rootScope', 'LOGGING_CONFIG', function ($window, $rootScope, LOGGING_CONFIG) {
199+
return ({
200+
send: function (userMessage, error) {
197201
var payload = {
198202
url: $window.location.href,
199203
systemError: error,
@@ -204,9 +208,9 @@ loggingModule.factory(
204208
// check if the config says we should log to the remote, and also if a remote endpoint was specified
205209
if (LOGGING_CONFIG.LOGGING_TYPE === 'remote' && LOGGING_CONFIG.REMOTE_ERROR_REPORT_ENDPOINT) {
206210
$.ajax({
207-
type: "POST",
211+
type: 'POST',
208212
url: LOGGING_CONFIG.REMOTE_ERROR_REPORT_ENDPOINT,
209-
contentType: "application/json",
213+
contentType: 'application/json',
210214
data: angular.toJson(payload)
211215
});
212216
}

0 commit comments

Comments
 (0)