-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (146 loc) · 5.11 KB
/
index.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const stripAnsi = require('strip-ansi');
function getTimestamp() {
const date = new Date();
let year = date.getFullYear().toString();
let month = (date.getMonth() + 1).toString();
if (month.length === 1) {
month = `0${month}`;
}
let day = date.getDate().toString();
if (day.length === 1) {
day = `0${day}`;
}
let hours = date.getHours().toString();
if (hours.length === 1) {
hours = `0${hours}`;
}
let minutes = date.getMinutes().toString();
if (minutes.length === 1) {
minutes = `0${minutes}`;
}
let seconds = date.getSeconds().toString();
if (seconds.length === 1) {
seconds = `0${seconds}`;
}
let offsetUTC = (date.getTimezoneOffset() / 60);
if (offsetUTC > 0) {
offsetUTC = `-${offsetUTC}`;
} else {
offsetUTC = `+${(offsetUTC * -1)}`;
}
const timestamp = `${year}-${month}-${day} ${hours}:${minutes}:${seconds} UTC${offsetUTC}`;
return timestamp;
}
function formatArguments(arguments) {
const string = arguments.reduce((string, argument) => {
let newString
if (string === '') {
newString = string;
} else {
newString = `${string} `;
}
if (argument === undefined) {
newString += undefined;
} else if (argument === null) {
newString += argument;
} else if (argument.constructor === Array) {
newString += argument.toString();
} else if (argument.constructor === String) {
newString += argument;
} else if (argument.constructor === Number) {
newString += argument.toString();
} else if (argument.constructor === Boolean) {
newString += argument.toString();
} else if (argument.constructor === Error) {
newString += argument.stack;
} else if (argument.constructor === Object) {
newString += JSON.stringify(argument, null, 4);
} else {
newString += argument.toString();
}
return newString;
}, '');
return string;
}
function printToConsole(timestamp, level, string) {
let log;
if (level === 'ERROR') {
log = `${chalk.grey(timestamp)} ${chalk.bgBlack(chalk.redBright(chalk.bold(level)))} ${string}\n`;
} else if (level === 'WARN') {
log = `${chalk.grey(timestamp)} ${chalk.bgYellow(chalk.black(chalk.bold(level)))} ${string}\n`;
} else if (level === 'INFO') {
log = `${chalk.grey(timestamp)} ${chalk.white(chalk.bold(level))} ${string}\n`;
} else if (level === 'DEBUG') {
log = `${chalk.grey(timestamp)} ${chalk.cyan(level)} ${string}\n`;
} else if (level === 'TRACE') {
log = `${chalk.grey(timestamp)} ${chalk.green(level)} ${string}\n`;
}
process.stdout.write(log);
}
function appendToFile(relativePath) {
const absolutePath = path.resolve(relativePath);
return function(timestamp, level, string) {
const strippedString = stripAnsi(string);
const log = `${timestamp} ${level} ${strippedString}\n`;
fs.appendFileSync(absolutePath, log);
}
}
const Retardlog = function(options) {
let shouldPrintToConsole = false;
let files = [];
options.map((option) => {
if (option.type === 'console') {
shouldPrintToConsole = true;
} else if (option.type === 'file') {
files.push(appendToFile(option.path));
}
});
this.error = function(...arguments) {
const timestamp = getTimestamp();
const string = formatArguments(arguments);
if (shouldPrintToConsole) {
printToConsole(timestamp, 'ERROR', string);
}
files.forEach(func => func(timestamp, 'ERROR', string));
}
this.warn = function (...arguments) {
const timestamp = getTimestamp();
const string = formatArguments(arguments);
if (shouldPrintToConsole) {
printToConsole(timestamp, 'WARN', string);
}
files.forEach(func => func(timestamp, ' WARN', string));
}
this.info = function (...arguments) {
const timestamp = getTimestamp();
const string = formatArguments(arguments);
if (shouldPrintToConsole) {
printToConsole(timestamp, 'INFO', string);
}
files.forEach(func => func(timestamp, ' INFO', string));
}
this.debug = function(...arguments) {
const timestamp = getTimestamp();
const string = formatArguments(arguments);
if (shouldPrintToConsole) {
printToConsole(timestamp, 'DEBUG', string);
}
files.forEach(func => func(timestamp, 'DEBUG', string));
}
this.trace = function(...arguments) {
const timestamp = getTimestamp();
const string = formatArguments(arguments);
if (shouldPrintToConsole) {
printToConsole(timestamp, 'TRACE', string);
}
files.forEach(func => func(timestamp, 'TRACE', string));
}
};
module.exports = {
create: (options) => {
return new Retardlog(options);
},
}