jet-logger 1.1.4 → 1.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/README.md +3 -2
- package/lib/index.d.ts +5 -25
- package/lib/index.js +42 -25
- 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. Can 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,31 +8,13 @@ export declare enum Formats {
|
|
|
8
8
|
Line = "LINE",
|
|
9
9
|
Json = "JSON"
|
|
10
10
|
}
|
|
11
|
-
declare const levels: {
|
|
12
|
-
info: {
|
|
13
|
-
color: string;
|
|
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 type TLevelProp = typeof levels[keyof typeof levels];
|
|
30
11
|
declare type TJetLogger = ReturnType<typeof JetLogger>;
|
|
31
12
|
export declare type TCustomLogger = (timestamp: Date, prefix: string, content: any) => void;
|
|
32
|
-
export declare function JetLogger(mode?: LoggerModes,
|
|
13
|
+
export declare function JetLogger(mode?: LoggerModes, filepath?: string, filepathDatetime?: boolean, timestamp?: boolean, format?: Formats, customLogger?: TCustomLogger): {
|
|
33
14
|
readonly settings: {
|
|
34
15
|
readonly mode: LoggerModes;
|
|
35
|
-
readonly
|
|
16
|
+
readonly filepath: string;
|
|
17
|
+
readonly filepathDatetime: boolean;
|
|
36
18
|
readonly timestamp: boolean;
|
|
37
19
|
readonly format: Formats;
|
|
38
20
|
readonly customLogger: TCustomLogger | undefined;
|
|
@@ -41,17 +23,16 @@ export declare function JetLogger(mode?: LoggerModes, filePath?: string, timesta
|
|
|
41
23
|
readonly imp: typeof imp;
|
|
42
24
|
readonly warn: typeof warn;
|
|
43
25
|
readonly err: typeof err;
|
|
44
|
-
readonly printLog: typeof printLog;
|
|
45
26
|
};
|
|
46
27
|
declare function info(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
47
28
|
declare function imp(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
48
29
|
declare function warn(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
49
30
|
declare function err(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
50
|
-
declare function printLog(this: TJetLogger, content: any, printFull: boolean, level: TLevelProp): void;
|
|
51
31
|
declare const _default: {
|
|
52
32
|
readonly settings: {
|
|
53
33
|
readonly mode: LoggerModes;
|
|
54
|
-
readonly
|
|
34
|
+
readonly filepath: string;
|
|
35
|
+
readonly filepathDatetime: boolean;
|
|
55
36
|
readonly timestamp: boolean;
|
|
56
37
|
readonly format: Formats;
|
|
57
38
|
readonly customLogger: TCustomLogger | undefined;
|
|
@@ -60,6 +41,5 @@ declare const _default: {
|
|
|
60
41
|
readonly imp: typeof imp;
|
|
61
42
|
readonly warn: typeof warn;
|
|
62
43
|
readonly err: typeof err;
|
|
63
|
-
readonly printLog: typeof printLog;
|
|
64
44
|
};
|
|
65
45
|
export default _default;
|
package/lib/index.js
CHANGED
|
@@ -43,23 +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
|
-
function JetLogger(mode,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
info: info,
|
|
55
|
-
imp: imp,
|
|
56
|
-
warn: warn,
|
|
57
|
-
err: err,
|
|
58
|
-
printLog: printLog,
|
|
59
|
-
};
|
|
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 };
|
|
60
55
|
}
|
|
61
56
|
exports.JetLogger = JetLogger;
|
|
62
|
-
function getSettings(mode,
|
|
57
|
+
function getSettings(mode, filepath, filepathDatetime, timestamp, format, customLogger) {
|
|
63
58
|
if (!mode) {
|
|
64
59
|
if (!!process.env.JET_LOGGER_MODE) {
|
|
65
60
|
mode = process.env.JET_LOGGER_MODE.toUpperCase();
|
|
@@ -68,15 +63,24 @@ function getSettings(mode, filePath, timestamp, format, customLogger) {
|
|
|
68
63
|
mode = defaults.mode;
|
|
69
64
|
}
|
|
70
65
|
}
|
|
71
|
-
if (!
|
|
66
|
+
if (!filepath) {
|
|
72
67
|
if (!!process.env.JET_LOGGER_FILEPATH) {
|
|
73
|
-
|
|
68
|
+
filepath = process.env.JET_LOGGER_FILEPATH;
|
|
74
69
|
}
|
|
75
70
|
else {
|
|
76
|
-
|
|
71
|
+
filepath = defaults.filepath;
|
|
77
72
|
}
|
|
78
73
|
}
|
|
79
|
-
if (
|
|
74
|
+
if (filepathDatetime === undefined || filepathDatetime === null) {
|
|
75
|
+
var envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
|
|
76
|
+
if (!!envVar) {
|
|
77
|
+
filepathDatetime = (envVar.toUpperCase() === 'TRUE');
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
filepathDatetime = defaults.filepathDatetime;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (timestamp === undefined || timestamp === null) {
|
|
80
84
|
if (!!process.env.JET_LOGGER_TIMESTAMP) {
|
|
81
85
|
timestamp = (process.env.JET_LOGGER_TIMESTAMP.toUpperCase() === 'TRUE');
|
|
82
86
|
}
|
|
@@ -94,26 +98,27 @@ function getSettings(mode, filePath, timestamp, format, customLogger) {
|
|
|
94
98
|
}
|
|
95
99
|
return {
|
|
96
100
|
mode: mode,
|
|
97
|
-
|
|
101
|
+
filepath: filepath,
|
|
102
|
+
filepathDatetime: filepathDatetime,
|
|
98
103
|
timestamp: timestamp,
|
|
99
104
|
format: format,
|
|
100
105
|
customLogger: customLogger,
|
|
101
106
|
};
|
|
102
107
|
}
|
|
103
108
|
function info(content, printFull) {
|
|
104
|
-
return
|
|
109
|
+
return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.info, this.settings);
|
|
105
110
|
}
|
|
106
111
|
function imp(content, printFull) {
|
|
107
|
-
return
|
|
112
|
+
return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.imp, this.settings);
|
|
108
113
|
}
|
|
109
114
|
function warn(content, printFull) {
|
|
110
|
-
return
|
|
115
|
+
return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.warn, this.settings);
|
|
111
116
|
}
|
|
112
117
|
function err(content, printFull) {
|
|
113
|
-
return
|
|
118
|
+
return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.err, this.settings);
|
|
114
119
|
}
|
|
115
|
-
function printLog(content, printFull, level) {
|
|
116
|
-
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;
|
|
117
122
|
if (mode === LoggerModes.Off) {
|
|
118
123
|
return;
|
|
119
124
|
}
|
|
@@ -149,9 +154,12 @@ function printLog(content, printFull, level) {
|
|
|
149
154
|
console.log(colorFn(content));
|
|
150
155
|
}
|
|
151
156
|
else if (mode === LoggerModes.File) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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); });
|
|
155
163
|
}
|
|
156
164
|
else if (mode === LoggerModes.Custom) {
|
|
157
165
|
if (!!customLogger) {
|
|
@@ -165,6 +173,15 @@ function printLog(content, printFull, level) {
|
|
|
165
173
|
throw Error(errors.modeErr);
|
|
166
174
|
}
|
|
167
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], fileNameNew = (dateStr + '_' + fileName);
|
|
182
|
+
filePathArr[lastIdx] = fileNameNew;
|
|
183
|
+
return filePathArr.join('/');
|
|
184
|
+
}
|
|
168
185
|
function writeToFile(content, filePath) {
|
|
169
186
|
return new Promise(function (res, rej) {
|
|
170
187
|
return fs_1.default.appendFile(filePath, content, function (err) {
|