1
|
|
|
var term = require('terminal-kit').terminal; |
2
|
|
|
var moment = require('moment'); |
3
|
|
|
var stringz = require('stringz'); // for emoji support ❤️ |
4
|
|
|
|
5
|
|
|
// keep the native pipe to stdout & stderr |
6
|
|
|
module.exports.nativeLog = global.console.log; |
7
|
|
|
module.exports.nativeError = global.console.error; |
8
|
|
|
|
9
|
|
|
// enables or disables certain types of logging |
10
|
|
|
var loggerTypes = {} |
11
|
|
|
module.exports.enable = type => { |
12
|
|
|
module.exports.options.styles[type] = module.exports.options.styles[type] || [term, module.exports.nativeLog]; |
13
|
|
|
loggerTypes[type] = true; |
14
|
|
|
}; |
15
|
|
|
module.exports.disable = type => loggerTypes[type] = false; |
16
|
|
|
module.exports.isEnabled = type => loggerTypes[type]; |
17
|
|
|
|
18
|
|
|
module.exports.options = { |
19
|
|
|
typePadding: ' ', // dictates the width of the type prefix |
20
|
|
|
styles: { // contains term styles for the various prefixes |
21
|
|
|
error: [term.error.bgRed.white, module.exports.nativeError], |
22
|
|
|
warn: [term.error.bgYellow.white, module.exports.nativeError], |
23
|
|
|
info: [term, module.exports.nativeLog], |
24
|
|
|
debug: [term, module.exports.nativeLog], |
25
|
|
|
success: [term.bgGreen.white, module.exports.nativeLog] |
26
|
|
|
}, |
27
|
|
|
// a function that takes a date and returns a string |
28
|
|
|
// used to print the date in the prefix |
29
|
|
|
dateFormatter: date => moment(date).format("D/M/YY HH:mm:ss.SSS") |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
var getLogTypePrefix = type => ` [${type}] ${module.exports.options.typePadding.substring(stringz.length(type) + 4)}`; |
33
|
|
|
var getPrefix = type => getLogTypePrefix(type) + module.exports.options.dateFormatter(new Date()) + " "; |
34
|
|
|
module.exports.printPrefix = (type, t = term) => {t(getPrefix(type));t.styleReset("| ")}; |
35
|
|
|
|
36
|
|
|
module.exports.makeSimpleLogger = type => { |
37
|
|
|
module.exports.enable(type); |
38
|
|
|
var TYPE = type == "success" ? "OK" : type.toUpperCase(); |
39
|
|
|
global.console[type] = function() { |
40
|
|
|
if (loggerTypes[type]) { |
41
|
|
|
module.exports.printPrefix(TYPE, module.exports.options.styles[type][0]); |
42
|
|
|
module.exports.options.styles[type][1].apply(this, arguments); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
module.exports.makeCustomLogger = (type, myfunction) => { |
48
|
|
|
module.exports.enable(type); |
49
|
|
|
global.console[type] = function() { |
50
|
|
|
if (loggerTypes[type]) { |
51
|
|
|
myfunction.apply(this, arguments); |
52
|
|
|
} |
53
|
|
|
}; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
module.exports.makeSimpleLogger("debug"); |
57
|
|
|
module.exports.makeSimpleLogger("info"); |
58
|
|
|
global.console.log = global.console.info; |
59
|
|
|
module.exports.makeSimpleLogger("warn"); |
60
|
|
|
module.exports.makeSimpleLogger("success"); |
61
|
|
|
|
62
|
|
|
module.exports.makeCustomLogger("error", function() { |
63
|
|
|
var isTrace = typeof arguments[0] == "string" && arguments[0].substring(0, 5) == "Trace"; |
64
|
|
|
var type = isTrace ? "TRACE" : "ERROR"; |
65
|
|
|
module.exports.printPrefix(type, module.exports.options.styles.error[0]); |
66
|
|
|
if (isTrace) { |
67
|
|
|
arguments[0] = arguments[0].substring(7); |
68
|
|
|
} |
69
|
|
|
module.exports.options.styles.error[1].apply(this, arguments); |
70
|
|
|
}) |
71
|
|
|
|