-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
33 lines (23 loc) · 878 Bytes
/
logger.js
File metadata and controls
33 lines (23 loc) · 878 Bytes
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
const chalk = require('chalk');
const dayjs = require('dayjs');
const format = '{tstamp} {tag} {text}\n';
function create(content) {
write(content, 'black', 'bgGreen', 'ADD', true);
}
function remove(content) {
write(content, 'black', 'bgRed', 'DEL', false);
}
function request(content) {
write(content, 'black', 'bgBlue', 'GET', false);
}
function write(content, tagColor, bgTagColor, tag, error = false) {
const timestamp = `[${dayjs().format('DD/MM - HH:mm:ss')}]`;
const logTag = `[${tag}]`;
const stream = error ? process.stderr : process.stdout;
const item = format
.replace('{tstamp}', chalk.gray(timestamp))
.replace('{tag}', chalk[bgTagColor][tagColor](logTag))
.replace('{text}', chalk.white(content));
stream.write(item);
}
module.exports = { create, remove, request };