This repository has been archived by the owner on Jul 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
axe.js
162 lines (140 loc) · 4.29 KB
/
axe.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
(function(root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define([], function() {
return factory(window.console);
});
} else if (typeof exports === 'object') {
module.exports = factory(console);
} else {
root.axe = factory(window.console);
}
}(this, function(console) {
'use strict';
var DEBUG = 0;
var INFO = 1;
var WARN = 2;
var ERROR = 3;
/**
* Default console log appender
* @constructor
*/
var ConsoleAppender = function() {};
ConsoleAppender.prototype.log = function(level, date, component, message) {
var logFunction;
if (level === DEBUG) {
logFunction = console.log.bind(console);
} else if (level === INFO) {
logFunction = console.info.bind(console);
} else if (level === WARN) {
logFunction = console.warn.bind(console);
} else if (level === ERROR) {
logFunction = console.error.bind(console);
}
if (component) {
logFunction('[' + date.toISOString() + '][' + component + ']', message);
} else {
logFunction('[' + date.toISOString() + '] ' + message);
}
};
/**
* Lightweight logger. Logging goes like this:
* var axe = require('axe');
* axe.debug([TAG], message);
* axe.info([TAG], message);
* axe.warn([TAG], message);
* axe.error([TAG], message);
*
* @constructor
*/
var Axe = function() {
/**
* Default console log appender
* @type {ConsoleAppender}
*/
this.defaultAppender = new ConsoleAppender();
/**
* Gathers all the appenders
* @type {Array}
*/
this.appenders = [this.defaultAppender];
/**
* Configures the log leve, ignores anything below this log level. Default: this.DEBUG
* @type {Number}
*/
this.logLevel = 0; // default to debug
/**
* Maximum number of log entries. Default: 10000
* @type {Number}
*/
this.maxEntries = 10000;
var levels = ['debug', 'info', 'warn', 'error'];
for (var i = 0; i < levels.length; i++) {
this[levels[i]] = this.log.bind(this, i);
}
/**
* Holds all the log entries
* @type {Array}
*/
this.logs = [];
this.DEBUG = DEBUG;
this.INFO = INFO;
this.WARN = WARN;
this.ERROR = ERROR;
};
/**
* Add an appender, no-op if already present.
* Must implemement the following method: appender.log(level [Number], date [Date], component [String], message [String/Error])
*
* @param {Object} appender The appender
*/
Axe.prototype.addAppender = function(appender) {
if (this.appenders.indexOf(appender) !== -1) {
return;
}
this.appenders.push(appender);
};
/**
* Removes an appender, no-op if not present.
* @param {Object} appender The appender
*/
Axe.prototype.removeAppender = function(appender) {
var index = this.appenders.indexOf(appender);
if (index !== -1) {
this.appenders.splice(index, 1);
}
};
/**
* Log something to the appenders
* @param {Number} level The log level
* @param {[type]} component [description]
* @param {[type]} message [description]
* @return {[type]} [description]
*/
Axe.prototype.log = function(level, component, message) {
if (level < this.logLevel) {
return;
}
if (component && !message) {
message = component;
component = undefined;
}
var log = [level, new Date(), component, message];
this.logs.push(log);
this.logs = this.logs.slice(-this.maxEntries);
this.appenders.forEach(function(appender) {
appender.log.apply(appender, log);
});
};
/**
* Dump the logger's entire logs (up to the max number of entries) to an appender.
*
* @param {Object} appender The appender
*/
Axe.prototype.dump = function(appender) {
this.logs.forEach(function(log) {
appender.log.apply(appender, log);
});
};
return new Axe();
}));