jet-logger 1.1.5 → 1.2.2

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 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
@@ -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, filePath?: string, timestamp?: boolean, format?: Formats, customLogger?: TCustomLogger): {
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 filePath: string;
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 filePath: string;
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
- fileName: 'jet-logger.log',
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, filePath, timestamp, format, customLogger) {
52
- return {
53
- settings: getSettings(mode, filePath, timestamp, format, customLogger),
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, filePath, timestamp, format, customLogger) {
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 (!filePath) {
66
+ if (!filepath) {
71
67
  if (!!process.env.JET_LOGGER_FILEPATH) {
72
- filePath = process.env.JET_LOGGER_FILEPATH;
68
+ filepath = process.env.JET_LOGGER_FILEPATH;
69
+ }
70
+ else {
71
+ filepath = defaults.filepath;
72
+ }
73
+ }
74
+ if (filepathDatetime === undefined || filepathDatetime === null) {
75
+ var envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
76
+ if (!!envVar) {
77
+ filepathDatetime = (envVar.toUpperCase() === 'TRUE');
73
78
  }
74
79
  else {
75
- filePath = defaults.fileName;
80
+ filepathDatetime = defaults.filepathDatetime;
76
81
  }
77
82
  }
78
- if (!timestamp) {
83
+ if (timestamp === undefined || timestamp === null) {
79
84
  if (!!process.env.JET_LOGGER_TIMESTAMP) {
80
85
  timestamp = (process.env.JET_LOGGER_TIMESTAMP.toUpperCase() === 'TRUE');
81
86
  }
@@ -91,9 +96,13 @@ function getSettings(mode, filePath, timestamp, format, customLogger) {
91
96
  format = defaults.format;
92
97
  }
93
98
  }
99
+ if (filepathDatetime) {
100
+ filepath = addDatetimeToFileName(filepath);
101
+ }
94
102
  return {
95
103
  mode: mode,
96
- filePath: filePath,
104
+ filepath: filepath,
105
+ filepathDatetime: filepathDatetime,
97
106
  timestamp: timestamp,
98
107
  format: format,
99
108
  customLogger: customLogger,
@@ -112,7 +121,7 @@ function err(content, printFull) {
112
121
  return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.err, this.settings);
113
122
  }
114
123
  function printLog(content, printFull, level, settings) {
115
- var mode = settings.mode, format = settings.format, timestamp = settings.timestamp, filePath = settings.filePath, customLogger = settings.customLogger;
124
+ var mode = settings.mode, format = settings.format, timestamp = settings.timestamp, filepath = settings.filepath, customLogger = settings.customLogger;
116
125
  if (mode === LoggerModes.Off) {
117
126
  return;
118
127
  }
@@ -134,7 +143,7 @@ function printLog(content, printFull, level, settings) {
134
143
  if (timestamp) {
135
144
  if (format === Formats.Line) {
136
145
  var time = '[' + new Date().toISOString() + '] ';
137
- content = time + content;
146
+ content = (time + content);
138
147
  }
139
148
  else if (format === Formats.Json) {
140
149
  jsonContent.timestamp = new Date().toISOString();
@@ -148,9 +157,8 @@ function printLog(content, printFull, level, settings) {
148
157
  console.log(colorFn(content));
149
158
  }
150
159
  else if (mode === LoggerModes.File) {
151
- writeToFile(content + '\n', filePath).catch(function (err) {
152
- console.log(err);
153
- });
160
+ writeToFile(content + '\n', filepath)
161
+ .catch(function (err) { return console.log(err); });
154
162
  }
155
163
  else if (mode === LoggerModes.Custom) {
156
164
  if (!!customLogger) {
@@ -164,11 +172,19 @@ function printLog(content, printFull, level, settings) {
164
172
  throw Error(errors.modeErr);
165
173
  }
166
174
  }
175
+ function addDatetimeToFileName(filePath) {
176
+ var dateStr = new Date().toISOString()
177
+ .split('-').join('')
178
+ .split(':').join('')
179
+ .slice(0, 15);
180
+ var filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = (dateStr + '_' + fileName);
181
+ filePathArr[lastIdx] = fileNameNew;
182
+ return filePathArr.join('/');
183
+ }
167
184
  function writeToFile(content, filePath) {
168
185
  return new Promise(function (res, rej) {
169
- return fs_1.default.appendFile(filePath, content, function (err) {
170
- return (!!err ? rej(err) : res());
171
- });
186
+ var fn = (function (err) { return !!err ? rej(err) : res(); });
187
+ return fs_1.default.appendFile(filePath, content, fn);
172
188
  });
173
189
  }
174
190
  exports.default = JetLogger();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jet-logger",
3
- "version": "1.1.5",
3
+ "version": "1.2.2",
4
4
  "description": "A super quick, easy to setup logging tool for NodeJS/TypeScript.",
5
5
  "main": "./lib/index.js",
6
6
  "typings": "./lib/index.d.ts",