jet-logger 2.2.0 → 2.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.
@@ -1,25 +1,16 @@
1
- /* eslint-disable no-console */
2
- /* eslint-disable no-process-env */
3
1
  import colors from 'colors';
4
2
  import fs from 'fs';
5
3
  import util from 'util';
6
- /******************************************************************************
7
- Constants
8
- ******************************************************************************/
9
- // Options for printing a log.
10
4
  const LOGGER_MODES = {
11
5
  Console: 'CONSOLE',
12
6
  File: 'FILE',
13
7
  Custom: 'CUSTOM',
14
8
  Off: 'OFF',
15
9
  };
16
- // Log formats
17
10
  const FORMATS = {
18
11
  Line: 'LINE',
19
12
  Json: 'JSON',
20
13
  };
21
- // Note colors here need be a color from
22
- // the colors library above.
23
14
  const LEVELS = {
24
15
  Info: {
25
16
  Color: 'green',
@@ -45,7 +36,6 @@ const DEFAULTS = {
45
36
  format: FORMATS.Line,
46
37
  customLogFn: () => ({}),
47
38
  };
48
- // Errors
49
39
  const Errors = {
50
40
  CustomLogger: 'Custom logger mode set to true, but no custom logger was provided.',
51
41
  Mode: 'The correct logger mode was not specified: Must be "CUSTOM", "FILE", ' +
@@ -57,22 +47,14 @@ export const JetLogger = {
57
47
  instanceOf,
58
48
  };
59
49
  const kJetLogger = Symbol('k-jet-logger');
60
- /******************************************************************************
61
- Functions
62
- ******************************************************************************/
63
- /**
64
- * Default function
65
- */
66
50
  export function jetLogger(options) {
67
51
  let mode = DEFAULTS.mode, filePath = DEFAULTS.filePath, timestamp = DEFAULTS.timestamp, format = DEFAULTS.format, customLogFn = DEFAULTS.customLogFn;
68
- // Setup the mode
69
52
  if (options?.mode !== undefined) {
70
53
  mode = options.mode;
71
54
  }
72
55
  else if (!!process.env.JET_LOGGER_MODE) {
73
56
  mode = process.env.JET_LOGGER_MODE.toUpperCase();
74
57
  }
75
- // ** Logger Mode Off ** //
76
58
  if (mode === LOGGER_MODES.Off) {
77
59
  return {
78
60
  info: (_, __) => ({}),
@@ -82,7 +64,6 @@ export function jetLogger(options) {
82
64
  [kJetLogger]: true,
83
65
  };
84
66
  }
85
- // ** Custom Logger Function ** //
86
67
  if (mode === LOGGER_MODES.Custom) {
87
68
  if (options?.customLogger !== undefined) {
88
69
  customLogFn = options.customLogger;
@@ -98,14 +79,12 @@ export function jetLogger(options) {
98
79
  [kJetLogger]: true,
99
80
  };
100
81
  }
101
- // Filepath
102
82
  if (options?.filepath !== undefined) {
103
83
  filePath = options.filepath;
104
84
  }
105
85
  else if (!!process.env.JET_LOGGER_FILEPATH) {
106
86
  filePath = process.env.JET_LOGGER_FILEPATH;
107
87
  }
108
- // Timestamp
109
88
  if (options?.timestamp !== undefined) {
110
89
  timestamp = options.timestamp;
111
90
  }
@@ -113,14 +92,12 @@ export function jetLogger(options) {
113
92
  const envVar = process.env.JET_LOGGER_TIMESTAMP;
114
93
  timestamp = envVar.toUpperCase() === 'TRUE';
115
94
  }
116
- // Format
117
95
  if (options?.format !== undefined) {
118
96
  format = options.format;
119
97
  }
120
98
  else if (!!process.env.JET_LOGGER_FORMAT) {
121
99
  format = process.env.JET_LOGGER_FORMAT.toUpperCase();
122
100
  }
123
- // Setup the formatter
124
101
  let formatter = () => '';
125
102
  if (format === FORMATS.Line) {
126
103
  formatter = setupLineFormatter(timestamp);
@@ -128,9 +105,7 @@ export function jetLogger(options) {
128
105
  else if (format === FORMATS.Json) {
129
106
  formatter = setupJsonFormatter(timestamp);
130
107
  }
131
- // ** Print to File ** //
132
108
  if (mode === LOGGER_MODES.File) {
133
- // FilePath dateTime
134
109
  let filePathDatetime = true;
135
110
  if (options?.filepathDatetimeParam !== undefined) {
136
111
  filePathDatetime = options.filepathDatetimeParam;
@@ -139,11 +114,9 @@ export function jetLogger(options) {
139
114
  const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
140
115
  filePathDatetime = envVar.toUpperCase() === 'TRUE';
141
116
  }
142
- // Modify filepath if filepath datetime is true
143
117
  if (filePathDatetime) {
144
118
  filePath = addDatetimeToFileName(filePath);
145
119
  }
146
- // Return
147
120
  return {
148
121
  info: setupPrintToFile(LEVELS.Info, formatter, filePath),
149
122
  imp: setupPrintToFile(LEVELS.Important, formatter, filePath),
@@ -152,7 +125,6 @@ export function jetLogger(options) {
152
125
  [kJetLogger]: true,
153
126
  };
154
127
  }
155
- // Console (Default)
156
128
  return {
157
129
  info: setupPrintToConsole(LEVELS.Info, formatter),
158
130
  imp: setupPrintToConsole(LEVELS.Important, formatter),
@@ -161,9 +133,6 @@ export function jetLogger(options) {
161
133
  [kJetLogger]: true,
162
134
  };
163
135
  }
164
- /**
165
- * Print a log with a custom logger function.
166
- */
167
136
  function setupPrintWithCustomLogger(level, customLogFn) {
168
137
  return (content, printFull) => {
169
138
  let contentNew;
@@ -176,9 +145,6 @@ function setupPrintWithCustomLogger(level, customLogFn) {
176
145
  return customLogFn(new Date(), level.Prefix, contentNew);
177
146
  };
178
147
  }
179
- /**
180
- * Setup line format.
181
- */
182
148
  function setupLineFormatter(timestamp) {
183
149
  if (timestamp) {
184
150
  return (content, level) => {
@@ -192,9 +158,6 @@ function setupLineFormatter(timestamp) {
192
158
  };
193
159
  }
194
160
  }
195
- /**
196
- * Setup json format.
197
- */
198
161
  function setupJsonFormatter(timestamp) {
199
162
  if (timestamp) {
200
163
  return (content, level) => {
@@ -216,9 +179,6 @@ function setupJsonFormatter(timestamp) {
216
179
  };
217
180
  }
218
181
  }
219
- /**
220
- * Write to file.
221
- */
222
182
  function setupPrintToFile(level, formatter, filePath) {
223
183
  return (content, printFull) => {
224
184
  let contentNew;
@@ -236,9 +196,6 @@ function setupPrintToFile(level, formatter, filePath) {
236
196
  });
237
197
  };
238
198
  }
239
- /**
240
- * Print a log to the console.
241
- */
242
199
  function setupPrintToConsole(level, formatter) {
243
200
  return (content, printFull) => {
244
201
  let contentNew;
@@ -253,12 +210,7 @@ function setupPrintToConsole(level, formatter) {
253
210
  console.log(colorFn(contentNew));
254
211
  };
255
212
  }
256
- /**
257
- * Prepend the filename in the file path with a timestamp.
258
- * i.e. '/home/jet-logger.log' => '/home/20220805T033709_jet-logger.log'
259
- */
260
213
  function addDatetimeToFileName(filePath) {
261
- // Get the date string
262
214
  const dateStr = new Date()
263
215
  .toISOString()
264
216
  .split('-')
@@ -266,20 +218,12 @@ function addDatetimeToFileName(filePath) {
266
218
  .split(':')
267
219
  .join('')
268
220
  .slice(0, 15);
269
- // Setup new file name
270
221
  const filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = dateStr + '_' + fileName;
271
- // Setup new file path
272
222
  filePathArr[lastIdx] = fileNameNew;
273
223
  return filePathArr.join('/');
274
224
  }
275
- /**
276
- * Check if an object is an instance of jetLogger
277
- */
278
225
  function instanceOf(arg) {
279
226
  return (typeof arg === 'object' &&
280
227
  arg[kJetLogger] === true);
281
228
  }
282
- /******************************************************************************
283
- Export
284
- ******************************************************************************/
285
229
  export default jetLogger();
@@ -1,25 +1,16 @@
1
- /* eslint-disable no-console */
2
- /* eslint-disable no-process-env */
3
1
  import colors from 'colors';
4
2
  import fs from 'fs';
5
3
  import util from 'util';
6
- /******************************************************************************
7
- Constants
8
- ******************************************************************************/
9
- // Options for printing a log.
10
4
  const LOGGER_MODES = {
11
5
  Console: 'CONSOLE',
12
6
  File: 'FILE',
13
7
  Custom: 'CUSTOM',
14
8
  Off: 'OFF',
15
9
  };
16
- // Log formats
17
10
  const FORMATS = {
18
11
  Line: 'LINE',
19
12
  Json: 'JSON',
20
13
  };
21
- // Note colors here need be a color from
22
- // the colors library above.
23
14
  const LEVELS = {
24
15
  Info: {
25
16
  Color: 'green',
@@ -45,7 +36,6 @@ const DEFAULTS = {
45
36
  format: FORMATS.Line,
46
37
  customLogFn: () => ({}),
47
38
  };
48
- // Errors
49
39
  const Errors = {
50
40
  CustomLogger: 'Custom logger mode set to true, but no custom logger was provided.',
51
41
  Mode: 'The correct logger mode was not specified: Must be "CUSTOM", "FILE", ' +
@@ -57,22 +47,14 @@ export const JetLogger = {
57
47
  instanceOf,
58
48
  };
59
49
  const kJetLogger = Symbol('k-jet-logger');
60
- /******************************************************************************
61
- Functions
62
- ******************************************************************************/
63
- /**
64
- * Default function
65
- */
66
50
  export function jetLogger(options) {
67
51
  let mode = DEFAULTS.mode, filePath = DEFAULTS.filePath, timestamp = DEFAULTS.timestamp, format = DEFAULTS.format, customLogFn = DEFAULTS.customLogFn;
68
- // Setup the mode
69
52
  if (options?.mode !== undefined) {
70
53
  mode = options.mode;
71
54
  }
72
55
  else if (!!process.env.JET_LOGGER_MODE) {
73
56
  mode = process.env.JET_LOGGER_MODE.toUpperCase();
74
57
  }
75
- // ** Logger Mode Off ** //
76
58
  if (mode === LOGGER_MODES.Off) {
77
59
  return {
78
60
  info: (_, __) => ({}),
@@ -82,7 +64,6 @@ export function jetLogger(options) {
82
64
  [kJetLogger]: true,
83
65
  };
84
66
  }
85
- // ** Custom Logger Function ** //
86
67
  if (mode === LOGGER_MODES.Custom) {
87
68
  if (options?.customLogger !== undefined) {
88
69
  customLogFn = options.customLogger;
@@ -98,14 +79,12 @@ export function jetLogger(options) {
98
79
  [kJetLogger]: true,
99
80
  };
100
81
  }
101
- // Filepath
102
82
  if (options?.filepath !== undefined) {
103
83
  filePath = options.filepath;
104
84
  }
105
85
  else if (!!process.env.JET_LOGGER_FILEPATH) {
106
86
  filePath = process.env.JET_LOGGER_FILEPATH;
107
87
  }
108
- // Timestamp
109
88
  if (options?.timestamp !== undefined) {
110
89
  timestamp = options.timestamp;
111
90
  }
@@ -113,14 +92,12 @@ export function jetLogger(options) {
113
92
  const envVar = process.env.JET_LOGGER_TIMESTAMP;
114
93
  timestamp = envVar.toUpperCase() === 'TRUE';
115
94
  }
116
- // Format
117
95
  if (options?.format !== undefined) {
118
96
  format = options.format;
119
97
  }
120
98
  else if (!!process.env.JET_LOGGER_FORMAT) {
121
99
  format = process.env.JET_LOGGER_FORMAT.toUpperCase();
122
100
  }
123
- // Setup the formatter
124
101
  let formatter = () => '';
125
102
  if (format === FORMATS.Line) {
126
103
  formatter = setupLineFormatter(timestamp);
@@ -128,9 +105,7 @@ export function jetLogger(options) {
128
105
  else if (format === FORMATS.Json) {
129
106
  formatter = setupJsonFormatter(timestamp);
130
107
  }
131
- // ** Print to File ** //
132
108
  if (mode === LOGGER_MODES.File) {
133
- // FilePath dateTime
134
109
  let filePathDatetime = true;
135
110
  if (options?.filepathDatetimeParam !== undefined) {
136
111
  filePathDatetime = options.filepathDatetimeParam;
@@ -139,11 +114,9 @@ export function jetLogger(options) {
139
114
  const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
140
115
  filePathDatetime = envVar.toUpperCase() === 'TRUE';
141
116
  }
142
- // Modify filepath if filepath datetime is true
143
117
  if (filePathDatetime) {
144
118
  filePath = addDatetimeToFileName(filePath);
145
119
  }
146
- // Return
147
120
  return {
148
121
  info: setupPrintToFile(LEVELS.Info, formatter, filePath),
149
122
  imp: setupPrintToFile(LEVELS.Important, formatter, filePath),
@@ -152,7 +125,6 @@ export function jetLogger(options) {
152
125
  [kJetLogger]: true,
153
126
  };
154
127
  }
155
- // Console (Default)
156
128
  return {
157
129
  info: setupPrintToConsole(LEVELS.Info, formatter),
158
130
  imp: setupPrintToConsole(LEVELS.Important, formatter),
@@ -161,9 +133,6 @@ export function jetLogger(options) {
161
133
  [kJetLogger]: true,
162
134
  };
163
135
  }
164
- /**
165
- * Print a log with a custom logger function.
166
- */
167
136
  function setupPrintWithCustomLogger(level, customLogFn) {
168
137
  return (content, printFull) => {
169
138
  let contentNew;
@@ -176,9 +145,6 @@ function setupPrintWithCustomLogger(level, customLogFn) {
176
145
  return customLogFn(new Date(), level.Prefix, contentNew);
177
146
  };
178
147
  }
179
- /**
180
- * Setup line format.
181
- */
182
148
  function setupLineFormatter(timestamp) {
183
149
  if (timestamp) {
184
150
  return (content, level) => {
@@ -192,9 +158,6 @@ function setupLineFormatter(timestamp) {
192
158
  };
193
159
  }
194
160
  }
195
- /**
196
- * Setup json format.
197
- */
198
161
  function setupJsonFormatter(timestamp) {
199
162
  if (timestamp) {
200
163
  return (content, level) => {
@@ -216,9 +179,6 @@ function setupJsonFormatter(timestamp) {
216
179
  };
217
180
  }
218
181
  }
219
- /**
220
- * Write to file.
221
- */
222
182
  function setupPrintToFile(level, formatter, filePath) {
223
183
  return (content, printFull) => {
224
184
  let contentNew;
@@ -236,9 +196,6 @@ function setupPrintToFile(level, formatter, filePath) {
236
196
  });
237
197
  };
238
198
  }
239
- /**
240
- * Print a log to the console.
241
- */
242
199
  function setupPrintToConsole(level, formatter) {
243
200
  return (content, printFull) => {
244
201
  let contentNew;
@@ -253,12 +210,7 @@ function setupPrintToConsole(level, formatter) {
253
210
  console.log(colorFn(contentNew));
254
211
  };
255
212
  }
256
- /**
257
- * Prepend the filename in the file path with a timestamp.
258
- * i.e. '/home/jet-logger.log' => '/home/20220805T033709_jet-logger.log'
259
- */
260
213
  function addDatetimeToFileName(filePath) {
261
- // Get the date string
262
214
  const dateStr = new Date()
263
215
  .toISOString()
264
216
  .split('-')
@@ -266,20 +218,12 @@ function addDatetimeToFileName(filePath) {
266
218
  .split(':')
267
219
  .join('')
268
220
  .slice(0, 15);
269
- // Setup new file name
270
221
  const filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = dateStr + '_' + fileName;
271
- // Setup new file path
272
222
  filePathArr[lastIdx] = fileNameNew;
273
223
  return filePathArr.join('/');
274
224
  }
275
- /**
276
- * Check if an object is an instance of jetLogger
277
- */
278
225
  function instanceOf(arg) {
279
226
  return (typeof arg === 'object' &&
280
227
  arg[kJetLogger] === true);
281
228
  }
282
- /******************************************************************************
283
- Export
284
- ******************************************************************************/
285
229
  export default jetLogger();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jet-logger",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "description": "A super quick, easy to setup logging tool for NodeJS/TypeScript.",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",