-
-
Notifications
You must be signed in to change notification settings - Fork 905
/
logger.js
executable file
·85 lines (74 loc) · 2.25 KB
/
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
var colors = require('colors'),
U = require('./utils');
exports.TRACE = 4;
exports.DEBUG = 3;
exports.INFO = 2;
exports.WARN = 1;
exports.ERROR = 0;
exports.NONE = -1;
exports.logLevel = exports.TRACE;
exports.stripColors = false;
exports.showTimestamp = false;
exports.trace = function(msg) {
if (exports.logLevel >= exports.TRACE) { printMessage(msg, 'trace', 'grey'); }
};
exports.debug = function(msg) {
if (exports.logLevel >= exports.DEBUG) { printMessage(msg, 'debug', 'cyan'); }
};
exports.info = function(msg) {
if (exports.logLevel >= exports.INFO) { printMessage(msg, 'info', 'white'); }
};
exports.warn = function(msg) {
if (exports.logLevel >= exports.WARN) { printMessage(msg, 'warn', 'yellow'); }
};
exports.error = function(msg) {
if (exports.logLevel >= exports.ERROR) { printMessage(msg, 'error', 'red'); }
};
// Private functions and members
var levels = ['info', 'debug', 'error', 'warn', 'trace'];
var has = function(array, item) {
var len = array.length;
for (var i = 0; i < len; i++) {
if (array[i] === item) {
return true;
}
}
return false;
};
var isArray = function(object) {
return Object.prototype.toString.call(object) === '[object Array]';
};
var formattedDate = function() {
var date = new Date();
var pad = function(val) {
val += '';
return val.length < 2 ? '0' + val : val;
};
return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate()) + ' ' +
pad(date.getHours()) + ':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds());
};
var printMessage = function(msg, level, color) {
// Validate arguments
msg = msg || '';
level = level || 'info';
level = has(levels, level) ? level : 'info';
color = color || 'white';
// Have to wrap in a function to avoid "Illegal invocation" error on android
var logFunc = function(msg) {
(level === 'debug' || level === 'trace' ? console.log : console[level])(msg);
};
function printLine(line) {
if (isArray(line)) {
for (var i = 0; i < line.length; i++) {
printLine(line[i]);
}
} else {
var tag = (exports.showTimestamp ? formattedDate() + ' -- ' : '') + '[' +
level.toUpperCase() + '] ';
var str = tag.grey + (line || '')[color];
if (exports.stripColors) { str = U.stripColors(str); }
logFunc(str);
}
}
printLine(msg);
};