Projekt

Obecné

Profil

Stáhnout (1.34 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
var logLevel = "info";
2
3
function dummy() {}
4
5
function shouldLog(level) {
6
	var shouldLog =
7
		(logLevel === "info" && level === "info") ||
8
		(["info", "warning"].indexOf(logLevel) >= 0 && level === "warning") ||
9
		(["info", "warning", "error"].indexOf(logLevel) >= 0 && level === "error");
10
	return shouldLog;
11
}
12
13
function logGroup(logFn) {
14
	return function(level, msg) {
15
		if (shouldLog(level)) {
16
			logFn(msg);
17
		}
18
	};
19
}
20
21
module.exports = function(level, msg) {
22
	if (shouldLog(level)) {
23
		if (level === "info") {
24
			console.log(msg);
25
		} else if (level === "warning") {
26
			console.warn(msg);
27
		} else if (level === "error") {
28
			console.error(msg);
29
		}
30
	}
31
};
32
33
/* eslint-disable node/no-unsupported-features/node-builtins */
34
var group = console.group || dummy;
35
var groupCollapsed = console.groupCollapsed || dummy;
36
var groupEnd = console.groupEnd || dummy;
37
/* eslint-enable node/no-unsupported-features/node-builtins */
38
39
module.exports.group = logGroup(group);
40
41
module.exports.groupCollapsed = logGroup(groupCollapsed);
42
43
module.exports.groupEnd = logGroup(groupEnd);
44
45
module.exports.setLogLevel = function(level) {
46
	logLevel = level;
47
};
48
49
module.exports.formatError = function(err) {
50
	var message = err.message;
51
	var stack = err.stack;
52
	if (!stack) {
53
		return message;
54
	} else if (stack.indexOf(message) < 0) {
55
		return message + "\n" + stack;
56
	} else {
57
		return stack;
58
	}
59
};