jet-logger 1.1.3 → 1.2.0
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/README.md +3 -2
- package/lib/index.d.ts +13 -33
- package/lib/index.js +44 -26
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,7 +21,8 @@ take priority over environment variables. Note that file writes happen asynchron
|
|
|
21
21
|
|
|
22
22
|
- The four environment variables are:
|
|
23
23
|
- `JET_LOGGER_MODE`: can be `'CONSOLE'`(default), `'FILE'`, `'CUSTOM'`, and `'OFF'`.
|
|
24
|
-
- `JET_LOGGER_FILEPATH`: the file-path for file mode. Default is _home_dir/jet-logger.log_
|
|
24
|
+
- `JET_LOGGER_FILEPATH`: the file-path for file mode. Default is `_home_dir/jet-logger.log_`.
|
|
25
|
+
- `JET_LOGGER_FILEPATH_DATETIME`: prepend the log file name with the datetime. an be `'TRUE'` (default) or `'FALSE'`.
|
|
25
26
|
- `JET_LOGGER_TIMESTAMP`: adds a timestamp next to each log. Can be `'TRUE'` (default) or `'FALSE'`.
|
|
26
27
|
- `JET_LOGGER_FORMAT`: formats log as a line or JSON object. Can be `'LINE'` (default) or `'JSON'`.
|
|
27
28
|
|
|
@@ -114,7 +115,7 @@ const customSend: TCustomLogger = (timestamp: Date, level: string, content: any)
|
|
|
114
115
|
}
|
|
115
116
|
|
|
116
117
|
router.get('api/users', async (req: Request, res: Reponse) => {
|
|
117
|
-
const logger = JetLogger(LoggerModes.CUSTOM, '', true, customSend);
|
|
118
|
+
const logger = JetLogger(LoggerModes.CUSTOM, '', true, true, undefined, customSend);
|
|
118
119
|
logger.rmTimestamp = true;
|
|
119
120
|
logger.info(req.params.msg);
|
|
120
121
|
return res.status(OK).json({
|
package/lib/index.d.ts
CHANGED
|
@@ -8,28 +8,13 @@ export declare enum Formats {
|
|
|
8
8
|
Line = "LINE",
|
|
9
9
|
Json = "JSON"
|
|
10
10
|
}
|
|
11
|
-
declare
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
prefix: string;
|
|
15
|
-
};
|
|
16
|
-
imp: {
|
|
17
|
-
color: string;
|
|
18
|
-
prefix: string;
|
|
19
|
-
};
|
|
20
|
-
warn: {
|
|
21
|
-
color: string;
|
|
22
|
-
prefix: string;
|
|
23
|
-
};
|
|
24
|
-
err: {
|
|
25
|
-
color: string;
|
|
26
|
-
prefix: string;
|
|
27
|
-
};
|
|
28
|
-
};
|
|
29
|
-
declare const _default: {
|
|
11
|
+
declare type TJetLogger = ReturnType<typeof JetLogger>;
|
|
12
|
+
export declare type TCustomLogger = (timestamp: Date, prefix: string, content: any) => void;
|
|
13
|
+
export declare function JetLogger(mode?: LoggerModes, filepath?: string, filepathDatetime?: boolean, timestamp?: boolean, format?: Formats, customLogger?: TCustomLogger): {
|
|
30
14
|
readonly settings: {
|
|
31
15
|
readonly mode: LoggerModes;
|
|
32
|
-
readonly
|
|
16
|
+
readonly filepath: string;
|
|
17
|
+
readonly filepathDatetime: boolean;
|
|
33
18
|
readonly timestamp: boolean;
|
|
34
19
|
readonly format: Formats;
|
|
35
20
|
readonly customLogger: TCustomLogger | undefined;
|
|
@@ -38,16 +23,16 @@ declare const _default: {
|
|
|
38
23
|
readonly imp: typeof imp;
|
|
39
24
|
readonly warn: typeof warn;
|
|
40
25
|
readonly err: typeof err;
|
|
41
|
-
readonly printLog: typeof printLog;
|
|
42
26
|
};
|
|
43
|
-
|
|
44
|
-
declare
|
|
45
|
-
declare
|
|
46
|
-
|
|
47
|
-
|
|
27
|
+
declare function info(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
28
|
+
declare function imp(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
29
|
+
declare function warn(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
30
|
+
declare function err(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
31
|
+
declare const _default: {
|
|
48
32
|
readonly settings: {
|
|
49
33
|
readonly mode: LoggerModes;
|
|
50
|
-
readonly
|
|
34
|
+
readonly filepath: string;
|
|
35
|
+
readonly filepathDatetime: boolean;
|
|
51
36
|
readonly timestamp: boolean;
|
|
52
37
|
readonly format: Formats;
|
|
53
38
|
readonly customLogger: TCustomLogger | undefined;
|
|
@@ -56,10 +41,5 @@ export declare function JetLogger(mode?: LoggerModes, filePath?: string, timesta
|
|
|
56
41
|
readonly imp: typeof imp;
|
|
57
42
|
readonly warn: typeof warn;
|
|
58
43
|
readonly err: typeof err;
|
|
59
|
-
readonly printLog: typeof printLog;
|
|
60
44
|
};
|
|
61
|
-
|
|
62
|
-
declare function imp(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
63
|
-
declare function warn(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
64
|
-
declare function err(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
65
|
-
declare function printLog(this: TJetLogger, content: any, printFull: boolean, level: TLevelProp): void;
|
|
45
|
+
export default _default;
|
package/lib/index.js
CHANGED
|
@@ -43,24 +43,18 @@ var errors = {
|
|
|
43
43
|
'"OFF", or "CONSOLE".'
|
|
44
44
|
};
|
|
45
45
|
var defaults = {
|
|
46
|
-
|
|
46
|
+
filepath: 'jet-logger.log',
|
|
47
47
|
mode: LoggerModes.Console,
|
|
48
48
|
timestamp: true,
|
|
49
|
+
filepathDatetime: true,
|
|
49
50
|
format: Formats.Line,
|
|
50
51
|
};
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return {
|
|
54
|
-
settings: getSettings(mode, filePath, timestamp, format, customLogger),
|
|
55
|
-
info: info,
|
|
56
|
-
imp: imp,
|
|
57
|
-
warn: warn,
|
|
58
|
-
err: err,
|
|
59
|
-
printLog: printLog,
|
|
60
|
-
};
|
|
52
|
+
function JetLogger(mode, filepath, filepathDatetime, timestamp, format, customLogger) {
|
|
53
|
+
var settings = getSettings(mode, filepath, timestamp, filepathDatetime, format, customLogger);
|
|
54
|
+
return { settings: settings, info: info, imp: imp, warn: warn, err: err };
|
|
61
55
|
}
|
|
62
56
|
exports.JetLogger = JetLogger;
|
|
63
|
-
function getSettings(mode,
|
|
57
|
+
function getSettings(mode, filepath, filepathDatetime, timestamp, format, customLogger) {
|
|
64
58
|
if (!mode) {
|
|
65
59
|
if (!!process.env.JET_LOGGER_MODE) {
|
|
66
60
|
mode = process.env.JET_LOGGER_MODE.toUpperCase();
|
|
@@ -69,15 +63,24 @@ function getSettings(mode, filePath, timestamp, format, customLogger) {
|
|
|
69
63
|
mode = defaults.mode;
|
|
70
64
|
}
|
|
71
65
|
}
|
|
72
|
-
if (!
|
|
66
|
+
if (!filepath) {
|
|
73
67
|
if (!!process.env.JET_LOGGER_FILEPATH) {
|
|
74
|
-
|
|
68
|
+
filepath = process.env.JET_LOGGER_FILEPATH;
|
|
75
69
|
}
|
|
76
70
|
else {
|
|
77
|
-
|
|
71
|
+
filepath = defaults.filepath;
|
|
78
72
|
}
|
|
79
73
|
}
|
|
80
|
-
if (
|
|
74
|
+
if (filepathDatetime === undefined) {
|
|
75
|
+
if (!!process.env.JET_LOGGER_FILEPATH_DATETIME) {
|
|
76
|
+
filepathDatetime = (process.env.JET_LOGGER_FILEPATH_DATETIME.toUpperCase() ===
|
|
77
|
+
'TRUE');
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
filepathDatetime = defaults.filepathDatetime;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (timestamp === undefined) {
|
|
81
84
|
if (!!process.env.JET_LOGGER_TIMESTAMP) {
|
|
82
85
|
timestamp = (process.env.JET_LOGGER_TIMESTAMP.toUpperCase() === 'TRUE');
|
|
83
86
|
}
|
|
@@ -95,26 +98,27 @@ function getSettings(mode, filePath, timestamp, format, customLogger) {
|
|
|
95
98
|
}
|
|
96
99
|
return {
|
|
97
100
|
mode: mode,
|
|
98
|
-
|
|
101
|
+
filepath: filepath,
|
|
102
|
+
filepathDatetime: filepathDatetime,
|
|
99
103
|
timestamp: timestamp,
|
|
100
104
|
format: format,
|
|
101
105
|
customLogger: customLogger,
|
|
102
106
|
};
|
|
103
107
|
}
|
|
104
108
|
function info(content, printFull) {
|
|
105
|
-
return
|
|
109
|
+
return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.info, this.settings);
|
|
106
110
|
}
|
|
107
111
|
function imp(content, printFull) {
|
|
108
|
-
return
|
|
112
|
+
return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.imp, this.settings);
|
|
109
113
|
}
|
|
110
114
|
function warn(content, printFull) {
|
|
111
|
-
return
|
|
115
|
+
return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.warn, this.settings);
|
|
112
116
|
}
|
|
113
117
|
function err(content, printFull) {
|
|
114
|
-
return
|
|
118
|
+
return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.err, this.settings);
|
|
115
119
|
}
|
|
116
|
-
function printLog(content, printFull, level) {
|
|
117
|
-
var
|
|
120
|
+
function printLog(content, printFull, level, settings) {
|
|
121
|
+
var mode = settings.mode, format = settings.format, timestamp = settings.timestamp, filepath = settings.filepath, filepathDatetime = settings.filepathDatetime, customLogger = settings.customLogger;
|
|
118
122
|
if (mode === LoggerModes.Off) {
|
|
119
123
|
return;
|
|
120
124
|
}
|
|
@@ -150,9 +154,12 @@ function printLog(content, printFull, level) {
|
|
|
150
154
|
console.log(colorFn(content));
|
|
151
155
|
}
|
|
152
156
|
else if (mode === LoggerModes.File) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
157
|
+
var filePathFinal = filepath;
|
|
158
|
+
if (filepathDatetime) {
|
|
159
|
+
filePathFinal = addDatetimeToFileName(filepath);
|
|
160
|
+
}
|
|
161
|
+
writeToFile(content + '\n', filePathFinal)
|
|
162
|
+
.catch(function (err) { return console.log(err); });
|
|
156
163
|
}
|
|
157
164
|
else if (mode === LoggerModes.Custom) {
|
|
158
165
|
if (!!customLogger) {
|
|
@@ -166,6 +173,16 @@ function printLog(content, printFull, level) {
|
|
|
166
173
|
throw Error(errors.modeErr);
|
|
167
174
|
}
|
|
168
175
|
}
|
|
176
|
+
function addDatetimeToFileName(filePath) {
|
|
177
|
+
var dateStr = new Date().toISOString()
|
|
178
|
+
.split('-').join('')
|
|
179
|
+
.split(':').join('')
|
|
180
|
+
.slice(0, 15);
|
|
181
|
+
var filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx];
|
|
182
|
+
var fileNameNew = (dateStr + '_' + fileName);
|
|
183
|
+
filePathArr[lastIdx] = fileNameNew;
|
|
184
|
+
return filePathArr.join('/');
|
|
185
|
+
}
|
|
169
186
|
function writeToFile(content, filePath) {
|
|
170
187
|
return new Promise(function (res, rej) {
|
|
171
188
|
return fs_1.default.appendFile(filePath, content, function (err) {
|
|
@@ -173,3 +190,4 @@ function writeToFile(content, filePath) {
|
|
|
173
190
|
});
|
|
174
191
|
});
|
|
175
192
|
}
|
|
193
|
+
exports.default = JetLogger();
|