forked from nmccready/angular-simple-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-simple-logger.light.js
147 lines (136 loc) · 4.1 KB
/
angular-simple-logger.light.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
/**
* angular-simple-logger
*
* @version: 0.1.7
* @author: Nicholas McCready
* @date: Tue Jan 26 2016 10:15:01 GMT-0500 (EST)
* @license: MIT
*/
(function (window, angular){
angular.module('nemLogging', []);
angular.module('nemLogging').provider('nemDebug', function (){
var ourDebug = null;
this.$get = function(){
//avail as service
return ourDebug;
};
//avail at provider, config time
this.debug = ourDebug;
return this;
});
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
slice = [].slice;
angular.module('nemLogging').provider('nemSimpleLogger', [
'nemDebugProvider', function(nemDebugProvider) {
var LEVELS, Logger, _debugCache, _fns, _isValidLogObject, _maybeExecLevel, _wrapDebug, i, key, len, nemDebug, val;
nemDebug = nemDebugProvider.debug;
_debugCache = {};
_fns = ['debug', 'info', 'warn', 'error', 'log'];
LEVELS = {};
for (key = i = 0, len = _fns.length; i < len; key = ++i) {
val = _fns[key];
LEVELS[val] = key;
}
_maybeExecLevel = function(level, current, fn) {
if (level >= current) {
return fn();
}
};
_isValidLogObject = function(logObject) {
var isValid, j, len1;
isValid = false;
if (!logObject) {
return isValid;
}
for (j = 0, len1 = _fns.length; j < len1; j++) {
val = _fns[j];
isValid = (logObject[val] != null) && typeof logObject[val] === 'function';
if (!isValid) {
break;
}
}
return isValid;
};
/*
Overide logeObject.debug with a nemDebug instance
see: https://github.com/visionmedia/debug/blob/master/Readme.md
*/
_wrapDebug = function(namespace, logObject) {
var debugInstance, j, len1, newLogger;
if (_debugCache[namespace] == null) {
_debugCache[namespace] = nemDebug(namespace);
}
debugInstance = _debugCache[namespace];
newLogger = {};
for (j = 0, len1 = _fns.length; j < len1; j++) {
val = _fns[j];
newLogger[val] = val === 'debug' ? debugInstance : logObject[val];
}
return newLogger;
};
Logger = (function() {
function Logger($log1) {
var fn1, j, len1, level, logFns;
this.$log = $log1;
this.spawn = bind(this.spawn, this);
if (!this.$log) {
throw 'internalLogger undefined';
}
if (!_isValidLogObject(this.$log)) {
throw '@$log is invalid';
}
this.doLog = true;
logFns = {};
fn1 = (function(_this) {
return function(level) {
logFns[level] = function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
if (_this.doLog) {
return _maybeExecLevel(LEVELS[level], _this.currentLevel, function() {
var ref;
return (ref = _this.$log)[level].apply(ref, args);
});
}
};
return _this[level] = logFns[level];
};
})(this);
for (j = 0, len1 = _fns.length; j < len1; j++) {
level = _fns[j];
fn1(level);
}
this.LEVELS = LEVELS;
this.currentLevel = LEVELS.error;
}
Logger.prototype.spawn = function(newInternalLogger) {
if (typeof newInternalLogger === 'string') {
if (!_isValidLogObject(this.$log)) {
throw '@$log is invalid';
}
if (!nemDebug) {
throw 'nemDebug is undefined this is probably the light version of this library sep debug logggers is not supported!';
}
return _wrapDebug(newInternalLogger, this.$log);
}
return new Logger(newInternalLogger || this.$log);
};
return Logger;
})();
this.decorator = [
'$log', function($delegate) {
var log;
log = new Logger($delegate);
log.currentLevel = LEVELS.debug;
return log;
}
];
this.$get = [
'$log', function($log) {
return new Logger($log);
}
];
return this;
}
]);
})(window, angular);