-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole-logger.js
135 lines (123 loc) · 4.2 KB
/
console-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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* eslint-disable no-console */
import chalk from 'chalk';
import LogUtility from './log-utility.js';
/**
* @typedef ConsoleLoggerConfiguration
* @property {Boolean} enabled
* @property {String} severity
* @property {Boolean} timestamp
* @property {Boolean} local
* @property {Boolean} colors
*/
/**
* Outputs log messages to stdout (console).
*/
class ConsoleLogger {
constructor(config) {
Object.defineProperty(this, '_storedConfig', {
value: {
enabled: true,
severity: 'error',
colors: true,
timestamp: true,
local: true
},
writable: true,
enumerable: false
});
this.config = config;
}
/**
* @type {ConsoleLoggerConfiguration}
*/
get config() {
let effective = {};
if (typeof process.env.STASHKU_LOG_CONSOLE_ENABLED !== 'undefined') {
effective.enabled = !!process.env.STASHKU_LOG_CONSOLE_ENABLED;
}
if (typeof process.env.STASHKU_LOG_CONSOLE_SEVERITY !== 'undefined') {
effective.severity = process.env.STASHKU_LOG_CONSOLE_SEVERITY;
}
if (typeof process.env.STASHKU_LOG_CONSOLE_TIMESTAMP !== 'undefined') {
effective.timestamp = !!process.env.STASHKU_LOG_CONSOLE_TIMESTAMP;
}
if (typeof process.env.STASHKU_LOG_CONSOLE_LOCAL !== 'undefined') {
effective.local = !!process.env.STASHKU_LOG_CONSOLE_LOCAL;
}
if (typeof process.env.STASHKU_LOG_CONSOLE_COLORS !== 'undefined') {
effective.colors = !!process.env.STASHKU_LOG_CONSOLE_COLORS;
}
return Object.assign(this._storedConfig, effective);
}
/**
* @param {ConsoleLoggerConfiguration} obj
*/
set config(obj) {
this._storedConfig = Object.assign(this._storedConfig, obj);
}
async write(severity, ...args) {
if (this.config && this.config.enabled) {
if (this.config.severity) {
//only log if severity at or above configured minimum.
if (LogUtility.severity(severity) < LogUtility.severity(this.config.severity)) {
return;
}
}
let colors = {
bright: 'greenBright',
regular: 'green'
};
if (severity === 'info') {
colors.bright = 'whiteBright';
colors.regular = 'white';
} else if (severity === 'warn') {
colors.bright = 'yellowBright';
colors.regular = 'yellow';
} else if (severity === 'error') {
colors.bright = 'redBright';
colors.regular = 'red';
}
let prefix = `[${severity}]`.padEnd(8, ' ');
if (this.config.colors) {
prefix = chalk`{${colors.bright} ${prefix}}`;
}
if (this.config.timestamp) {
if (this.config.colors) {
prefix += chalk`{${colors.regular} ${LogUtility.timestamp(!!this.config.local)}}`;
} else {
prefix += `${LogUtility.timestamp(!!this.config.local)}`;
}
}
console[severity](prefix, ...args);
}
}
/**
* Writes a debug-severity log message. This is the lowest severity.
* @param {...any} args - Any object values to be logged.
*/
async debug(...args) {
this.write('debug', ...args);
}
/**
* Writes an information-severity log message. This is the second-to-lowest severity.
* @param {...any} args - Any object values to be logged.
*/
async info(...args) {
this.write('info', ...args);
}
/**
* Writes a warning-severity log message. This is the second-to-highest severity.
* @param {...any} args - Any object values to be logged.
*/
async warn(...args) {
this.write('warn', ...args);
}
/**
* Writes a error-severity log message. This is the highest severity.
* @param {...any} args - Any object values to be logged.
*/
async error(...args) {
this.write('error', ...args);
}
}
export default ConsoleLogger;