jet-logger 1.1.5 → 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 +5 -3
- package/lib/index.js +38 -19
- 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
|
@@ -10,10 +10,11 @@ export declare enum Formats {
|
|
|
10
10
|
}
|
|
11
11
|
declare type TJetLogger = ReturnType<typeof JetLogger>;
|
|
12
12
|
export declare type TCustomLogger = (timestamp: Date, prefix: string, content: any) => void;
|
|
13
|
-
export declare function JetLogger(mode?: LoggerModes,
|
|
13
|
+
export declare function JetLogger(mode?: LoggerModes, filepath?: string, filepathDatetime?: boolean, timestamp?: boolean, format?: Formats, customLogger?: TCustomLogger): {
|
|
14
14
|
readonly settings: {
|
|
15
15
|
readonly mode: LoggerModes;
|
|
16
|
-
readonly
|
|
16
|
+
readonly filepath: string;
|
|
17
|
+
readonly filepathDatetime: boolean;
|
|
17
18
|
readonly timestamp: boolean;
|
|
18
19
|
readonly format: Formats;
|
|
19
20
|
readonly customLogger: TCustomLogger | undefined;
|
|
@@ -30,7 +31,8 @@ declare function err(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
|
30
31
|
declare const _default: {
|
|
31
32
|
readonly settings: {
|
|
32
33
|
readonly mode: LoggerModes;
|
|
33
|
-
readonly
|
|
34
|
+
readonly filepath: string;
|
|
35
|
+
readonly filepathDatetime: boolean;
|
|
34
36
|
readonly timestamp: boolean;
|
|
35
37
|
readonly format: Formats;
|
|
36
38
|
readonly customLogger: TCustomLogger | undefined;
|
package/lib/index.js
CHANGED
|
@@ -43,22 +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
|
-
};
|
|
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 };
|
|
59
55
|
}
|
|
60
56
|
exports.JetLogger = JetLogger;
|
|
61
|
-
function getSettings(mode,
|
|
57
|
+
function getSettings(mode, filepath, filepathDatetime, timestamp, format, customLogger) {
|
|
62
58
|
if (!mode) {
|
|
63
59
|
if (!!process.env.JET_LOGGER_MODE) {
|
|
64
60
|
mode = process.env.JET_LOGGER_MODE.toUpperCase();
|
|
@@ -67,15 +63,24 @@ function getSettings(mode, filePath, timestamp, format, customLogger) {
|
|
|
67
63
|
mode = defaults.mode;
|
|
68
64
|
}
|
|
69
65
|
}
|
|
70
|
-
if (!
|
|
66
|
+
if (!filepath) {
|
|
71
67
|
if (!!process.env.JET_LOGGER_FILEPATH) {
|
|
72
|
-
|
|
68
|
+
filepath = process.env.JET_LOGGER_FILEPATH;
|
|
73
69
|
}
|
|
74
70
|
else {
|
|
75
|
-
|
|
71
|
+
filepath = defaults.filepath;
|
|
76
72
|
}
|
|
77
73
|
}
|
|
78
|
-
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) {
|
|
79
84
|
if (!!process.env.JET_LOGGER_TIMESTAMP) {
|
|
80
85
|
timestamp = (process.env.JET_LOGGER_TIMESTAMP.toUpperCase() === 'TRUE');
|
|
81
86
|
}
|
|
@@ -93,7 +98,8 @@ function getSettings(mode, filePath, timestamp, format, customLogger) {
|
|
|
93
98
|
}
|
|
94
99
|
return {
|
|
95
100
|
mode: mode,
|
|
96
|
-
|
|
101
|
+
filepath: filepath,
|
|
102
|
+
filepathDatetime: filepathDatetime,
|
|
97
103
|
timestamp: timestamp,
|
|
98
104
|
format: format,
|
|
99
105
|
customLogger: customLogger,
|
|
@@ -112,7 +118,7 @@ function err(content, printFull) {
|
|
|
112
118
|
return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.err, this.settings);
|
|
113
119
|
}
|
|
114
120
|
function printLog(content, printFull, level, settings) {
|
|
115
|
-
var mode = settings.mode, format = settings.format, timestamp = settings.timestamp,
|
|
121
|
+
var mode = settings.mode, format = settings.format, timestamp = settings.timestamp, filepath = settings.filepath, filepathDatetime = settings.filepathDatetime, customLogger = settings.customLogger;
|
|
116
122
|
if (mode === LoggerModes.Off) {
|
|
117
123
|
return;
|
|
118
124
|
}
|
|
@@ -148,9 +154,12 @@ function printLog(content, printFull, level, settings) {
|
|
|
148
154
|
console.log(colorFn(content));
|
|
149
155
|
}
|
|
150
156
|
else if (mode === LoggerModes.File) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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); });
|
|
154
163
|
}
|
|
155
164
|
else if (mode === LoggerModes.Custom) {
|
|
156
165
|
if (!!customLogger) {
|
|
@@ -164,6 +173,16 @@ function printLog(content, printFull, level, settings) {
|
|
|
164
173
|
throw Error(errors.modeErr);
|
|
165
174
|
}
|
|
166
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
|
+
}
|
|
167
186
|
function writeToFile(content, filePath) {
|
|
168
187
|
return new Promise(function (res, rej) {
|
|
169
188
|
return fs_1.default.appendFile(filePath, content, function (err) {
|