forked from practicalmeteor/meteor-mocha-console-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloglevel.js
306 lines (253 loc) · 9.06 KB
/
loglevel.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*! loglevel - v1.2.0 - https://github.com/pimterry/loglevel - (c) 2014 Tim Perry - licensed MIT */
Loglevel = function (options) {
var self = {};
if(options && options.prefix) {
self.prefix = options.prefix;
} else {
self.prefix = '';
}
if(options && options.level) {
self.level = options.level;
} else {
self.level = 'info';
}
var noop = function() {};
var undefinedType = "undefined";
function realMethod(methodName) {
if (typeof console === undefinedType) {
return false; // We can't build a real method without a console to log to
} else if (console[methodName] !== undefined) {
return bindMethod(console, methodName);
} else if (console.log !== undefined) {
return bindMethod(console, 'log');
} else {
return noop;
}
}
function bindMethod(obj, methodName) {
var method = obj[methodName];
if (typeof method.bind === 'function') {
return method.bind(obj, self.prefix);
} else {
try {
return Function.prototype.bind.call(method, obj, self.prefix);
} catch (e) {
// Missing bind shim or IE8 + Modernizr, fallback to wrapping
return function() {
return Function.prototype.apply.apply(method, [obj, arguments]);
};
}
}
}
function enableLoggingWhenConsoleArrives(methodName, level) {
return function () {
if (typeof console !== undefinedType) {
replaceLoggingMethods(level);
self[methodName].apply(self, arguments);
}
};
}
var logMethods = [
"trace",
"fine",
"debug",
"info",
"warn",
"error"
];
function replaceLoggingMethods(level) {
for (var i = 0; i < logMethods.length; i++) {
var methodName = logMethods[i];
self[methodName] = (i < level) ? noop : self.methodFactory(methodName, level);
}
}
function persistLevelIfPossible(levelNum) {
var levelName = (logMethods[levelNum] || 'silent').toUpperCase();
// Use localStorage if available
try {
window.localStorage['loglevel'] = levelName;
return;
} catch (ignore) {}
// Use session cookie as fallback
try {
window.document.cookie = "loglevel=" + levelName + ";";
} catch (ignore) {}
}
function loadPersistedLevel() {
var storedLevel;
try {
storedLevel = window.localStorage['loglevel'];
} catch (ignore) {}
if (typeof storedLevel === undefinedType) {
try {
storedLevel = /loglevel=([^;]+)/.exec(window.document.cookie)[1];
} catch (ignore) {}
}
if (self.levels[storedLevel] === undefined) {
storedLevel = "WARN";
}
self.setLevel(self.levels[storedLevel]);
}
/*
*
* Public API
*
*/
self.levels = { "TRACE": 0, "FINE": 1, "DEBUG": 2, "INFO": 3, "WARN": 4,
"ERROR": 5, "SILENT": 6};
self.methodFactory = function (methodName, level) {
return realMethod(methodName) ||
enableLoggingWhenConsoleArrives(methodName, level);
};
self.setLevel = function (level) {
if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) {
level = self.levels[level.toUpperCase()];
}
if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) {
//persistLevelIfPossible(level);
self.level = level;
replaceLoggingMethods(level);
if (typeof console === undefinedType && level < self.levels.SILENT) {
return "No console available for logging";
}
} else {
throw "log.setLevel() called with invalid level: " + level;
}
};
self.enableAll = function() {
self.setLevel(self.levels.TRACE);
};
self.disableAll = function() {
self.setLevel(self.levels.SILENT);
};
// Grab the current global log variable in case of overwrite
var _log = (typeof window !== undefinedType) ? window.log : undefined;
self.noConflict = function() {
if (typeof window !== undefinedType &&
window.log === self) {
window.log = _log;
}
return self;
};
self.setPrefix = function(prefix) {
if(typeof prefix === undefinedType || prefix === null) {
prefix = '';
}
self.prefix = prefix;
self.setLevel(self.level);
};
//loadPersistedLevel();
self.setLevel(self.level);
return self;
};
log = Loglevel({prefix: 'practicalmeteor:loglevel:'});
export { log };
if (this.practical == null) { this.practical = {}; }
(function() {
let instance = undefined;
const Cls = (practical.LoggerFactory = class LoggerFactory {
static initClass() {
instance = null;
}
static get() {
return instance != null ? instance : (instance = new practical.LoggerFactory());
}
// The 'global' namespace is checked first, in order to allow people to enforce
// a loglevel across the board.
_getSettingsLoglevel(namespace, defaultLevel){
let level;
if (namespace == null) { namespace = ''; }
if (defaultLevel == null) { defaultLevel = 'info'; }
const globalLevel = this._getNamespaceLoglevel('global');
if (globalLevel != null) { return globalLevel; }
if (namespace.length > 0) { level = this._getNamespaceLoglevel(namespace); }
if (level == null) { level = this._getNamespaceLoglevel('default'); }
return level != null ? level : (level = defaultLevel);
}
// @returns Meteor.settings.loglevel.namespace server side
// or if called client side or it doesn't exist server side,
// Meteor.settings.public.loglevel.namespace.
// This allows to set only public loglevel for both client and server side.
_getNamespaceLoglevel(namespace){
let level = __guard__(__guard__(Meteor.settings != null ? Meteor.settings.public : undefined, x1 => x1.loglevel), x => x[namespace]);
if (Meteor.isServer) {
const serverLevel = __guard__(Meteor.settings != null ? Meteor.settings.loglevel : undefined, x2 => x2[namespace]);
if (serverLevel != null) { level = serverLevel; }
}
return level;
}
createLogger(namespace, defaultLevel){
if (namespace == null) { namespace = ''; }
if (defaultLevel == null) { defaultLevel = 'info'; }
const options = {};
if (namespace.length > 0) { options.prefix = namespace + ':'; }
options.level = this._getSettingsLoglevel(namespace, defaultLevel);
const log = Loglevel(options)
return log;
}
createPackageLogger(packageName, defaultLevel){
if (defaultLevel == null) { defaultLevel = 'info'; }
return this.createLogger(packageName, defaultLevel);
}
createAppLogger(appName, defaultLevel){
if (appName == null) { appName = 'app'; }
if (defaultLevel == null) { defaultLevel = 'info'; }
return this.createLogger(appName, defaultLevel);
}
});
Cls.initClass();
return Cls;
})();
const LoggerFactory = practical.LoggerFactory.get();
function __guard__(value, transform) {
return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined;
}
export class ObjectLogger {
constructor(className, defaultLevel){
this.className = className;
if (defaultLevel == null) { defaultLevel = 'info'; }
this.defaultLevel = defaultLevel;
this.log = loglevel.createLogger(this.className, this.defaultLevel);
this.callStack = [];
this.log.enter = this.bindMethod(this.enter, 'debug');
this.log.fineEnter = this.bindMethod(this.enter, 'fine');
this.log.return = this.bindMethod(this.return, 'debug');
this.log.fineReturn = this.bindMethod(this.return, 'fine');
return this.log;
}
enter(level, ...args){
if (args.length === 0) { throw new Error(('ObjectLogger: No method name provided to enter')); }
const methodName = args.shift();
this.callStack.unshift(methodName);
this.log.setPrefix(`${this.className}.${methodName}:`);
args.unshift('ENTER');
return this.log[level].apply(this.log, args);
}
return(level){
this.log[level].call(this.log, 'RETURN');
this.callStack.shift();
if (this.callStack.length > 0) {
const methodName = this.callStack[0];
return this.log.setPrefix(`${this.className}.${methodName}:`);
}
}
bindMethod(method, level) {
if (typeof method.bind === 'function') {
return method.bind(this, level);
} else {
try {
return Function.prototype.bind.call(method, this, level);
} catch (e) {
// Missing bind shim or IE8 + Modernizr, fallback to wrapping
return (...args)=> {
args.unshift(level);
return Function.prototype.apply.apply(method, [
this,
args
]);
};
}
}
}
}
export default ObjectLogger;