File size: 2,503 Bytes
311cc15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const LogLevel = {
	Debug: {
		name: "Debug",
		color: [ 38, 5, 13 ],
		error: false,
		production: false,
		textColor: null
	},
	Info: {
		name: "Info",
		color: null,
		error: false,
		production: true,
		textColor: null
	},
	Success: {
		name: "Success",
		color: [ 38, 5, 10 ],
		error: false,
		production: true,
		textColor: null
	},
	Warning: {
		name: "Warning",
		color: [ 38, 5, 11 ],
		error: true,
		production: true,
		textColor: [ 38, 5, 11 ]
	},
	Error: {
		name: "Error",
		color: [ 38, 5, 9 ],
		error: true,
		production: true,
		textColor: [ 38, 5, 9 ]
	},
	Fatal: {
		name: "Fatal",
		color: [ 38, 5, 0, 48, 5, 9 ],
		error: true,
		production: true,
		textColor: [ 38, 5, 9 ]
	}
};

const LogLevel_ = Object.keys(LogLevel).reduce((r, v) => { r[v] = v; return r; }, {});

function color(colors, text) {
	if (colors == null) return text;
	if (typeof colors != "object") colors = [ 38, 5, colors ];
	return `\x1b[${colors.join(";")}m${text}\x1b[0m`;
}

function bracket(text) {
	return `${color(7, "[")}${text}${color(7, "]")}`;
}

function Logger(production, prefix) {
	if (production != null && arguments.length === 1 && production.data != null && typeof production.data === "object") {
		production = production.data;
	}

	if (production != null && typeof production === "object" && arguments.length === 1 && typeof production.production === "boolean") {
		({ prefix, production } = production);
	}

	if (typeof production !== "boolean") {
		throw new TypeError("production must be of type boolean");
	}

	if (prefix != null && typeof prefix !== "string") {
		throw new TypeError("prefix must be of type string");
	}

	if (prefix == null || prefix.length == 0) prefix = null;

	const log = function(level, ...messages) {
		if (typeof level !== "string" || !Object.keys(LogLevel).includes(level)) {
			throw new TypeError("type must be a LogLevel");
		}
		const logType = LogLevel[level];
		if (production && !logType.production) return;
		const prefixText = prefix != null ? bracket(prefix) + " " : "";
		const typeText = bracket(color(logType.color, logType.name.toUpperCase()));
		for (const i in messages) {
			if (!messages.hasOwnProperty(i)) continue;
			const output = `${prefixText}${typeText} `;
			process[logType.error ? "stderr" : "stdout"].write(output);
			console[logType.error ? "error" : "log"](messages[i]);
		}
	};

	log.data = Object.freeze({ production, prefix });

	return Object.freeze(log);
}

module.exports = { Logger, LogLevel: Object.freeze(LogLevel_) };