forked from ajbrown/angular-loggly-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-loggly-logger.js
190 lines (142 loc) · 5.22 KB
/
angular-loggly-logger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* ngLoggly is a module which will send your log messages to a configured
* [Loggly](http://loggly.com) connector.
*
* Major credit should go to Thomas Burleson, who's highly informative blog
* post on [Enhancing AngularJs Logging using Decorators](http://bit.ly/1pOI0bb)
* provided the foundation (if not the majority of the brainpower) for this
* module.
*/
; (function( angular ) {
"use strict";
angular.module( 'ngLoggly.logger', [] )
.provider( 'LogglyLogger', function() {
var self = this;
var logSuccessHandler;
var logFailureHandler;
var https = true;
var extra = {};
var includeCurrentUrl = false;
var includeTimestamp = false;
var token = null;
var endpoint = '://logs-01.loggly.com/inputs/';
var buildUrl = function ( data ) {
var msg = encodeURIComponent( angular.toJson( data ) );
return (https ? 'https' : 'http') + endpoint + token + '.gif?PLAINTEXT=' + msg;
};
this.setExtra = function (d) {
extra = d;
return self;
};
this.inputToken = function ( s ) {
if (angular.isDefined(s)) {
token = s;
return self;
}
return token;
};
this.useHttps = function (flag) {
if (angular.isDefined(flag)) {
https = !!flag;
return self;
}
return https;
};
this.includeUrl = function (flag) {
if (angular.isDefined(flag)) {
includeCurrentUrl = !!flag;
return self;
}
return includeCurrentUrl;
};
this.includeTimestamp = function (flag) {
if (angular.isDefined(flag)) {
includeTimestamp = !!flag;
return self;
}
return includeTimestamp;
};
this.$get = [ '$injector', function ($injector) {
var lastLog = null;
/**
* Send the specified data to loggly as a json message.
* @param data
*/
var sendMessage = function (data) {
//If a token is not configured, don't do anything.
if (!token) {
return;
}
//TODO we're injecting this here to resolve circular dependency issues. Is this safe?
var $location = $injector.get( '$location' );
lastLog = new Date();
var sentData = angular.extend(extra, data, {}),
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
if (includeCurrentUrl) {
sentData.url = $location.absUrl()
}
if( includeTimestamp ) {
sentData.timestamp = lastLog.toISOString();
}
//Loggly's API doesn't send us cross-domain headers, so we can't interact directly
new Image().src = buildUrl(sentData);
};
var attach = function() {
};
return {
lastLog: function(){ return lastLog },
attach: attach,
sendMessage: sendMessage
}
}];
} );
angular.module( 'ngLoggly', ['ngLoggly.logger'] )
.config( [ '$provide', function( $provide ) {
$provide.decorator('$log', [ "$delegate", '$injector', function ( $delegate, $injector ) {
var wrapLogFunction = function(logFn, level, loggerName) {
var logger = $injector.get('LogglyLogger');
var wrappedFn = function () {
var args = Array.prototype.slice.call(arguments);
logFn.apply(null, args);
var msg = args.length == 1 ? args[0] : args;
var sending = { level: level, message: msg };
if( loggerName ) {
sending.logger = msg
}
//Send the message to through the loggly sender
logger.sendMessage( sending );
};
wrappedFn.logs = [];
return wrappedFn;
};
var _$log = (function ($delegate) {
return {
log: $delegate.log,
info: $delegate.info,
warn: $delegate.warn,
error: $delegate.error
};
})($delegate);
var getLogger = function ( name ) {
return {
log: wrapLogFunction( _$log.log, 'INFO', name ),
info: wrapLogFunction( _$log.info, 'INFO', name ),
warn: wrapLogFunction( _$log.warn, 'WARN', name ),
debug: wrapLogFunction( _$log.debug, 'DEBUG', name ),
error: wrapLogFunction( _$log.error, 'ERROR', name )
}
};
//wrap the existing API
$delegate.log = wrapLogFunction($delegate.log, 'INFO');
$delegate.info = wrapLogFunction($delegate.info, 'INFO');
$delegate.warn = wrapLogFunction($delegate.warn, 'WARN');
$delegate.debug = wrapLogFunction($delegate.debug, 'DEBUG');
$delegate.error = wrapLogFunction($delegate.error, 'ERROR');
//Add some methods
$delegate.getLogger = getLogger;
return $delegate;
}]);
}]);
})(window.angular);