jet-logger 2.1.1 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/jetLogger.js +229 -0
- package/dist/esm/index.js +1 -1
- package/dist/esm/jetLogger.js +229 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/jetLogger.d.ts +76 -0
- package/package.json +3 -2
- package/dist/cjs/JetLogger.js +0 -239
- package/dist/esm/JetLogger.js +0 -239
- package/dist/types/JetLogger.d.ts +0 -75
package/dist/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { JetLogger, jetLogger, default, } from './jetLogger.js';
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import colors from 'colors';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import util from 'util';
|
|
4
|
+
const LOGGER_MODES = {
|
|
5
|
+
Console: 'CONSOLE',
|
|
6
|
+
File: 'FILE',
|
|
7
|
+
Custom: 'CUSTOM',
|
|
8
|
+
Off: 'OFF',
|
|
9
|
+
};
|
|
10
|
+
const FORMATS = {
|
|
11
|
+
Line: 'LINE',
|
|
12
|
+
Json: 'JSON',
|
|
13
|
+
};
|
|
14
|
+
const LEVELS = {
|
|
15
|
+
Info: {
|
|
16
|
+
Color: 'green',
|
|
17
|
+
Prefix: 'INFO',
|
|
18
|
+
},
|
|
19
|
+
Important: {
|
|
20
|
+
Color: 'magenta',
|
|
21
|
+
Prefix: 'IMPORTANT',
|
|
22
|
+
},
|
|
23
|
+
Warning: {
|
|
24
|
+
Color: 'yellow',
|
|
25
|
+
Prefix: 'WARNING',
|
|
26
|
+
},
|
|
27
|
+
Error: {
|
|
28
|
+
Color: 'red',
|
|
29
|
+
Prefix: 'ERROR',
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
const DEFAULTS = {
|
|
33
|
+
mode: LOGGER_MODES.Console,
|
|
34
|
+
filePath: 'jet-logger.log',
|
|
35
|
+
timestamp: true,
|
|
36
|
+
format: FORMATS.Line,
|
|
37
|
+
customLogFn: () => ({}),
|
|
38
|
+
};
|
|
39
|
+
const Errors = {
|
|
40
|
+
CustomLogger: 'Custom logger mode set to true, but no custom logger was provided.',
|
|
41
|
+
Mode: 'The correct logger mode was not specified: Must be "CUSTOM", "FILE", ' +
|
|
42
|
+
'"OFF", or "CONSOLE".',
|
|
43
|
+
};
|
|
44
|
+
export const JetLogger = {
|
|
45
|
+
Modes: LOGGER_MODES,
|
|
46
|
+
Formats: FORMATS,
|
|
47
|
+
instanceOf,
|
|
48
|
+
};
|
|
49
|
+
const kJetLogger = Symbol('k-jet-logger');
|
|
50
|
+
export function jetLogger(options) {
|
|
51
|
+
let mode = DEFAULTS.mode, filePath = DEFAULTS.filePath, timestamp = DEFAULTS.timestamp, format = DEFAULTS.format, customLogFn = DEFAULTS.customLogFn;
|
|
52
|
+
if (options?.mode !== undefined) {
|
|
53
|
+
mode = options.mode;
|
|
54
|
+
}
|
|
55
|
+
else if (!!process.env.JET_LOGGER_MODE) {
|
|
56
|
+
mode = process.env.JET_LOGGER_MODE.toUpperCase();
|
|
57
|
+
}
|
|
58
|
+
if (mode === LOGGER_MODES.Off) {
|
|
59
|
+
return {
|
|
60
|
+
info: (_, __) => ({}),
|
|
61
|
+
imp: (_, __) => ({}),
|
|
62
|
+
warn: (_, __) => ({}),
|
|
63
|
+
err: (_, __) => ({}),
|
|
64
|
+
[kJetLogger]: true,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if (mode === LOGGER_MODES.Custom) {
|
|
68
|
+
if (options?.customLogger !== undefined) {
|
|
69
|
+
customLogFn = options.customLogger;
|
|
70
|
+
}
|
|
71
|
+
if (!customLogFn) {
|
|
72
|
+
throw Error(Errors.CustomLogger);
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
info: setupPrintWithCustomLogger(LEVELS.Info, customLogFn),
|
|
76
|
+
imp: setupPrintWithCustomLogger(LEVELS.Important, customLogFn),
|
|
77
|
+
warn: setupPrintWithCustomLogger(LEVELS.Warning, customLogFn),
|
|
78
|
+
err: setupPrintWithCustomLogger(LEVELS.Error, customLogFn),
|
|
79
|
+
[kJetLogger]: true,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
if (options?.filepath !== undefined) {
|
|
83
|
+
filePath = options.filepath;
|
|
84
|
+
}
|
|
85
|
+
else if (!!process.env.JET_LOGGER_FILEPATH) {
|
|
86
|
+
filePath = process.env.JET_LOGGER_FILEPATH;
|
|
87
|
+
}
|
|
88
|
+
if (options?.timestamp !== undefined) {
|
|
89
|
+
timestamp = options.timestamp;
|
|
90
|
+
}
|
|
91
|
+
else if (!!process.env.JET_LOGGER_TIMESTAMP) {
|
|
92
|
+
const envVar = process.env.JET_LOGGER_TIMESTAMP;
|
|
93
|
+
timestamp = envVar.toUpperCase() === 'TRUE';
|
|
94
|
+
}
|
|
95
|
+
if (options?.format !== undefined) {
|
|
96
|
+
format = options.format;
|
|
97
|
+
}
|
|
98
|
+
else if (!!process.env.JET_LOGGER_FORMAT) {
|
|
99
|
+
format = process.env.JET_LOGGER_FORMAT.toUpperCase();
|
|
100
|
+
}
|
|
101
|
+
let formatter = () => '';
|
|
102
|
+
if (format === FORMATS.Line) {
|
|
103
|
+
formatter = setupLineFormatter(timestamp);
|
|
104
|
+
}
|
|
105
|
+
else if (format === FORMATS.Json) {
|
|
106
|
+
formatter = setupJsonFormatter(timestamp);
|
|
107
|
+
}
|
|
108
|
+
if (mode === LOGGER_MODES.File) {
|
|
109
|
+
let filePathDatetime = true;
|
|
110
|
+
if (options?.filepathDatetimeParam !== undefined) {
|
|
111
|
+
filePathDatetime = options.filepathDatetimeParam;
|
|
112
|
+
}
|
|
113
|
+
else if (!!process.env.JET_LOGGER_FILEPATH_DATETIME) {
|
|
114
|
+
const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
|
|
115
|
+
filePathDatetime = envVar.toUpperCase() === 'TRUE';
|
|
116
|
+
}
|
|
117
|
+
if (filePathDatetime) {
|
|
118
|
+
filePath = addDatetimeToFileName(filePath);
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
info: setupPrintToFile(LEVELS.Info, formatter, filePath),
|
|
122
|
+
imp: setupPrintToFile(LEVELS.Important, formatter, filePath),
|
|
123
|
+
warn: setupPrintToFile(LEVELS.Warning, formatter, filePath),
|
|
124
|
+
err: setupPrintToFile(LEVELS.Error, formatter, filePath),
|
|
125
|
+
[kJetLogger]: true,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
info: setupPrintToConsole(LEVELS.Info, formatter),
|
|
130
|
+
imp: setupPrintToConsole(LEVELS.Important, formatter),
|
|
131
|
+
warn: setupPrintToConsole(LEVELS.Warning, formatter),
|
|
132
|
+
err: setupPrintToConsole(LEVELS.Error, formatter),
|
|
133
|
+
[kJetLogger]: true,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function setupPrintWithCustomLogger(level, customLogFn) {
|
|
137
|
+
return (content, printFull) => {
|
|
138
|
+
let contentNew;
|
|
139
|
+
if (printFull) {
|
|
140
|
+
contentNew = util.inspect(content);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
contentNew = String(content);
|
|
144
|
+
}
|
|
145
|
+
return customLogFn(new Date(), level.Prefix, contentNew);
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function setupLineFormatter(timestamp) {
|
|
149
|
+
if (timestamp) {
|
|
150
|
+
return (content, level) => {
|
|
151
|
+
const contentNew = level.Prefix + ': ' + content, time = '[' + new Date().toISOString() + '] ';
|
|
152
|
+
return time + contentNew;
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
return (content, level) => {
|
|
157
|
+
return level.Prefix + ': ' + content;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function setupJsonFormatter(timestamp) {
|
|
162
|
+
if (timestamp) {
|
|
163
|
+
return (content, level) => {
|
|
164
|
+
const json = {
|
|
165
|
+
level: level.Prefix,
|
|
166
|
+
message: content,
|
|
167
|
+
};
|
|
168
|
+
json.timestamp = new Date().toISOString();
|
|
169
|
+
return JSON.stringify(json);
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
return (content, level) => {
|
|
174
|
+
const json = {
|
|
175
|
+
level: level.Prefix,
|
|
176
|
+
message: content,
|
|
177
|
+
};
|
|
178
|
+
return JSON.stringify(json);
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function setupPrintToFile(level, formatter, filePath) {
|
|
183
|
+
return (content, printFull) => {
|
|
184
|
+
let contentNew;
|
|
185
|
+
if (!!printFull) {
|
|
186
|
+
contentNew = util.inspect(content);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
contentNew = String(content);
|
|
190
|
+
}
|
|
191
|
+
contentNew = formatter(contentNew, level);
|
|
192
|
+
fs.appendFile(filePath, contentNew, (err) => {
|
|
193
|
+
if (!!err) {
|
|
194
|
+
console.error(err);
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
function setupPrintToConsole(level, formatter) {
|
|
200
|
+
return (content, printFull) => {
|
|
201
|
+
let contentNew;
|
|
202
|
+
if (!!printFull) {
|
|
203
|
+
contentNew = util.inspect(content);
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
contentNew = String(content);
|
|
207
|
+
}
|
|
208
|
+
const colorFn = colors[level.Color];
|
|
209
|
+
contentNew = formatter(contentNew, level);
|
|
210
|
+
console.log(colorFn(contentNew));
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
function addDatetimeToFileName(filePath) {
|
|
214
|
+
const dateStr = new Date()
|
|
215
|
+
.toISOString()
|
|
216
|
+
.split('-')
|
|
217
|
+
.join('')
|
|
218
|
+
.split(':')
|
|
219
|
+
.join('')
|
|
220
|
+
.slice(0, 15);
|
|
221
|
+
const filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = dateStr + '_' + fileName;
|
|
222
|
+
filePathArr[lastIdx] = fileNameNew;
|
|
223
|
+
return filePathArr.join('/');
|
|
224
|
+
}
|
|
225
|
+
function instanceOf(arg) {
|
|
226
|
+
return (typeof arg === 'object' &&
|
|
227
|
+
arg[kJetLogger] === true);
|
|
228
|
+
}
|
|
229
|
+
export default jetLogger();
|
package/dist/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { JetLogger, jetLogger, default, } from './jetLogger.js';
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import colors from 'colors';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import util from 'util';
|
|
4
|
+
const LOGGER_MODES = {
|
|
5
|
+
Console: 'CONSOLE',
|
|
6
|
+
File: 'FILE',
|
|
7
|
+
Custom: 'CUSTOM',
|
|
8
|
+
Off: 'OFF',
|
|
9
|
+
};
|
|
10
|
+
const FORMATS = {
|
|
11
|
+
Line: 'LINE',
|
|
12
|
+
Json: 'JSON',
|
|
13
|
+
};
|
|
14
|
+
const LEVELS = {
|
|
15
|
+
Info: {
|
|
16
|
+
Color: 'green',
|
|
17
|
+
Prefix: 'INFO',
|
|
18
|
+
},
|
|
19
|
+
Important: {
|
|
20
|
+
Color: 'magenta',
|
|
21
|
+
Prefix: 'IMPORTANT',
|
|
22
|
+
},
|
|
23
|
+
Warning: {
|
|
24
|
+
Color: 'yellow',
|
|
25
|
+
Prefix: 'WARNING',
|
|
26
|
+
},
|
|
27
|
+
Error: {
|
|
28
|
+
Color: 'red',
|
|
29
|
+
Prefix: 'ERROR',
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
const DEFAULTS = {
|
|
33
|
+
mode: LOGGER_MODES.Console,
|
|
34
|
+
filePath: 'jet-logger.log',
|
|
35
|
+
timestamp: true,
|
|
36
|
+
format: FORMATS.Line,
|
|
37
|
+
customLogFn: () => ({}),
|
|
38
|
+
};
|
|
39
|
+
const Errors = {
|
|
40
|
+
CustomLogger: 'Custom logger mode set to true, but no custom logger was provided.',
|
|
41
|
+
Mode: 'The correct logger mode was not specified: Must be "CUSTOM", "FILE", ' +
|
|
42
|
+
'"OFF", or "CONSOLE".',
|
|
43
|
+
};
|
|
44
|
+
export const JetLogger = {
|
|
45
|
+
Modes: LOGGER_MODES,
|
|
46
|
+
Formats: FORMATS,
|
|
47
|
+
instanceOf,
|
|
48
|
+
};
|
|
49
|
+
const kJetLogger = Symbol('k-jet-logger');
|
|
50
|
+
export function jetLogger(options) {
|
|
51
|
+
let mode = DEFAULTS.mode, filePath = DEFAULTS.filePath, timestamp = DEFAULTS.timestamp, format = DEFAULTS.format, customLogFn = DEFAULTS.customLogFn;
|
|
52
|
+
if (options?.mode !== undefined) {
|
|
53
|
+
mode = options.mode;
|
|
54
|
+
}
|
|
55
|
+
else if (!!process.env.JET_LOGGER_MODE) {
|
|
56
|
+
mode = process.env.JET_LOGGER_MODE.toUpperCase();
|
|
57
|
+
}
|
|
58
|
+
if (mode === LOGGER_MODES.Off) {
|
|
59
|
+
return {
|
|
60
|
+
info: (_, __) => ({}),
|
|
61
|
+
imp: (_, __) => ({}),
|
|
62
|
+
warn: (_, __) => ({}),
|
|
63
|
+
err: (_, __) => ({}),
|
|
64
|
+
[kJetLogger]: true,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if (mode === LOGGER_MODES.Custom) {
|
|
68
|
+
if (options?.customLogger !== undefined) {
|
|
69
|
+
customLogFn = options.customLogger;
|
|
70
|
+
}
|
|
71
|
+
if (!customLogFn) {
|
|
72
|
+
throw Error(Errors.CustomLogger);
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
info: setupPrintWithCustomLogger(LEVELS.Info, customLogFn),
|
|
76
|
+
imp: setupPrintWithCustomLogger(LEVELS.Important, customLogFn),
|
|
77
|
+
warn: setupPrintWithCustomLogger(LEVELS.Warning, customLogFn),
|
|
78
|
+
err: setupPrintWithCustomLogger(LEVELS.Error, customLogFn),
|
|
79
|
+
[kJetLogger]: true,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
if (options?.filepath !== undefined) {
|
|
83
|
+
filePath = options.filepath;
|
|
84
|
+
}
|
|
85
|
+
else if (!!process.env.JET_LOGGER_FILEPATH) {
|
|
86
|
+
filePath = process.env.JET_LOGGER_FILEPATH;
|
|
87
|
+
}
|
|
88
|
+
if (options?.timestamp !== undefined) {
|
|
89
|
+
timestamp = options.timestamp;
|
|
90
|
+
}
|
|
91
|
+
else if (!!process.env.JET_LOGGER_TIMESTAMP) {
|
|
92
|
+
const envVar = process.env.JET_LOGGER_TIMESTAMP;
|
|
93
|
+
timestamp = envVar.toUpperCase() === 'TRUE';
|
|
94
|
+
}
|
|
95
|
+
if (options?.format !== undefined) {
|
|
96
|
+
format = options.format;
|
|
97
|
+
}
|
|
98
|
+
else if (!!process.env.JET_LOGGER_FORMAT) {
|
|
99
|
+
format = process.env.JET_LOGGER_FORMAT.toUpperCase();
|
|
100
|
+
}
|
|
101
|
+
let formatter = () => '';
|
|
102
|
+
if (format === FORMATS.Line) {
|
|
103
|
+
formatter = setupLineFormatter(timestamp);
|
|
104
|
+
}
|
|
105
|
+
else if (format === FORMATS.Json) {
|
|
106
|
+
formatter = setupJsonFormatter(timestamp);
|
|
107
|
+
}
|
|
108
|
+
if (mode === LOGGER_MODES.File) {
|
|
109
|
+
let filePathDatetime = true;
|
|
110
|
+
if (options?.filepathDatetimeParam !== undefined) {
|
|
111
|
+
filePathDatetime = options.filepathDatetimeParam;
|
|
112
|
+
}
|
|
113
|
+
else if (!!process.env.JET_LOGGER_FILEPATH_DATETIME) {
|
|
114
|
+
const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
|
|
115
|
+
filePathDatetime = envVar.toUpperCase() === 'TRUE';
|
|
116
|
+
}
|
|
117
|
+
if (filePathDatetime) {
|
|
118
|
+
filePath = addDatetimeToFileName(filePath);
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
info: setupPrintToFile(LEVELS.Info, formatter, filePath),
|
|
122
|
+
imp: setupPrintToFile(LEVELS.Important, formatter, filePath),
|
|
123
|
+
warn: setupPrintToFile(LEVELS.Warning, formatter, filePath),
|
|
124
|
+
err: setupPrintToFile(LEVELS.Error, formatter, filePath),
|
|
125
|
+
[kJetLogger]: true,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
info: setupPrintToConsole(LEVELS.Info, formatter),
|
|
130
|
+
imp: setupPrintToConsole(LEVELS.Important, formatter),
|
|
131
|
+
warn: setupPrintToConsole(LEVELS.Warning, formatter),
|
|
132
|
+
err: setupPrintToConsole(LEVELS.Error, formatter),
|
|
133
|
+
[kJetLogger]: true,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function setupPrintWithCustomLogger(level, customLogFn) {
|
|
137
|
+
return (content, printFull) => {
|
|
138
|
+
let contentNew;
|
|
139
|
+
if (printFull) {
|
|
140
|
+
contentNew = util.inspect(content);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
contentNew = String(content);
|
|
144
|
+
}
|
|
145
|
+
return customLogFn(new Date(), level.Prefix, contentNew);
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function setupLineFormatter(timestamp) {
|
|
149
|
+
if (timestamp) {
|
|
150
|
+
return (content, level) => {
|
|
151
|
+
const contentNew = level.Prefix + ': ' + content, time = '[' + new Date().toISOString() + '] ';
|
|
152
|
+
return time + contentNew;
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
return (content, level) => {
|
|
157
|
+
return level.Prefix + ': ' + content;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function setupJsonFormatter(timestamp) {
|
|
162
|
+
if (timestamp) {
|
|
163
|
+
return (content, level) => {
|
|
164
|
+
const json = {
|
|
165
|
+
level: level.Prefix,
|
|
166
|
+
message: content,
|
|
167
|
+
};
|
|
168
|
+
json.timestamp = new Date().toISOString();
|
|
169
|
+
return JSON.stringify(json);
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
return (content, level) => {
|
|
174
|
+
const json = {
|
|
175
|
+
level: level.Prefix,
|
|
176
|
+
message: content,
|
|
177
|
+
};
|
|
178
|
+
return JSON.stringify(json);
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function setupPrintToFile(level, formatter, filePath) {
|
|
183
|
+
return (content, printFull) => {
|
|
184
|
+
let contentNew;
|
|
185
|
+
if (!!printFull) {
|
|
186
|
+
contentNew = util.inspect(content);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
contentNew = String(content);
|
|
190
|
+
}
|
|
191
|
+
contentNew = formatter(contentNew, level);
|
|
192
|
+
fs.appendFile(filePath, contentNew, (err) => {
|
|
193
|
+
if (!!err) {
|
|
194
|
+
console.error(err);
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
function setupPrintToConsole(level, formatter) {
|
|
200
|
+
return (content, printFull) => {
|
|
201
|
+
let contentNew;
|
|
202
|
+
if (!!printFull) {
|
|
203
|
+
contentNew = util.inspect(content);
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
contentNew = String(content);
|
|
207
|
+
}
|
|
208
|
+
const colorFn = colors[level.Color];
|
|
209
|
+
contentNew = formatter(contentNew, level);
|
|
210
|
+
console.log(colorFn(contentNew));
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
function addDatetimeToFileName(filePath) {
|
|
214
|
+
const dateStr = new Date()
|
|
215
|
+
.toISOString()
|
|
216
|
+
.split('-')
|
|
217
|
+
.join('')
|
|
218
|
+
.split(':')
|
|
219
|
+
.join('')
|
|
220
|
+
.slice(0, 15);
|
|
221
|
+
const filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = dateStr + '_' + fileName;
|
|
222
|
+
filePathArr[lastIdx] = fileNameNew;
|
|
223
|
+
return filePathArr.join('/');
|
|
224
|
+
}
|
|
225
|
+
function instanceOf(arg) {
|
|
226
|
+
return (typeof arg === 'object' &&
|
|
227
|
+
arg[kJetLogger] === true);
|
|
228
|
+
}
|
|
229
|
+
export default jetLogger();
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { type CustomLogger, JetLogger, jetLogger, default, } from './jetLogger.js';
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/******************************************************************************
|
|
2
|
+
Constants
|
|
3
|
+
******************************************************************************/
|
|
4
|
+
declare const LOGGER_MODES: {
|
|
5
|
+
readonly Console: "CONSOLE";
|
|
6
|
+
readonly File: "FILE";
|
|
7
|
+
readonly Custom: "CUSTOM";
|
|
8
|
+
readonly Off: "OFF";
|
|
9
|
+
};
|
|
10
|
+
declare const FORMATS: {
|
|
11
|
+
readonly Line: "LINE";
|
|
12
|
+
readonly Json: "JSON";
|
|
13
|
+
};
|
|
14
|
+
export declare const JetLogger: {
|
|
15
|
+
readonly Modes: {
|
|
16
|
+
readonly Console: "CONSOLE";
|
|
17
|
+
readonly File: "FILE";
|
|
18
|
+
readonly Custom: "CUSTOM";
|
|
19
|
+
readonly Off: "OFF";
|
|
20
|
+
};
|
|
21
|
+
readonly Formats: {
|
|
22
|
+
readonly Line: "LINE";
|
|
23
|
+
readonly Json: "JSON";
|
|
24
|
+
};
|
|
25
|
+
readonly instanceOf: typeof instanceOf;
|
|
26
|
+
};
|
|
27
|
+
declare const kJetLogger: unique symbol;
|
|
28
|
+
/******************************************************************************
|
|
29
|
+
Types
|
|
30
|
+
******************************************************************************/
|
|
31
|
+
type LoggerModes = (typeof LOGGER_MODES)[keyof typeof LOGGER_MODES];
|
|
32
|
+
type Formats = (typeof FORMATS)[keyof typeof FORMATS];
|
|
33
|
+
type LogFunction = (content: unknown, printFull?: boolean) => void;
|
|
34
|
+
export type CustomLogger = (timestamp: Date, prefix: string, content: unknown) => void;
|
|
35
|
+
interface Options {
|
|
36
|
+
mode?: LoggerModes;
|
|
37
|
+
filepath?: string;
|
|
38
|
+
filepathDatetimeParam?: boolean;
|
|
39
|
+
timestamp?: boolean;
|
|
40
|
+
format?: Formats;
|
|
41
|
+
customLogger?: CustomLogger;
|
|
42
|
+
}
|
|
43
|
+
interface JetLogger {
|
|
44
|
+
info: (content: unknown, print?: boolean) => void;
|
|
45
|
+
imp: (content: unknown, print?: boolean) => void;
|
|
46
|
+
warn: (content: unknown, print?: boolean) => void;
|
|
47
|
+
err: (content: unknown, print?: boolean) => void;
|
|
48
|
+
}
|
|
49
|
+
/******************************************************************************
|
|
50
|
+
Functions
|
|
51
|
+
******************************************************************************/
|
|
52
|
+
/**
|
|
53
|
+
* Default function
|
|
54
|
+
*/
|
|
55
|
+
export declare function jetLogger(options?: Options): {
|
|
56
|
+
readonly info: LogFunction;
|
|
57
|
+
readonly imp: LogFunction;
|
|
58
|
+
readonly warn: LogFunction;
|
|
59
|
+
readonly err: LogFunction;
|
|
60
|
+
readonly [kJetLogger]: true;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Check if an object is an instance of jetLogger
|
|
64
|
+
*/
|
|
65
|
+
declare function instanceOf(arg: unknown): arg is JetLogger;
|
|
66
|
+
/******************************************************************************
|
|
67
|
+
Export
|
|
68
|
+
******************************************************************************/
|
|
69
|
+
declare const _default: {
|
|
70
|
+
readonly info: LogFunction;
|
|
71
|
+
readonly imp: LogFunction;
|
|
72
|
+
readonly warn: LogFunction;
|
|
73
|
+
readonly err: LogFunction;
|
|
74
|
+
readonly [kJetLogger]: true;
|
|
75
|
+
};
|
|
76
|
+
export default _default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jet-logger",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"description": "A super quick, easy to setup logging tool for NodeJS/TypeScript.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -59,11 +59,12 @@
|
|
|
59
59
|
},
|
|
60
60
|
"homepage": "https://github.com/seanpmaxwell/jet-logger#readme",
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"colors": "1.
|
|
62
|
+
"colors": "1.4.0"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
65
|
"@eslint/js": "^9.26.0",
|
|
66
66
|
"@stylistic/eslint-plugin": "^5.6.1",
|
|
67
|
+
"@trivago/prettier-plugin-sort-imports": "^6.0.1",
|
|
67
68
|
"@types/node": "^22.8.1",
|
|
68
69
|
"eslint": "^9.26.0",
|
|
69
70
|
"eslint-config-prettier": "^10.1.8",
|
package/dist/cjs/JetLogger.js
DELETED
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-process-env */
|
|
2
|
-
import util from 'util';
|
|
3
|
-
import colors from 'colors';
|
|
4
|
-
import fs from 'fs';
|
|
5
|
-
/******************************************************************************
|
|
6
|
-
Variables
|
|
7
|
-
******************************************************************************/
|
|
8
|
-
// Options for printing a log.
|
|
9
|
-
export const LoggerModes = {
|
|
10
|
-
Console: 'CONSOLE',
|
|
11
|
-
File: 'FILE',
|
|
12
|
-
Custom: 'CUSTOM',
|
|
13
|
-
Off: 'OFF',
|
|
14
|
-
};
|
|
15
|
-
// Log formats
|
|
16
|
-
export const Formats = {
|
|
17
|
-
Line: 'LINE',
|
|
18
|
-
Json: 'JSON',
|
|
19
|
-
};
|
|
20
|
-
// Note colors here need be a color from
|
|
21
|
-
// the colors library above.
|
|
22
|
-
const Levels = {
|
|
23
|
-
Info: {
|
|
24
|
-
Color: 'green',
|
|
25
|
-
Prefix: 'INFO',
|
|
26
|
-
},
|
|
27
|
-
Important: {
|
|
28
|
-
Color: 'magenta',
|
|
29
|
-
Prefix: 'IMPORTANT',
|
|
30
|
-
},
|
|
31
|
-
Warning: {
|
|
32
|
-
Color: 'yellow',
|
|
33
|
-
Prefix: 'WARNING',
|
|
34
|
-
},
|
|
35
|
-
Error: {
|
|
36
|
-
Color: 'red',
|
|
37
|
-
Prefix: 'ERROR',
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
// Errors
|
|
41
|
-
const Errors = {
|
|
42
|
-
CustomLogFn: 'Custom logger mode set to true, but no custom logger was ' + 'provided.',
|
|
43
|
-
Mode: 'The correct logger mode was not specified: Must be "CUSTOM", ' +
|
|
44
|
-
'"FILE", "OFF", or "CONSOLE".',
|
|
45
|
-
};
|
|
46
|
-
/******************************************************************************
|
|
47
|
-
Classes
|
|
48
|
-
******************************************************************************/
|
|
49
|
-
export class JetLogger {
|
|
50
|
-
mode = LoggerModes.Console;
|
|
51
|
-
filePath = 'jet-logger.log';
|
|
52
|
-
timestamp = true;
|
|
53
|
-
format = Formats.Line;
|
|
54
|
-
customLogFn = () => ({});
|
|
55
|
-
/**
|
|
56
|
-
* Constructor
|
|
57
|
-
*/
|
|
58
|
-
constructor(mode, filepath, filepathDatetimeParam, timestamp, format, customLogFn) {
|
|
59
|
-
// Setup the mode
|
|
60
|
-
if (mode !== undefined) {
|
|
61
|
-
this.mode = mode;
|
|
62
|
-
}
|
|
63
|
-
else if (!!process.env.JET_LOGGER_MODE) {
|
|
64
|
-
this.mode = process.env.JET_LOGGER_MODE.toUpperCase();
|
|
65
|
-
}
|
|
66
|
-
// Filepath
|
|
67
|
-
if (filepath !== undefined) {
|
|
68
|
-
this.filePath = filepath;
|
|
69
|
-
}
|
|
70
|
-
else if (!!process.env.JET_LOGGER_FILEPATH) {
|
|
71
|
-
this.filePath = process.env.JET_LOGGER_FILEPATH;
|
|
72
|
-
}
|
|
73
|
-
// FilePath dateTime
|
|
74
|
-
let filePathDatetime = true;
|
|
75
|
-
if (filepathDatetimeParam !== undefined) {
|
|
76
|
-
filePathDatetime = filepathDatetimeParam;
|
|
77
|
-
}
|
|
78
|
-
else if (!!process.env.JET_LOGGER_FILEPATH_DATETIME) {
|
|
79
|
-
const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
|
|
80
|
-
filePathDatetime = envVar.toUpperCase() === 'TRUE';
|
|
81
|
-
}
|
|
82
|
-
// Timestamp
|
|
83
|
-
if (timestamp !== undefined) {
|
|
84
|
-
this.timestamp = timestamp;
|
|
85
|
-
}
|
|
86
|
-
else if (!!process.env.JET_LOGGER_TIMESTAMP) {
|
|
87
|
-
const envVar = process.env.JET_LOGGER_TIMESTAMP;
|
|
88
|
-
this.timestamp = envVar.toUpperCase() === 'TRUE';
|
|
89
|
-
}
|
|
90
|
-
// Format
|
|
91
|
-
if (format !== undefined) {
|
|
92
|
-
this.format = format;
|
|
93
|
-
}
|
|
94
|
-
else if (!!process.env.JET_LOGGER_FORMAT) {
|
|
95
|
-
this.format = process.env.JET_LOGGER_FORMAT.toUpperCase();
|
|
96
|
-
}
|
|
97
|
-
// Modify filepath if filepath datetime is true
|
|
98
|
-
if (filePathDatetime) {
|
|
99
|
-
this.filePath = this.addDatetimeToFileName(this.filePath);
|
|
100
|
-
}
|
|
101
|
-
// Custom Logger Function
|
|
102
|
-
if (customLogFn !== undefined) {
|
|
103
|
-
this.customLogFn = customLogFn;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Prepend the filename in the file path with a timestamp.
|
|
108
|
-
* i.e. '/home/jet-logger.log' => '/home/20220805T033709_jet-logger.log'
|
|
109
|
-
*/
|
|
110
|
-
addDatetimeToFileName(filePath) {
|
|
111
|
-
// Get the date string
|
|
112
|
-
const dateStr = new Date()
|
|
113
|
-
.toISOString()
|
|
114
|
-
.split('-')
|
|
115
|
-
.join('')
|
|
116
|
-
.split(':')
|
|
117
|
-
.join('')
|
|
118
|
-
.slice(0, 15);
|
|
119
|
-
// Setup new file name
|
|
120
|
-
const filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = dateStr + '_' + fileName;
|
|
121
|
-
// Setup new file path
|
|
122
|
-
filePathArr[lastIdx] = fileNameNew;
|
|
123
|
-
return filePathArr.join('/');
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Print information.
|
|
127
|
-
*/
|
|
128
|
-
info(content, printFull) {
|
|
129
|
-
this.printLog(content, !!printFull, Levels.Info);
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Print important information.
|
|
133
|
-
*/
|
|
134
|
-
imp(content, printFull) {
|
|
135
|
-
this.printLog(content, !!printFull, Levels.Important);
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Print important information.
|
|
139
|
-
*/
|
|
140
|
-
warn(content, printFull) {
|
|
141
|
-
this.printLog(content, !!printFull, Levels.Warning);
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Print important information.
|
|
145
|
-
*/
|
|
146
|
-
err(content, printFull) {
|
|
147
|
-
this.printLog(content, !!printFull, Levels.Error);
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Print the log using the provided settings.
|
|
151
|
-
*/
|
|
152
|
-
printLog(contentParam, printFull, level) {
|
|
153
|
-
// Do nothing if turned off
|
|
154
|
-
if (this.mode === LoggerModes.Off) {
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
// Print full
|
|
158
|
-
let content;
|
|
159
|
-
if (printFull) {
|
|
160
|
-
content = util.inspect(contentParam);
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
content = String(contentParam);
|
|
164
|
-
}
|
|
165
|
-
// Fire the custom logger if that's the option
|
|
166
|
-
if (this.mode === LoggerModes.Custom) {
|
|
167
|
-
if (!!this.customLogFn) {
|
|
168
|
-
return this.customLogFn(new Date(), level.Prefix, content);
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
throw Error(Errors.CustomLogFn);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
// Print line or json
|
|
175
|
-
if (this.format === Formats.Line) {
|
|
176
|
-
content = this.setupLineFormat(content, level);
|
|
177
|
-
}
|
|
178
|
-
else if (this.format === Formats.Json) {
|
|
179
|
-
content = this.setupJsonFormat(content, level);
|
|
180
|
-
}
|
|
181
|
-
// Print Console
|
|
182
|
-
if (this.mode === LoggerModes.Console) {
|
|
183
|
-
const colorFn = colors[level.Color];
|
|
184
|
-
// eslint-disable-next-line no-console
|
|
185
|
-
console.log(colorFn(content));
|
|
186
|
-
// Print File
|
|
187
|
-
}
|
|
188
|
-
else if (this.mode === LoggerModes.File) {
|
|
189
|
-
this.writeToFile(content + '\n');
|
|
190
|
-
// If reach this point, mode setting was bad
|
|
191
|
-
}
|
|
192
|
-
else {
|
|
193
|
-
throw Error(Errors.Mode);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Setup line format.
|
|
198
|
-
*/
|
|
199
|
-
setupLineFormat(content, level) {
|
|
200
|
-
// Format
|
|
201
|
-
content = level.Prefix + ': ' + content;
|
|
202
|
-
if (this.timestamp) {
|
|
203
|
-
const time = '[' + new Date().toISOString() + '] ';
|
|
204
|
-
return time + content;
|
|
205
|
-
}
|
|
206
|
-
// Return
|
|
207
|
-
return content;
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Setup json format.
|
|
211
|
-
*/
|
|
212
|
-
setupJsonFormat(content, level) {
|
|
213
|
-
// Format
|
|
214
|
-
const json = {
|
|
215
|
-
level: level.Prefix,
|
|
216
|
-
message: content,
|
|
217
|
-
};
|
|
218
|
-
if (this.timestamp) {
|
|
219
|
-
json.timestamp = new Date().toISOString();
|
|
220
|
-
}
|
|
221
|
-
// Return
|
|
222
|
-
return JSON.stringify(json);
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Write to file.
|
|
226
|
-
*/
|
|
227
|
-
writeToFile(content) {
|
|
228
|
-
fs.appendFile(this.filePath, content, (err) => {
|
|
229
|
-
if (!!err) {
|
|
230
|
-
// eslint-disable-next-line no-console
|
|
231
|
-
console.error(err);
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
/******************************************************************************
|
|
237
|
-
Export
|
|
238
|
-
******************************************************************************/
|
|
239
|
-
export default new JetLogger();
|
package/dist/esm/JetLogger.js
DELETED
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-process-env */
|
|
2
|
-
import util from 'util';
|
|
3
|
-
import colors from 'colors';
|
|
4
|
-
import fs from 'fs';
|
|
5
|
-
/******************************************************************************
|
|
6
|
-
Variables
|
|
7
|
-
******************************************************************************/
|
|
8
|
-
// Options for printing a log.
|
|
9
|
-
export const LoggerModes = {
|
|
10
|
-
Console: 'CONSOLE',
|
|
11
|
-
File: 'FILE',
|
|
12
|
-
Custom: 'CUSTOM',
|
|
13
|
-
Off: 'OFF',
|
|
14
|
-
};
|
|
15
|
-
// Log formats
|
|
16
|
-
export const Formats = {
|
|
17
|
-
Line: 'LINE',
|
|
18
|
-
Json: 'JSON',
|
|
19
|
-
};
|
|
20
|
-
// Note colors here need be a color from
|
|
21
|
-
// the colors library above.
|
|
22
|
-
const Levels = {
|
|
23
|
-
Info: {
|
|
24
|
-
Color: 'green',
|
|
25
|
-
Prefix: 'INFO',
|
|
26
|
-
},
|
|
27
|
-
Important: {
|
|
28
|
-
Color: 'magenta',
|
|
29
|
-
Prefix: 'IMPORTANT',
|
|
30
|
-
},
|
|
31
|
-
Warning: {
|
|
32
|
-
Color: 'yellow',
|
|
33
|
-
Prefix: 'WARNING',
|
|
34
|
-
},
|
|
35
|
-
Error: {
|
|
36
|
-
Color: 'red',
|
|
37
|
-
Prefix: 'ERROR',
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
// Errors
|
|
41
|
-
const Errors = {
|
|
42
|
-
CustomLogFn: 'Custom logger mode set to true, but no custom logger was ' + 'provided.',
|
|
43
|
-
Mode: 'The correct logger mode was not specified: Must be "CUSTOM", ' +
|
|
44
|
-
'"FILE", "OFF", or "CONSOLE".',
|
|
45
|
-
};
|
|
46
|
-
/******************************************************************************
|
|
47
|
-
Classes
|
|
48
|
-
******************************************************************************/
|
|
49
|
-
export class JetLogger {
|
|
50
|
-
mode = LoggerModes.Console;
|
|
51
|
-
filePath = 'jet-logger.log';
|
|
52
|
-
timestamp = true;
|
|
53
|
-
format = Formats.Line;
|
|
54
|
-
customLogFn = () => ({});
|
|
55
|
-
/**
|
|
56
|
-
* Constructor
|
|
57
|
-
*/
|
|
58
|
-
constructor(mode, filepath, filepathDatetimeParam, timestamp, format, customLogFn) {
|
|
59
|
-
// Setup the mode
|
|
60
|
-
if (mode !== undefined) {
|
|
61
|
-
this.mode = mode;
|
|
62
|
-
}
|
|
63
|
-
else if (!!process.env.JET_LOGGER_MODE) {
|
|
64
|
-
this.mode = process.env.JET_LOGGER_MODE.toUpperCase();
|
|
65
|
-
}
|
|
66
|
-
// Filepath
|
|
67
|
-
if (filepath !== undefined) {
|
|
68
|
-
this.filePath = filepath;
|
|
69
|
-
}
|
|
70
|
-
else if (!!process.env.JET_LOGGER_FILEPATH) {
|
|
71
|
-
this.filePath = process.env.JET_LOGGER_FILEPATH;
|
|
72
|
-
}
|
|
73
|
-
// FilePath dateTime
|
|
74
|
-
let filePathDatetime = true;
|
|
75
|
-
if (filepathDatetimeParam !== undefined) {
|
|
76
|
-
filePathDatetime = filepathDatetimeParam;
|
|
77
|
-
}
|
|
78
|
-
else if (!!process.env.JET_LOGGER_FILEPATH_DATETIME) {
|
|
79
|
-
const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
|
|
80
|
-
filePathDatetime = envVar.toUpperCase() === 'TRUE';
|
|
81
|
-
}
|
|
82
|
-
// Timestamp
|
|
83
|
-
if (timestamp !== undefined) {
|
|
84
|
-
this.timestamp = timestamp;
|
|
85
|
-
}
|
|
86
|
-
else if (!!process.env.JET_LOGGER_TIMESTAMP) {
|
|
87
|
-
const envVar = process.env.JET_LOGGER_TIMESTAMP;
|
|
88
|
-
this.timestamp = envVar.toUpperCase() === 'TRUE';
|
|
89
|
-
}
|
|
90
|
-
// Format
|
|
91
|
-
if (format !== undefined) {
|
|
92
|
-
this.format = format;
|
|
93
|
-
}
|
|
94
|
-
else if (!!process.env.JET_LOGGER_FORMAT) {
|
|
95
|
-
this.format = process.env.JET_LOGGER_FORMAT.toUpperCase();
|
|
96
|
-
}
|
|
97
|
-
// Modify filepath if filepath datetime is true
|
|
98
|
-
if (filePathDatetime) {
|
|
99
|
-
this.filePath = this.addDatetimeToFileName(this.filePath);
|
|
100
|
-
}
|
|
101
|
-
// Custom Logger Function
|
|
102
|
-
if (customLogFn !== undefined) {
|
|
103
|
-
this.customLogFn = customLogFn;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Prepend the filename in the file path with a timestamp.
|
|
108
|
-
* i.e. '/home/jet-logger.log' => '/home/20220805T033709_jet-logger.log'
|
|
109
|
-
*/
|
|
110
|
-
addDatetimeToFileName(filePath) {
|
|
111
|
-
// Get the date string
|
|
112
|
-
const dateStr = new Date()
|
|
113
|
-
.toISOString()
|
|
114
|
-
.split('-')
|
|
115
|
-
.join('')
|
|
116
|
-
.split(':')
|
|
117
|
-
.join('')
|
|
118
|
-
.slice(0, 15);
|
|
119
|
-
// Setup new file name
|
|
120
|
-
const filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = dateStr + '_' + fileName;
|
|
121
|
-
// Setup new file path
|
|
122
|
-
filePathArr[lastIdx] = fileNameNew;
|
|
123
|
-
return filePathArr.join('/');
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Print information.
|
|
127
|
-
*/
|
|
128
|
-
info(content, printFull) {
|
|
129
|
-
this.printLog(content, !!printFull, Levels.Info);
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Print important information.
|
|
133
|
-
*/
|
|
134
|
-
imp(content, printFull) {
|
|
135
|
-
this.printLog(content, !!printFull, Levels.Important);
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Print important information.
|
|
139
|
-
*/
|
|
140
|
-
warn(content, printFull) {
|
|
141
|
-
this.printLog(content, !!printFull, Levels.Warning);
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Print important information.
|
|
145
|
-
*/
|
|
146
|
-
err(content, printFull) {
|
|
147
|
-
this.printLog(content, !!printFull, Levels.Error);
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Print the log using the provided settings.
|
|
151
|
-
*/
|
|
152
|
-
printLog(contentParam, printFull, level) {
|
|
153
|
-
// Do nothing if turned off
|
|
154
|
-
if (this.mode === LoggerModes.Off) {
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
// Print full
|
|
158
|
-
let content;
|
|
159
|
-
if (printFull) {
|
|
160
|
-
content = util.inspect(contentParam);
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
content = String(contentParam);
|
|
164
|
-
}
|
|
165
|
-
// Fire the custom logger if that's the option
|
|
166
|
-
if (this.mode === LoggerModes.Custom) {
|
|
167
|
-
if (!!this.customLogFn) {
|
|
168
|
-
return this.customLogFn(new Date(), level.Prefix, content);
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
throw Error(Errors.CustomLogFn);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
// Print line or json
|
|
175
|
-
if (this.format === Formats.Line) {
|
|
176
|
-
content = this.setupLineFormat(content, level);
|
|
177
|
-
}
|
|
178
|
-
else if (this.format === Formats.Json) {
|
|
179
|
-
content = this.setupJsonFormat(content, level);
|
|
180
|
-
}
|
|
181
|
-
// Print Console
|
|
182
|
-
if (this.mode === LoggerModes.Console) {
|
|
183
|
-
const colorFn = colors[level.Color];
|
|
184
|
-
// eslint-disable-next-line no-console
|
|
185
|
-
console.log(colorFn(content));
|
|
186
|
-
// Print File
|
|
187
|
-
}
|
|
188
|
-
else if (this.mode === LoggerModes.File) {
|
|
189
|
-
this.writeToFile(content + '\n');
|
|
190
|
-
// If reach this point, mode setting was bad
|
|
191
|
-
}
|
|
192
|
-
else {
|
|
193
|
-
throw Error(Errors.Mode);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Setup line format.
|
|
198
|
-
*/
|
|
199
|
-
setupLineFormat(content, level) {
|
|
200
|
-
// Format
|
|
201
|
-
content = level.Prefix + ': ' + content;
|
|
202
|
-
if (this.timestamp) {
|
|
203
|
-
const time = '[' + new Date().toISOString() + '] ';
|
|
204
|
-
return time + content;
|
|
205
|
-
}
|
|
206
|
-
// Return
|
|
207
|
-
return content;
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Setup json format.
|
|
211
|
-
*/
|
|
212
|
-
setupJsonFormat(content, level) {
|
|
213
|
-
// Format
|
|
214
|
-
const json = {
|
|
215
|
-
level: level.Prefix,
|
|
216
|
-
message: content,
|
|
217
|
-
};
|
|
218
|
-
if (this.timestamp) {
|
|
219
|
-
json.timestamp = new Date().toISOString();
|
|
220
|
-
}
|
|
221
|
-
// Return
|
|
222
|
-
return JSON.stringify(json);
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Write to file.
|
|
226
|
-
*/
|
|
227
|
-
writeToFile(content) {
|
|
228
|
-
fs.appendFile(this.filePath, content, (err) => {
|
|
229
|
-
if (!!err) {
|
|
230
|
-
// eslint-disable-next-line no-console
|
|
231
|
-
console.error(err);
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
/******************************************************************************
|
|
237
|
-
Export
|
|
238
|
-
******************************************************************************/
|
|
239
|
-
export default new JetLogger();
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/******************************************************************************
|
|
2
|
-
Variables
|
|
3
|
-
******************************************************************************/
|
|
4
|
-
export declare const LoggerModes: {
|
|
5
|
-
readonly Console: "CONSOLE";
|
|
6
|
-
readonly File: "FILE";
|
|
7
|
-
readonly Custom: "CUSTOM";
|
|
8
|
-
readonly Off: "OFF";
|
|
9
|
-
};
|
|
10
|
-
export declare const Formats: {
|
|
11
|
-
readonly Line: "LINE";
|
|
12
|
-
readonly Json: "JSON";
|
|
13
|
-
};
|
|
14
|
-
/******************************************************************************
|
|
15
|
-
Types
|
|
16
|
-
******************************************************************************/
|
|
17
|
-
type TLoggerModes = (typeof LoggerModes)[keyof typeof LoggerModes];
|
|
18
|
-
type TFormats = (typeof Formats)[keyof typeof Formats];
|
|
19
|
-
export type TCustomLoggerFunction = (timestamp: Date, prefix: string, content: unknown) => void;
|
|
20
|
-
/******************************************************************************
|
|
21
|
-
Classes
|
|
22
|
-
******************************************************************************/
|
|
23
|
-
export declare class JetLogger {
|
|
24
|
-
private mode;
|
|
25
|
-
private filePath;
|
|
26
|
-
private timestamp;
|
|
27
|
-
private format;
|
|
28
|
-
private customLogFn;
|
|
29
|
-
/**
|
|
30
|
-
* Constructor
|
|
31
|
-
*/
|
|
32
|
-
constructor(mode?: TLoggerModes, filepath?: string, filepathDatetimeParam?: boolean, timestamp?: boolean, format?: TFormats, customLogFn?: TCustomLoggerFunction);
|
|
33
|
-
/**
|
|
34
|
-
* Prepend the filename in the file path with a timestamp.
|
|
35
|
-
* i.e. '/home/jet-logger.log' => '/home/20220805T033709_jet-logger.log'
|
|
36
|
-
*/
|
|
37
|
-
private addDatetimeToFileName;
|
|
38
|
-
/**
|
|
39
|
-
* Print information.
|
|
40
|
-
*/
|
|
41
|
-
info(content: unknown, printFull?: boolean): void;
|
|
42
|
-
/**
|
|
43
|
-
* Print important information.
|
|
44
|
-
*/
|
|
45
|
-
imp(content: unknown, printFull?: boolean): void;
|
|
46
|
-
/**
|
|
47
|
-
* Print important information.
|
|
48
|
-
*/
|
|
49
|
-
warn(content: unknown, printFull?: boolean): void;
|
|
50
|
-
/**
|
|
51
|
-
* Print important information.
|
|
52
|
-
*/
|
|
53
|
-
err(content: unknown, printFull?: boolean): void;
|
|
54
|
-
/**
|
|
55
|
-
* Print the log using the provided settings.
|
|
56
|
-
*/
|
|
57
|
-
private printLog;
|
|
58
|
-
/**
|
|
59
|
-
* Setup line format.
|
|
60
|
-
*/
|
|
61
|
-
private setupLineFormat;
|
|
62
|
-
/**
|
|
63
|
-
* Setup json format.
|
|
64
|
-
*/
|
|
65
|
-
private setupJsonFormat;
|
|
66
|
-
/**
|
|
67
|
-
* Write to file.
|
|
68
|
-
*/
|
|
69
|
-
private writeToFile;
|
|
70
|
-
}
|
|
71
|
-
/******************************************************************************
|
|
72
|
-
Export
|
|
73
|
-
******************************************************************************/
|
|
74
|
-
declare const _default: JetLogger;
|
|
75
|
-
export default _default;
|