1
1
'use strict' ;
2
2
3
3
/**
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.
10
12
*/
11
13
var loggingModule = angular . module ( 'talis.services.logging' , [ ] ) ;
12
14
13
15
/**
14
16
* 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
16
18
* methods in the global context is not the 'angular way'
17
19
*/
18
20
loggingModule . factory (
@@ -25,8 +27,7 @@ loggingModule.factory(
25
27
) ;
26
28
27
29
/**
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
30
31
*/
31
32
loggingModule . provider (
32
33
"$exceptionHandler" , {
@@ -37,45 +38,44 @@ loggingModule.provider(
37
38
) ;
38
39
39
40
/**
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.
43
44
*/
44
45
loggingModule . factory (
45
46
"exceptionLoggingService" ,
46
47
[ "$log" , "$window" , "stacktraceService" , "LOGGING_CONFIG" , function ( $log , $window , stacktraceService , LOGGING_CONFIG ) {
47
48
function error ( exception , cause ) {
48
-
49
49
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 ) ;
52
52
}
53
53
54
54
// check if the config says we should log to the remote, and also if a remote endpoint was specified
55
55
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
+ }
79
79
}
80
80
}
81
81
return ( error ) ;
@@ -90,50 +90,70 @@ loggingModule.factory(
90
90
loggingModule . factory (
91
91
"applicationLoggingService" ,
92
92
[ "$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' ] ;
99
94
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' ;
114
120
}
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 ) ;
115
145
} ,
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 ) ;
137
157
}
138
158
} ) ;
139
159
} ]
@@ -153,12 +173,12 @@ loggingModule.factory(
153
173
154
174
// check if the config says we should log to the remote, and also if a remote endpoint was specified
155
175
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
+ } ) ;
162
182
}
163
183
}
164
184
} ) ;
0 commit comments