jet-logger 1.3.0 → 1.3.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 +1 -1
- package/lib/jetLogger.js +15 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -108,7 +108,7 @@ const customSend: TCustomLogFn = (timestamp: Date, level: string, content: any)
|
|
|
108
108
|
|
|
109
109
|
router.get('api/users', async (req: Request, res: Reponse) => {
|
|
110
110
|
const logger: ILogger = jetLogger(LoggerModes.CUSTOM, '', true, true, undefined, customSend);
|
|
111
|
-
logger.rmTimestamp = true;
|
|
111
|
+
logger.settings.rmTimestamp = true;
|
|
112
112
|
logger.info(req.params.msg);
|
|
113
113
|
return res.status(OK).json({
|
|
114
114
|
message: 'console_mode',
|
package/lib/jetLogger.js
CHANGED
|
@@ -51,11 +51,11 @@ var Defaults = {
|
|
|
51
51
|
Format: Formats.Line,
|
|
52
52
|
};
|
|
53
53
|
function jetLogger(mode, filepath, filepathDatetime, timestamp, format, customLogFn) {
|
|
54
|
-
var settings =
|
|
54
|
+
var settings = _getSettings(mode, filepath, timestamp, filepathDatetime, format, customLogFn);
|
|
55
55
|
return { settings: settings, info: info, imp: imp, warn: warn, err: err };
|
|
56
56
|
}
|
|
57
57
|
exports.jetLogger = jetLogger;
|
|
58
|
-
function
|
|
58
|
+
function _getSettings(mode, filepath, filepathDatetime, timestamp, format, customLogFn) {
|
|
59
59
|
if (!mode) {
|
|
60
60
|
if (!!process.env.JET_LOGGER_MODE) {
|
|
61
61
|
mode = process.env.JET_LOGGER_MODE.toUpperCase();
|
|
@@ -98,7 +98,7 @@ function getSettings(mode, filepath, filepathDatetime, timestamp, format, custom
|
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
if (filepathDatetime) {
|
|
101
|
-
filepath =
|
|
101
|
+
filepath = _addDatetimeToFileName(filepath);
|
|
102
102
|
}
|
|
103
103
|
return {
|
|
104
104
|
mode: mode,
|
|
@@ -109,7 +109,7 @@ function getSettings(mode, filepath, filepathDatetime, timestamp, format, custom
|
|
|
109
109
|
customLogFn: customLogFn,
|
|
110
110
|
};
|
|
111
111
|
}
|
|
112
|
-
function
|
|
112
|
+
function _addDatetimeToFileName(filePath) {
|
|
113
113
|
var dateStr = new Date().toISOString()
|
|
114
114
|
.split('-').join('')
|
|
115
115
|
.split(':').join('')
|
|
@@ -119,18 +119,18 @@ function addDatetimeToFileName(filePath) {
|
|
|
119
119
|
return filePathArr.join('/');
|
|
120
120
|
}
|
|
121
121
|
function info(content, printFull) {
|
|
122
|
-
return
|
|
122
|
+
return _printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, Levels.Info, this.settings);
|
|
123
123
|
}
|
|
124
124
|
function imp(content, printFull) {
|
|
125
|
-
return
|
|
125
|
+
return _printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, Levels.Imp, this.settings);
|
|
126
126
|
}
|
|
127
127
|
function warn(content, printFull) {
|
|
128
|
-
return
|
|
128
|
+
return _printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, Levels.Warn, this.settings);
|
|
129
129
|
}
|
|
130
130
|
function err(content, printFull) {
|
|
131
|
-
return
|
|
131
|
+
return _printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, Levels.Err, this.settings);
|
|
132
132
|
}
|
|
133
|
-
function
|
|
133
|
+
function _printLog(content, printFull, level, settings) {
|
|
134
134
|
var mode = settings.mode, format = settings.format, timestamp = settings.timestamp, filepath = settings.filepath, customLogFn = settings.customLogFn;
|
|
135
135
|
if (mode === LoggerModes.Off) {
|
|
136
136
|
return;
|
|
@@ -147,24 +147,24 @@ function printLog(content, printFull, level, settings) {
|
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
149
|
if (format === Formats.Line) {
|
|
150
|
-
content =
|
|
150
|
+
content = _setupLineFormat(content, timestamp, level);
|
|
151
151
|
}
|
|
152
152
|
else if (format === Formats.Json) {
|
|
153
|
-
content =
|
|
153
|
+
content = _setupJsonFormat(content, timestamp, level);
|
|
154
154
|
}
|
|
155
155
|
if (mode === LoggerModes.Console) {
|
|
156
156
|
var colorFn = colors_1.default[level.Color];
|
|
157
157
|
console.log(colorFn(content));
|
|
158
158
|
}
|
|
159
159
|
else if (mode === LoggerModes.File) {
|
|
160
|
-
|
|
160
|
+
_writeToFile(content + '\n', filepath)
|
|
161
161
|
.catch(function (err) { return console.log(err); });
|
|
162
162
|
}
|
|
163
163
|
else {
|
|
164
164
|
throw Error(Errors.Mode);
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
|
-
function
|
|
167
|
+
function _setupLineFormat(content, timestamp, level) {
|
|
168
168
|
content = (level.Prefix + ': ' + content);
|
|
169
169
|
if (timestamp) {
|
|
170
170
|
var time = '[' + new Date().toISOString() + '] ';
|
|
@@ -172,7 +172,7 @@ function setupLineFormat(content, timestamp, level) {
|
|
|
172
172
|
}
|
|
173
173
|
return content;
|
|
174
174
|
}
|
|
175
|
-
function
|
|
175
|
+
function _setupJsonFormat(content, timestamp, level) {
|
|
176
176
|
var json = {
|
|
177
177
|
level: level.Prefix,
|
|
178
178
|
message: content,
|
|
@@ -182,7 +182,7 @@ function setupJsonFormat(content, timestamp, level) {
|
|
|
182
182
|
}
|
|
183
183
|
return JSON.stringify(json);
|
|
184
184
|
}
|
|
185
|
-
function
|
|
185
|
+
function _writeToFile(content, filePath) {
|
|
186
186
|
return new Promise(function (res, rej) {
|
|
187
187
|
return fs_1.default.appendFile(filePath, content, (function (err) { return !!err ? rej(err) : res(); }));
|
|
188
188
|
});
|