-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.js
63 lines (56 loc) · 1.68 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
var fs = require("fs");
var path = require("path");
var util = require("util");
module.exports = function Logger(options) {
options = options || {};
this.verbosity = options.verbosity || 0;
this.inspect = options.inspect || false;
this.file = options.file || null;
this.colors = options.colors || {
gray: "\x1b[0m",
red: "\x1b[31;1m",
blue: "\x1b[34;1m",
cyan: "\x1b[36;1m",
white: "\x1b[37;1m",
green: "\x1b[32;1m",
yellow: "\x1b[33;1m",
magenta: "\x1b[35;1m"
};
if(fs.existsSync(this.file))
fs.unlink(path.resolve(this.file));
return function _logger(msg, verbosity, color) {
verbosity = verbosity || 0;
color = color || "white";
var tag;
switch(color) {
case "red":
tag = " error ";
break;
case "green":
tag = " success ";
break;
case "yellow":
tag = " warning ";
break;
default:
tag = " info ";
break;
}
if(msg && this.verbosity > verbosity) {
msg = [
'[',
this.colors[color],
tag,
this.colors["gray"],
']',
' ',
this.colors[color],
(this.inspect && typeof msg === "object" ? util.inspect(msg) : msg),
this.colors["gray"]
].join('');
console.log(msg);
if(this.file)
fs.appendFile(this.file, msg.replace(/\x1b\[\d+(;\d)?m/g, '') + '\r\n');
}
}.bind(this);
}