-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
78 lines (76 loc) · 2.23 KB
/
Copy pathlogger.js
File metadata and controls
78 lines (76 loc) · 2.23 KB
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
// Write your package code here!
var BaseLogger;
(function(){
BaseLogger = function(level) {
if(Object.defineProperty)
// Follows log4j levels, can use numbers instead as well
Object.defineProperty(this, "logLevel", {
get: function() {
return this.levels[this._logLevel];
},
set: function(level) {
this._logLevel = this.levels.indexOf(level);
}
});
else
this.logLevel = function(newLevel){ this._logLevel = this.levels.indexOf(level)}
if(level && this.levels.indexOf(level) >= 0)
this.logLevel = level;
else
this.logLevel = 'info';
};
BaseLogger.prototype = {
levels:[
'off',
'fatal',
'error',
'warn',
'info',
'debug',
'trace'
],
_curLevel: null,
log: function(){
var curLevel = this._curLevel;
for(var i = 0; i < arguments.length; i++){
if(console.error && curLevel && curLevel <= 2) console.error(arguments[i]);
else if(console.warn && curLevel && curLevel === 3) console.warn(arguments[i]);
else if(console.info && curLevel && curLevel === 4) console.info(arguments[i]);
else if(console.trace && curLevel && curLevel === 6) console.trace(arguments[i]);
else if(console.log) console.log(arguments[i]);
}
this._curLevel = null;
},
_log: function(level, args){
this._curLevel = level;
if(level <= this._logLevel && console)
this.log.apply(this, args);
},
fatal: function(){
this._log(1, arguments);
},
error: function(){
this._log( 2, arguments);
},
warn: function(){
this._log( 3, arguments);
},
info: function(){
this._log( 4, arguments);
},
debug: function(){
this._log( 5, arguments);
},
trace: function(){
this._log( 6, arguments);
}
};
/*
* Initializes it with all console functions that someone may need to call,
* though will not fail if bind method does not exist (but will not have those funcitons either)
*/
if(Function.prototype.bind)
for(key in console)
if(typeof console[key] === 'function' && !BaseLogger.hasOwnProperty(key))
BaseLogger[key] = console[key].bind(console);
})();