jet-logger 2.2.1 → 2.2.3
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/jetLogger.js +58 -68
- package/dist/esm/jetLogger.js +58 -68
- package/dist/types/jetLogger.d.ts +17 -31
- package/package.json +1 -1
package/dist/cjs/jetLogger.js
CHANGED
|
@@ -1,82 +1,78 @@
|
|
|
1
1
|
import colors from 'colors';
|
|
2
2
|
import fs from 'fs';
|
|
3
3
|
import util from 'util';
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
const Modes = {
|
|
5
|
+
CONSOLE: 'console',
|
|
6
|
+
FILE: 'file',
|
|
7
|
+
CUSTOM: 'custom',
|
|
8
|
+
OFF: 'off',
|
|
9
9
|
};
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
const Formats = {
|
|
11
|
+
LINE: 'line',
|
|
12
|
+
JSON: 'json',
|
|
13
13
|
};
|
|
14
|
-
const
|
|
14
|
+
const Levels = {
|
|
15
15
|
Info: {
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
COLOR: 'green',
|
|
17
|
+
PREFIX: 'INFO',
|
|
18
18
|
},
|
|
19
19
|
Important: {
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
COLOR: 'magenta',
|
|
21
|
+
PREFIX: 'IMPORTANT',
|
|
22
22
|
},
|
|
23
23
|
Warning: {
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
COLOR: 'yellow',
|
|
25
|
+
PREFIX: 'WARNING',
|
|
26
26
|
},
|
|
27
27
|
Error: {
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
COLOR: 'red',
|
|
29
|
+
PREFIX: 'ERROR',
|
|
30
30
|
},
|
|
31
31
|
};
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
const Defaults = {
|
|
33
|
+
MODE: Modes.CONSOLE,
|
|
34
|
+
FILE_PATH: 'jet-logger.log',
|
|
35
|
+
TIMESTAMP: true,
|
|
36
|
+
FORMAT: Formats.LINE,
|
|
37
|
+
CUSTOM_LOGGER_FUNCTION: () => ({}),
|
|
38
38
|
};
|
|
39
39
|
const Errors = {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
'"
|
|
40
|
+
CUSTOM_LOGGER: '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
43
|
};
|
|
44
44
|
export const JetLogger = {
|
|
45
|
-
Modes
|
|
46
|
-
Formats
|
|
47
|
-
instanceOf,
|
|
45
|
+
Modes,
|
|
46
|
+
Formats,
|
|
48
47
|
};
|
|
49
|
-
const kJetLogger = Symbol('k-jet-logger');
|
|
50
48
|
export function jetLogger(options) {
|
|
51
|
-
let mode =
|
|
49
|
+
let mode = Defaults.MODE, filePath = Defaults.FILE_PATH, timestamp = Defaults.TIMESTAMP, format = Defaults.FORMAT, customLogFn = Defaults.CUSTOM_LOGGER_FUNCTION;
|
|
52
50
|
if (options?.mode !== undefined) {
|
|
53
51
|
mode = options.mode;
|
|
54
52
|
}
|
|
55
53
|
else if (!!process.env.JET_LOGGER_MODE) {
|
|
56
|
-
mode = process.env.JET_LOGGER_MODE.
|
|
54
|
+
mode = process.env.JET_LOGGER_MODE.toLowerCase();
|
|
57
55
|
}
|
|
58
|
-
if (mode ===
|
|
56
|
+
if (mode === Modes.OFF) {
|
|
59
57
|
return {
|
|
60
58
|
info: (_, __) => ({}),
|
|
61
59
|
imp: (_, __) => ({}),
|
|
62
60
|
warn: (_, __) => ({}),
|
|
63
61
|
err: (_, __) => ({}),
|
|
64
|
-
[kJetLogger]: true,
|
|
65
62
|
};
|
|
66
63
|
}
|
|
67
|
-
if (mode ===
|
|
64
|
+
if (mode === Modes.CUSTOM) {
|
|
68
65
|
if (options?.customLogger !== undefined) {
|
|
69
66
|
customLogFn = options.customLogger;
|
|
70
67
|
}
|
|
71
68
|
if (!customLogFn) {
|
|
72
|
-
throw Error(Errors.
|
|
69
|
+
throw Error(Errors.CUSTOM_LOGGER);
|
|
73
70
|
}
|
|
74
71
|
return {
|
|
75
|
-
info: setupPrintWithCustomLogger(
|
|
76
|
-
imp: setupPrintWithCustomLogger(
|
|
77
|
-
warn: setupPrintWithCustomLogger(
|
|
78
|
-
err: setupPrintWithCustomLogger(
|
|
79
|
-
[kJetLogger]: true,
|
|
72
|
+
info: setupPrintWithCustomLogger(Levels.Info, customLogFn),
|
|
73
|
+
imp: setupPrintWithCustomLogger(Levels.Important, customLogFn),
|
|
74
|
+
warn: setupPrintWithCustomLogger(Levels.Warning, customLogFn),
|
|
75
|
+
err: setupPrintWithCustomLogger(Levels.Error, customLogFn),
|
|
80
76
|
};
|
|
81
77
|
}
|
|
82
78
|
if (options?.filepath !== undefined) {
|
|
@@ -90,47 +86,45 @@ export function jetLogger(options) {
|
|
|
90
86
|
}
|
|
91
87
|
else if (!!process.env.JET_LOGGER_TIMESTAMP) {
|
|
92
88
|
const envVar = process.env.JET_LOGGER_TIMESTAMP;
|
|
93
|
-
timestamp = envVar.
|
|
89
|
+
timestamp = envVar.toLowerCase() === 'true';
|
|
94
90
|
}
|
|
95
91
|
if (options?.format !== undefined) {
|
|
96
92
|
format = options.format;
|
|
97
93
|
}
|
|
98
94
|
else if (!!process.env.JET_LOGGER_FORMAT) {
|
|
99
|
-
format = process.env.JET_LOGGER_FORMAT.
|
|
95
|
+
format = process.env.JET_LOGGER_FORMAT.toLowerCase();
|
|
100
96
|
}
|
|
101
97
|
let formatter = () => '';
|
|
102
|
-
if (format ===
|
|
98
|
+
if (format === Formats.LINE) {
|
|
103
99
|
formatter = setupLineFormatter(timestamp);
|
|
104
100
|
}
|
|
105
|
-
else if (format ===
|
|
101
|
+
else if (format === Formats.JSON) {
|
|
106
102
|
formatter = setupJsonFormatter(timestamp);
|
|
107
103
|
}
|
|
108
|
-
if (mode ===
|
|
104
|
+
if (mode === Modes.FILE) {
|
|
109
105
|
let filePathDatetime = true;
|
|
110
106
|
if (options?.filepathDatetimeParam !== undefined) {
|
|
111
107
|
filePathDatetime = options.filepathDatetimeParam;
|
|
112
108
|
}
|
|
113
109
|
else if (!!process.env.JET_LOGGER_FILEPATH_DATETIME) {
|
|
114
110
|
const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
|
|
115
|
-
filePathDatetime = envVar.
|
|
111
|
+
filePathDatetime = envVar.toLowerCase() === 'true';
|
|
116
112
|
}
|
|
117
113
|
if (filePathDatetime) {
|
|
118
114
|
filePath = addDatetimeToFileName(filePath);
|
|
119
115
|
}
|
|
120
116
|
return {
|
|
121
|
-
info: setupPrintToFile(
|
|
122
|
-
imp: setupPrintToFile(
|
|
123
|
-
warn: setupPrintToFile(
|
|
124
|
-
err: setupPrintToFile(
|
|
125
|
-
[kJetLogger]: true,
|
|
117
|
+
info: setupPrintToFile(Levels.Info, formatter, filePath),
|
|
118
|
+
imp: setupPrintToFile(Levels.Important, formatter, filePath),
|
|
119
|
+
warn: setupPrintToFile(Levels.Warning, formatter, filePath),
|
|
120
|
+
err: setupPrintToFile(Levels.Error, formatter, filePath),
|
|
126
121
|
};
|
|
127
122
|
}
|
|
128
123
|
return {
|
|
129
|
-
info: setupPrintToConsole(
|
|
130
|
-
imp: setupPrintToConsole(
|
|
131
|
-
warn: setupPrintToConsole(
|
|
132
|
-
err: setupPrintToConsole(
|
|
133
|
-
[kJetLogger]: true,
|
|
124
|
+
info: setupPrintToConsole(Levels.Info, formatter),
|
|
125
|
+
imp: setupPrintToConsole(Levels.Important, formatter),
|
|
126
|
+
warn: setupPrintToConsole(Levels.Warning, formatter),
|
|
127
|
+
err: setupPrintToConsole(Levels.Error, formatter),
|
|
134
128
|
};
|
|
135
129
|
}
|
|
136
130
|
function setupPrintWithCustomLogger(level, customLogFn) {
|
|
@@ -142,19 +136,19 @@ function setupPrintWithCustomLogger(level, customLogFn) {
|
|
|
142
136
|
else {
|
|
143
137
|
contentNew = String(content);
|
|
144
138
|
}
|
|
145
|
-
return customLogFn(new Date(), level.
|
|
139
|
+
return customLogFn(new Date(), level.PREFIX, contentNew);
|
|
146
140
|
};
|
|
147
141
|
}
|
|
148
142
|
function setupLineFormatter(timestamp) {
|
|
149
143
|
if (timestamp) {
|
|
150
144
|
return (content, level) => {
|
|
151
|
-
const contentNew = level.
|
|
145
|
+
const contentNew = level.PREFIX + ': ' + content, time = '[' + new Date().toISOString() + '] ';
|
|
152
146
|
return time + contentNew;
|
|
153
147
|
};
|
|
154
148
|
}
|
|
155
149
|
else {
|
|
156
150
|
return (content, level) => {
|
|
157
|
-
return level.
|
|
151
|
+
return level.PREFIX + ': ' + content;
|
|
158
152
|
};
|
|
159
153
|
}
|
|
160
154
|
}
|
|
@@ -162,7 +156,7 @@ function setupJsonFormatter(timestamp) {
|
|
|
162
156
|
if (timestamp) {
|
|
163
157
|
return (content, level) => {
|
|
164
158
|
const json = {
|
|
165
|
-
level: level.
|
|
159
|
+
level: level.PREFIX,
|
|
166
160
|
message: content,
|
|
167
161
|
};
|
|
168
162
|
json.timestamp = new Date().toISOString();
|
|
@@ -172,7 +166,7 @@ function setupJsonFormatter(timestamp) {
|
|
|
172
166
|
else {
|
|
173
167
|
return (content, level) => {
|
|
174
168
|
const json = {
|
|
175
|
-
level: level.
|
|
169
|
+
level: level.PREFIX,
|
|
176
170
|
message: content,
|
|
177
171
|
};
|
|
178
172
|
return JSON.stringify(json);
|
|
@@ -189,7 +183,7 @@ function setupPrintToFile(level, formatter, filePath) {
|
|
|
189
183
|
contentNew = String(content);
|
|
190
184
|
}
|
|
191
185
|
contentNew = formatter(contentNew, level);
|
|
192
|
-
fs.appendFile(filePath, contentNew, (err) => {
|
|
186
|
+
fs.appendFile(filePath, contentNew + '\n', (err) => {
|
|
193
187
|
if (!!err) {
|
|
194
188
|
console.error(err);
|
|
195
189
|
}
|
|
@@ -205,7 +199,7 @@ function setupPrintToConsole(level, formatter) {
|
|
|
205
199
|
else {
|
|
206
200
|
contentNew = String(content);
|
|
207
201
|
}
|
|
208
|
-
const colorFn = colors[level.
|
|
202
|
+
const colorFn = colors[level.COLOR];
|
|
209
203
|
contentNew = formatter(contentNew, level);
|
|
210
204
|
console.log(colorFn(contentNew));
|
|
211
205
|
};
|
|
@@ -222,8 +216,4 @@ function addDatetimeToFileName(filePath) {
|
|
|
222
216
|
filePathArr[lastIdx] = fileNameNew;
|
|
223
217
|
return filePathArr.join('/');
|
|
224
218
|
}
|
|
225
|
-
function instanceOf(arg) {
|
|
226
|
-
return (typeof arg === 'object' &&
|
|
227
|
-
arg[kJetLogger] === true);
|
|
228
|
-
}
|
|
229
219
|
export default jetLogger();
|
package/dist/esm/jetLogger.js
CHANGED
|
@@ -1,82 +1,78 @@
|
|
|
1
1
|
import colors from 'colors';
|
|
2
2
|
import fs from 'fs';
|
|
3
3
|
import util from 'util';
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
const Modes = {
|
|
5
|
+
CONSOLE: 'console',
|
|
6
|
+
FILE: 'file',
|
|
7
|
+
CUSTOM: 'custom',
|
|
8
|
+
OFF: 'off',
|
|
9
9
|
};
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
const Formats = {
|
|
11
|
+
LINE: 'line',
|
|
12
|
+
JSON: 'json',
|
|
13
13
|
};
|
|
14
|
-
const
|
|
14
|
+
const Levels = {
|
|
15
15
|
Info: {
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
COLOR: 'green',
|
|
17
|
+
PREFIX: 'INFO',
|
|
18
18
|
},
|
|
19
19
|
Important: {
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
COLOR: 'magenta',
|
|
21
|
+
PREFIX: 'IMPORTANT',
|
|
22
22
|
},
|
|
23
23
|
Warning: {
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
COLOR: 'yellow',
|
|
25
|
+
PREFIX: 'WARNING',
|
|
26
26
|
},
|
|
27
27
|
Error: {
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
COLOR: 'red',
|
|
29
|
+
PREFIX: 'ERROR',
|
|
30
30
|
},
|
|
31
31
|
};
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
const Defaults = {
|
|
33
|
+
MODE: Modes.CONSOLE,
|
|
34
|
+
FILE_PATH: 'jet-logger.log',
|
|
35
|
+
TIMESTAMP: true,
|
|
36
|
+
FORMAT: Formats.LINE,
|
|
37
|
+
CUSTOM_LOGGER_FUNCTION: () => ({}),
|
|
38
38
|
};
|
|
39
39
|
const Errors = {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
'"
|
|
40
|
+
CUSTOM_LOGGER: '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
43
|
};
|
|
44
44
|
export const JetLogger = {
|
|
45
|
-
Modes
|
|
46
|
-
Formats
|
|
47
|
-
instanceOf,
|
|
45
|
+
Modes,
|
|
46
|
+
Formats,
|
|
48
47
|
};
|
|
49
|
-
const kJetLogger = Symbol('k-jet-logger');
|
|
50
48
|
export function jetLogger(options) {
|
|
51
|
-
let mode =
|
|
49
|
+
let mode = Defaults.MODE, filePath = Defaults.FILE_PATH, timestamp = Defaults.TIMESTAMP, format = Defaults.FORMAT, customLogFn = Defaults.CUSTOM_LOGGER_FUNCTION;
|
|
52
50
|
if (options?.mode !== undefined) {
|
|
53
51
|
mode = options.mode;
|
|
54
52
|
}
|
|
55
53
|
else if (!!process.env.JET_LOGGER_MODE) {
|
|
56
|
-
mode = process.env.JET_LOGGER_MODE.
|
|
54
|
+
mode = process.env.JET_LOGGER_MODE.toLowerCase();
|
|
57
55
|
}
|
|
58
|
-
if (mode ===
|
|
56
|
+
if (mode === Modes.OFF) {
|
|
59
57
|
return {
|
|
60
58
|
info: (_, __) => ({}),
|
|
61
59
|
imp: (_, __) => ({}),
|
|
62
60
|
warn: (_, __) => ({}),
|
|
63
61
|
err: (_, __) => ({}),
|
|
64
|
-
[kJetLogger]: true,
|
|
65
62
|
};
|
|
66
63
|
}
|
|
67
|
-
if (mode ===
|
|
64
|
+
if (mode === Modes.CUSTOM) {
|
|
68
65
|
if (options?.customLogger !== undefined) {
|
|
69
66
|
customLogFn = options.customLogger;
|
|
70
67
|
}
|
|
71
68
|
if (!customLogFn) {
|
|
72
|
-
throw Error(Errors.
|
|
69
|
+
throw Error(Errors.CUSTOM_LOGGER);
|
|
73
70
|
}
|
|
74
71
|
return {
|
|
75
|
-
info: setupPrintWithCustomLogger(
|
|
76
|
-
imp: setupPrintWithCustomLogger(
|
|
77
|
-
warn: setupPrintWithCustomLogger(
|
|
78
|
-
err: setupPrintWithCustomLogger(
|
|
79
|
-
[kJetLogger]: true,
|
|
72
|
+
info: setupPrintWithCustomLogger(Levels.Info, customLogFn),
|
|
73
|
+
imp: setupPrintWithCustomLogger(Levels.Important, customLogFn),
|
|
74
|
+
warn: setupPrintWithCustomLogger(Levels.Warning, customLogFn),
|
|
75
|
+
err: setupPrintWithCustomLogger(Levels.Error, customLogFn),
|
|
80
76
|
};
|
|
81
77
|
}
|
|
82
78
|
if (options?.filepath !== undefined) {
|
|
@@ -90,47 +86,45 @@ export function jetLogger(options) {
|
|
|
90
86
|
}
|
|
91
87
|
else if (!!process.env.JET_LOGGER_TIMESTAMP) {
|
|
92
88
|
const envVar = process.env.JET_LOGGER_TIMESTAMP;
|
|
93
|
-
timestamp = envVar.
|
|
89
|
+
timestamp = envVar.toLowerCase() === 'true';
|
|
94
90
|
}
|
|
95
91
|
if (options?.format !== undefined) {
|
|
96
92
|
format = options.format;
|
|
97
93
|
}
|
|
98
94
|
else if (!!process.env.JET_LOGGER_FORMAT) {
|
|
99
|
-
format = process.env.JET_LOGGER_FORMAT.
|
|
95
|
+
format = process.env.JET_LOGGER_FORMAT.toLowerCase();
|
|
100
96
|
}
|
|
101
97
|
let formatter = () => '';
|
|
102
|
-
if (format ===
|
|
98
|
+
if (format === Formats.LINE) {
|
|
103
99
|
formatter = setupLineFormatter(timestamp);
|
|
104
100
|
}
|
|
105
|
-
else if (format ===
|
|
101
|
+
else if (format === Formats.JSON) {
|
|
106
102
|
formatter = setupJsonFormatter(timestamp);
|
|
107
103
|
}
|
|
108
|
-
if (mode ===
|
|
104
|
+
if (mode === Modes.FILE) {
|
|
109
105
|
let filePathDatetime = true;
|
|
110
106
|
if (options?.filepathDatetimeParam !== undefined) {
|
|
111
107
|
filePathDatetime = options.filepathDatetimeParam;
|
|
112
108
|
}
|
|
113
109
|
else if (!!process.env.JET_LOGGER_FILEPATH_DATETIME) {
|
|
114
110
|
const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
|
|
115
|
-
filePathDatetime = envVar.
|
|
111
|
+
filePathDatetime = envVar.toLowerCase() === 'true';
|
|
116
112
|
}
|
|
117
113
|
if (filePathDatetime) {
|
|
118
114
|
filePath = addDatetimeToFileName(filePath);
|
|
119
115
|
}
|
|
120
116
|
return {
|
|
121
|
-
info: setupPrintToFile(
|
|
122
|
-
imp: setupPrintToFile(
|
|
123
|
-
warn: setupPrintToFile(
|
|
124
|
-
err: setupPrintToFile(
|
|
125
|
-
[kJetLogger]: true,
|
|
117
|
+
info: setupPrintToFile(Levels.Info, formatter, filePath),
|
|
118
|
+
imp: setupPrintToFile(Levels.Important, formatter, filePath),
|
|
119
|
+
warn: setupPrintToFile(Levels.Warning, formatter, filePath),
|
|
120
|
+
err: setupPrintToFile(Levels.Error, formatter, filePath),
|
|
126
121
|
};
|
|
127
122
|
}
|
|
128
123
|
return {
|
|
129
|
-
info: setupPrintToConsole(
|
|
130
|
-
imp: setupPrintToConsole(
|
|
131
|
-
warn: setupPrintToConsole(
|
|
132
|
-
err: setupPrintToConsole(
|
|
133
|
-
[kJetLogger]: true,
|
|
124
|
+
info: setupPrintToConsole(Levels.Info, formatter),
|
|
125
|
+
imp: setupPrintToConsole(Levels.Important, formatter),
|
|
126
|
+
warn: setupPrintToConsole(Levels.Warning, formatter),
|
|
127
|
+
err: setupPrintToConsole(Levels.Error, formatter),
|
|
134
128
|
};
|
|
135
129
|
}
|
|
136
130
|
function setupPrintWithCustomLogger(level, customLogFn) {
|
|
@@ -142,19 +136,19 @@ function setupPrintWithCustomLogger(level, customLogFn) {
|
|
|
142
136
|
else {
|
|
143
137
|
contentNew = String(content);
|
|
144
138
|
}
|
|
145
|
-
return customLogFn(new Date(), level.
|
|
139
|
+
return customLogFn(new Date(), level.PREFIX, contentNew);
|
|
146
140
|
};
|
|
147
141
|
}
|
|
148
142
|
function setupLineFormatter(timestamp) {
|
|
149
143
|
if (timestamp) {
|
|
150
144
|
return (content, level) => {
|
|
151
|
-
const contentNew = level.
|
|
145
|
+
const contentNew = level.PREFIX + ': ' + content, time = '[' + new Date().toISOString() + '] ';
|
|
152
146
|
return time + contentNew;
|
|
153
147
|
};
|
|
154
148
|
}
|
|
155
149
|
else {
|
|
156
150
|
return (content, level) => {
|
|
157
|
-
return level.
|
|
151
|
+
return level.PREFIX + ': ' + content;
|
|
158
152
|
};
|
|
159
153
|
}
|
|
160
154
|
}
|
|
@@ -162,7 +156,7 @@ function setupJsonFormatter(timestamp) {
|
|
|
162
156
|
if (timestamp) {
|
|
163
157
|
return (content, level) => {
|
|
164
158
|
const json = {
|
|
165
|
-
level: level.
|
|
159
|
+
level: level.PREFIX,
|
|
166
160
|
message: content,
|
|
167
161
|
};
|
|
168
162
|
json.timestamp = new Date().toISOString();
|
|
@@ -172,7 +166,7 @@ function setupJsonFormatter(timestamp) {
|
|
|
172
166
|
else {
|
|
173
167
|
return (content, level) => {
|
|
174
168
|
const json = {
|
|
175
|
-
level: level.
|
|
169
|
+
level: level.PREFIX,
|
|
176
170
|
message: content,
|
|
177
171
|
};
|
|
178
172
|
return JSON.stringify(json);
|
|
@@ -189,7 +183,7 @@ function setupPrintToFile(level, formatter, filePath) {
|
|
|
189
183
|
contentNew = String(content);
|
|
190
184
|
}
|
|
191
185
|
contentNew = formatter(contentNew, level);
|
|
192
|
-
fs.appendFile(filePath, contentNew, (err) => {
|
|
186
|
+
fs.appendFile(filePath, contentNew + '\n', (err) => {
|
|
193
187
|
if (!!err) {
|
|
194
188
|
console.error(err);
|
|
195
189
|
}
|
|
@@ -205,7 +199,7 @@ function setupPrintToConsole(level, formatter) {
|
|
|
205
199
|
else {
|
|
206
200
|
contentNew = String(content);
|
|
207
201
|
}
|
|
208
|
-
const colorFn = colors[level.
|
|
202
|
+
const colorFn = colors[level.COLOR];
|
|
209
203
|
contentNew = formatter(contentNew, level);
|
|
210
204
|
console.log(colorFn(contentNew));
|
|
211
205
|
};
|
|
@@ -222,8 +216,4 @@ function addDatetimeToFileName(filePath) {
|
|
|
222
216
|
filePathArr[lastIdx] = fileNameNew;
|
|
223
217
|
return filePathArr.join('/');
|
|
224
218
|
}
|
|
225
|
-
function instanceOf(arg) {
|
|
226
|
-
return (typeof arg === 'object' &&
|
|
227
|
-
arg[kJetLogger] === true);
|
|
228
|
-
}
|
|
229
219
|
export default jetLogger();
|
|
@@ -1,51 +1,43 @@
|
|
|
1
1
|
/******************************************************************************
|
|
2
2
|
Constants
|
|
3
3
|
******************************************************************************/
|
|
4
|
-
declare const
|
|
5
|
-
readonly
|
|
6
|
-
readonly
|
|
7
|
-
readonly
|
|
8
|
-
readonly
|
|
4
|
+
declare const Modes: {
|
|
5
|
+
readonly CONSOLE: "console";
|
|
6
|
+
readonly FILE: "file";
|
|
7
|
+
readonly CUSTOM: "custom";
|
|
8
|
+
readonly OFF: "off";
|
|
9
9
|
};
|
|
10
|
-
declare const
|
|
11
|
-
readonly
|
|
12
|
-
readonly
|
|
10
|
+
declare const Formats: {
|
|
11
|
+
readonly LINE: "line";
|
|
12
|
+
readonly JSON: "json";
|
|
13
13
|
};
|
|
14
14
|
export declare const JetLogger: {
|
|
15
15
|
readonly Modes: {
|
|
16
|
-
readonly
|
|
17
|
-
readonly
|
|
18
|
-
readonly
|
|
19
|
-
readonly
|
|
16
|
+
readonly CONSOLE: "console";
|
|
17
|
+
readonly FILE: "file";
|
|
18
|
+
readonly CUSTOM: "custom";
|
|
19
|
+
readonly OFF: "off";
|
|
20
20
|
};
|
|
21
21
|
readonly Formats: {
|
|
22
|
-
readonly
|
|
23
|
-
readonly
|
|
22
|
+
readonly LINE: "line";
|
|
23
|
+
readonly JSON: "json";
|
|
24
24
|
};
|
|
25
|
-
readonly instanceOf: typeof instanceOf;
|
|
26
25
|
};
|
|
27
|
-
declare const kJetLogger: unique symbol;
|
|
28
26
|
/******************************************************************************
|
|
29
27
|
Types
|
|
30
28
|
******************************************************************************/
|
|
31
|
-
type
|
|
32
|
-
type Formats = (typeof
|
|
29
|
+
type Modes = (typeof Modes)[keyof typeof Modes];
|
|
30
|
+
type Formats = (typeof Formats)[keyof typeof Formats];
|
|
33
31
|
type LogFunction = (content: unknown, printFull?: boolean) => void;
|
|
34
32
|
export type CustomLogger = (timestamp: Date, prefix: string, content: unknown) => void;
|
|
35
33
|
interface Options {
|
|
36
|
-
mode?:
|
|
34
|
+
mode?: Modes;
|
|
37
35
|
filepath?: string;
|
|
38
36
|
filepathDatetimeParam?: boolean;
|
|
39
37
|
timestamp?: boolean;
|
|
40
38
|
format?: Formats;
|
|
41
39
|
customLogger?: CustomLogger;
|
|
42
40
|
}
|
|
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
41
|
/******************************************************************************
|
|
50
42
|
Functions
|
|
51
43
|
******************************************************************************/
|
|
@@ -57,12 +49,7 @@ export declare function jetLogger(options?: Options): {
|
|
|
57
49
|
readonly imp: LogFunction;
|
|
58
50
|
readonly warn: LogFunction;
|
|
59
51
|
readonly err: LogFunction;
|
|
60
|
-
readonly [kJetLogger]: true;
|
|
61
52
|
};
|
|
62
|
-
/**
|
|
63
|
-
* Check if an object is an instance of jetLogger
|
|
64
|
-
*/
|
|
65
|
-
declare function instanceOf(arg: unknown): arg is JetLogger;
|
|
66
53
|
/******************************************************************************
|
|
67
54
|
Export
|
|
68
55
|
******************************************************************************/
|
|
@@ -71,6 +58,5 @@ declare const _default: {
|
|
|
71
58
|
readonly imp: LogFunction;
|
|
72
59
|
readonly warn: LogFunction;
|
|
73
60
|
readonly err: LogFunction;
|
|
74
|
-
readonly [kJetLogger]: true;
|
|
75
61
|
};
|
|
76
62
|
export default _default;
|