Skip to content

Commit 11850e0

Browse files
committed
Merge pull request #5 from talis/SupportAllLoggingLevelsWithThreshold
90% - Support all logging levels with threshold
2 parents 896a9f3 + 778bfca commit 11850e0

File tree

4 files changed

+125
-92
lines changed

4 files changed

+125
-92
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## 2.0.0 (2016-02-19)
2+
3+
Features:
4+
5+
- Support all trace, debug, info, warn and error levels.
6+
- Use LOGGING_THRESHOLD instead of LOGGING_LEVEL in the config object for clarity, default is info level.
7+
8+
Bugfixes:
9+
10+
- Use the correct console.xxx version to get the correct icons in the browser console for the log level requested.
11+
12+
## 1.x.x
13+
14+
Initial version

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,24 @@ Exposes the following, all of which require the logging config object:
1111

1212
The config object to be passed to each service has the following properties:
1313

14+
- LOGGING_THRESHOLD
15+
- trace/debug/info/warn/error (default = info) - the threshold at which to create log entries
1416
- LOGGING_TYPE (can be local|remote|none)
1517
- local: log to local console only
1618
- remote: log to local console and remote endpoint
1719
- none: perform no logging
1820
- REMOTE_LOGGING_ENDPOINT: the full URL to the endpoint where log messages are sent via AJAX POST
1921
- REMOTE_ERROR_REPORT_ENDPOINT: the full URL to the endpoint where user error reports will be sent via AJAX POST
20-
- LOGGING_LEVEL (can be debug|error)
21-
- debug: log both debug and error messages
22-
- error: only log error messages (debug messages are not logged)
2322

2423
The config object should be declared as an AngularJS constant named LOGGING_CONFIG.
2524

2625
Example use of LOGGING_CONFIG:
2726

2827
```javascript
2928
// config object for application logger
30-
config.LOGGING_CONFIG = {
29+
config.LOGGING_CONFIG = {
30+
LOGGING_THRESHOLD: 'info',
3131
LOGGING_TYPE: 'local',
3232
REMOTE_LOGGING_ENDPOINT: config.TDC_ENDPOINT + "/clientlogger",
33-
REMOTE_ERROR_REPORT_ENDPOINT: config.TDC_ENDPOINT + "/usererrorreport",
34-
LOGGING_LEVEL: "debug" };
33+
REMOTE_ERROR_REPORT_ENDPOINT: config.TDC_ENDPOINT + "/usererrorreport"
3534
```

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": "1.1.0",
3+
"version": "2.0.0",
44
"main": "./js/logging.js",
55
"description": "Client side logging sent to the server",
66
"repository": {

js/logging.js

Lines changed: 105 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
'use strict';
22

33
/**
4-
* this module does a number of things:
5-
* - first it overrides the built in angular exception handling, so that any exceptions that would normally be logged
6-
* to the console, are POSTed via Ajax to a serverside service. It does the cross browser, by using stacktrace.js
7-
* - second it exposes an applicationLoggingService that can be injected into any angular module. This service exposes
8-
* and 'error' and 'debug' methods which if called will log to the console but also send those messages to a serverside
9-
* service.
4+
* This module does a number of things:
5+
*
6+
* - It overrides the built in Angular exception handling, so that any exceptions that would normally be logged
7+
* to the console, are POSTed via AJAX to a server-side service. It does the cross-browser, by using stacktrace.js
8+
*
9+
* - It exposes an applicationLoggingService that can be injected into any Angular module. This service exposes
10+
* 'trace', 'debug', 'info', 'warn' and 'error' methods which will log to the console but also send those messages
11+
* to a server-side service.
1012
*/
1113
var loggingModule = angular.module('talis.services.logging', []);
1214

1315
/**
1416
* Create a service that gives us a nice Angular-esque wrapper around the
15-
* stackTrace.js pintStackTrace() method. We use a service because calling
17+
* stackTrace.js printStackTrace() method. We use a service because calling
1618
* methods in the global context is not the 'angular way'
1719
*/
1820
loggingModule.factory(
@@ -25,8 +27,7 @@ loggingModule.factory(
2527
);
2628

2729
/**
28-
* Override Angular's built in exception handler, and tell it to use our new
29-
* exceptionLoggingService
30+
* Override Angular's built in exception handler, and tell it to use our new exceptionLoggingService
3031
*/
3132
loggingModule.provider(
3233
"$exceptionHandler",{
@@ -37,45 +38,44 @@ loggingModule.provider(
3738
);
3839

3940
/**
40-
* Exception Logging Service, currently only used by the $exceptionHandler
41-
* but logs to the console and uses the stacktraceService to generate a browser independent stacktrace
42-
* which is POSTed to server side clientlogging service.
41+
* Exception Logging Service, currently only used by the $exceptionHandler but logs to the console and uses the
42+
* stacktraceService to generate a browser independent stacktrace which is POSTed to server side clientlogging
43+
* service.
4344
*/
4445
loggingModule.factory(
4546
"exceptionLoggingService",
4647
["$log","$window", "stacktraceService", "LOGGING_CONFIG", function($log, $window, stacktraceService, LOGGING_CONFIG){
4748
function error( exception, cause){
48-
4949
if (LOGGING_CONFIG.LOGGING_TYPE !== 'none') {
50-
// preserve default behaviour i.e. dump to browser console
51-
$log.error.apply($log, arguments);
50+
// preserve default behaviour i.e. dump to browser console
51+
$log.error.apply($log, arguments);
5252
}
5353

5454
// check if the config says we should log to the remote, and also if a remote endpoint was specified
5555
if (LOGGING_CONFIG.LOGGING_TYPE === 'remote' && LOGGING_CONFIG.REMOTE_LOGGING_ENDPOINT) {
56-
// now log server side.
57-
try{
58-
var errorMessage = exception.toString();
59-
var stackTrace = stacktraceService.print({e: exception});
60-
61-
// use AJAX not an angular service because if something has gone wrong
62-
// angular might be fubar'd
63-
$.ajax({
64-
type: "POST",
65-
url: LOGGING_CONFIG.REMOTE_LOGGING_ENDPOINT,
66-
contentType: "application/json",
67-
data: angular.toJson({
68-
url: $window.location.href,
69-
message: errorMessage,
70-
type: "exception",
71-
stackTrace: stackTrace,
72-
cause: ( cause || "")
73-
})
74-
});
75-
} catch (loggingError){
76-
$log.warn("Error logging failed");
77-
$log.log(loggingError);
78-
}
56+
// now log server side.
57+
try {
58+
var errorMessage = exception.toString();
59+
var stackTrace = stacktraceService.print({e: exception});
60+
61+
// use AJAX not an angular service because if something has gone wrong
62+
// angular might be fubar'd
63+
$.ajax({
64+
type: "POST",
65+
url: LOGGING_CONFIG.REMOTE_LOGGING_ENDPOINT,
66+
contentType: "application/json",
67+
data: angular.toJson({
68+
url: $window.location.href,
69+
message: errorMessage,
70+
type: "exception",
71+
stackTrace: stackTrace,
72+
cause: ( cause || "")
73+
})
74+
});
75+
} catch (loggingError) {
76+
$log.warn("Error logging failed");
77+
$log.log(loggingError);
78+
}
7979
}
8080
}
8181
return( error );
@@ -90,50 +90,70 @@ loggingModule.factory(
9090
loggingModule.factory(
9191
"applicationLoggingService",
9292
["$log","$window", "LOGGING_CONFIG", function($log, $window, LOGGING_CONFIG){
93-
return({
94-
error: function(message, desc){
95-
if (LOGGING_CONFIG.LOGGING_TYPE !== 'none') {
96-
// preserve default behaviour
97-
$log.error.apply($log, [message]);
98-
}
93+
var arrLoggingLevels = ['trace', 'debug', 'info', 'warn', 'error'];
9994

100-
// check if the config says we should log to the remote, and also if a remote endpoint was specified
101-
if (LOGGING_CONFIG.LOGGING_TYPE === 'remote' && LOGGING_CONFIG.REMOTE_LOGGING_ENDPOINT) {
102-
// send server side
103-
$.ajax({
104-
type: "POST",
105-
url: LOGGING_CONFIG.REMOTE_LOGGING_ENDPOINT,
106-
contentType: "application/json",
107-
data: angular.toJson({
108-
url: $window.location.href,
109-
message: message,
110-
type: "error",
111-
desc: desc
112-
})
113-
});
95+
var loggingThreshold = LOGGING_CONFIG.LOGGING_THRESHOLD || 'info';
96+
97+
var iLoggingThreshold = arrLoggingLevels.indexOf(loggingThreshold);
98+
99+
var isLoggingEnabledForSeverity = function(severity) {
100+
var iRequestedLevel = arrLoggingLevels.indexOf(severity);
101+
if (iRequestedLevel === -1) {
102+
// Invalid level requested
103+
return false;
104+
}
105+
106+
return (iRequestedLevel >= iLoggingThreshold);
107+
}
108+
109+
var log = function(severity, message, desc) {
110+
if (!isLoggingEnabledForSeverity(severity)) {
111+
return;
112+
}
113+
114+
if (LOGGING_CONFIG.LOGGING_TYPE !== 'none') {
115+
// preserve default behaviour
116+
var angularLogSeverity = severity;
117+
if (angularLogSeverity === 'trace') {
118+
// Angular $log doesn't support trace so we use 'log' instead.
119+
angularLogSeverity = 'log';
114120
}
121+
122+
$log[angularLogSeverity].apply($log, [message]);
123+
}
124+
125+
// check if the config says we should log to the remote, and also if a remote endpoint was specified
126+
if (LOGGING_CONFIG.LOGGING_TYPE === 'remote' && LOGGING_CONFIG.REMOTE_LOGGING_ENDPOINT) {
127+
// send server side
128+
$.ajax({
129+
type: "POST",
130+
url: LOGGING_CONFIG.REMOTE_LOGGING_ENDPOINT,
131+
contentType: "application/json",
132+
data: angular.toJson({
133+
type: severity,
134+
url: $window.location.href,
135+
message: message,
136+
desc: desc
137+
})
138+
});
139+
}
140+
};
141+
142+
return({
143+
trace: function(message, desc) {
144+
log('trace', message, desc);
115145
},
116-
debug: function(message, desc){
117-
if (LOGGING_CONFIG.LOGGING_LEVEL !== 'error') {
118-
if (LOGGING_CONFIG.LOGGING_TYPE !== 'none') {
119-
$log.log.apply($log, [message]);
120-
}
121-
122-
// check if the config says we should log to the remote, and also if a remote endpoint was specified
123-
if (LOGGING_CONFIG.LOGGING_TYPE === 'remote' && LOGGING_CONFIG.REMOTE_LOGGING_ENDPOINT) {
124-
$.ajax({
125-
type: "POST",
126-
url: LOGGING_CONFIG.REMOTE_LOGGING_ENDPOINT,
127-
contentType: "application/json",
128-
data: angular.toJson({
129-
url: $window.location.href,
130-
message: message,
131-
type: "debug",
132-
desc: desc
133-
})
134-
});
135-
}
136-
}
146+
debug: function(message, desc) {
147+
log('debug', message, desc);
148+
},
149+
info: function(message, desc) {
150+
log('info', message, desc);
151+
},
152+
warn: function(message, desc) {
153+
log('warn', message, desc);
154+
},
155+
error: function(message, desc) {
156+
log('error', message, desc);
137157
}
138158
});
139159
}]
@@ -153,12 +173,12 @@ loggingModule.factory(
153173

154174
// check if the config says we should log to the remote, and also if a remote endpoint was specified
155175
if (LOGGING_CONFIG.LOGGING_TYPE === 'remote' && LOGGING_CONFIG.REMOTE_ERROR_REPORT_ENDPOINT) {
156-
$.ajax({
157-
type: "POST",
158-
url: LOGGING_CONFIG.REMOTE_ERROR_REPORT_ENDPOINT,
159-
contentType: "application/json",
160-
data: angular.toJson(payload)
161-
});
176+
$.ajax({
177+
type: "POST",
178+
url: LOGGING_CONFIG.REMOTE_ERROR_REPORT_ENDPOINT,
179+
contentType: "application/json",
180+
data: angular.toJson(payload)
181+
});
162182
}
163183
}
164184
});

0 commit comments

Comments
 (0)