jet-logger 2.2.0 → 2.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.
@@ -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", ' +
@@ -54,35 +44,23 @@ const Errors = {
54
44
  export const JetLogger = {
55
45
  Modes: LOGGER_MODES,
56
46
  Formats: FORMATS,
57
- instanceOf,
58
47
  };
59
- const kJetLogger = Symbol('k-jet-logger');
60
- /******************************************************************************
61
- Functions
62
- ******************************************************************************/
63
- /**
64
- * Default function
65
- */
66
48
  export function jetLogger(options) {
67
49
  let mode = DEFAULTS.mode, filePath = DEFAULTS.filePath, timestamp = DEFAULTS.timestamp, format = DEFAULTS.format, customLogFn = DEFAULTS.customLogFn;
68
- // Setup the mode
69
50
  if (options?.mode !== undefined) {
70
51
  mode = options.mode;
71
52
  }
72
53
  else if (!!process.env.JET_LOGGER_MODE) {
73
54
  mode = process.env.JET_LOGGER_MODE.toUpperCase();
74
55
  }
75
- // ** Logger Mode Off ** //
76
56
  if (mode === LOGGER_MODES.Off) {
77
57
  return {
78
58
  info: (_, __) => ({}),
79
59
  imp: (_, __) => ({}),
80
60
  warn: (_, __) => ({}),
81
61
  err: (_, __) => ({}),
82
- [kJetLogger]: true,
83
62
  };
84
63
  }
85
- // ** Custom Logger Function ** //
86
64
  if (mode === LOGGER_MODES.Custom) {
87
65
  if (options?.customLogger !== undefined) {
88
66
  customLogFn = options.customLogger;
@@ -95,17 +73,14 @@ export function jetLogger(options) {
95
73
  imp: setupPrintWithCustomLogger(LEVELS.Important, customLogFn),
96
74
  warn: setupPrintWithCustomLogger(LEVELS.Warning, customLogFn),
97
75
  err: setupPrintWithCustomLogger(LEVELS.Error, customLogFn),
98
- [kJetLogger]: true,
99
76
  };
100
77
  }
101
- // Filepath
102
78
  if (options?.filepath !== undefined) {
103
79
  filePath = options.filepath;
104
80
  }
105
81
  else if (!!process.env.JET_LOGGER_FILEPATH) {
106
82
  filePath = process.env.JET_LOGGER_FILEPATH;
107
83
  }
108
- // Timestamp
109
84
  if (options?.timestamp !== undefined) {
110
85
  timestamp = options.timestamp;
111
86
  }
@@ -113,14 +88,12 @@ export function jetLogger(options) {
113
88
  const envVar = process.env.JET_LOGGER_TIMESTAMP;
114
89
  timestamp = envVar.toUpperCase() === 'TRUE';
115
90
  }
116
- // Format
117
91
  if (options?.format !== undefined) {
118
92
  format = options.format;
119
93
  }
120
94
  else if (!!process.env.JET_LOGGER_FORMAT) {
121
95
  format = process.env.JET_LOGGER_FORMAT.toUpperCase();
122
96
  }
123
- // Setup the formatter
124
97
  let formatter = () => '';
125
98
  if (format === FORMATS.Line) {
126
99
  formatter = setupLineFormatter(timestamp);
@@ -128,9 +101,7 @@ export function jetLogger(options) {
128
101
  else if (format === FORMATS.Json) {
129
102
  formatter = setupJsonFormatter(timestamp);
130
103
  }
131
- // ** Print to File ** //
132
104
  if (mode === LOGGER_MODES.File) {
133
- // FilePath dateTime
134
105
  let filePathDatetime = true;
135
106
  if (options?.filepathDatetimeParam !== undefined) {
136
107
  filePathDatetime = options.filepathDatetimeParam;
@@ -139,31 +110,23 @@ export function jetLogger(options) {
139
110
  const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
140
111
  filePathDatetime = envVar.toUpperCase() === 'TRUE';
141
112
  }
142
- // Modify filepath if filepath datetime is true
143
113
  if (filePathDatetime) {
144
114
  filePath = addDatetimeToFileName(filePath);
145
115
  }
146
- // Return
147
116
  return {
148
117
  info: setupPrintToFile(LEVELS.Info, formatter, filePath),
149
118
  imp: setupPrintToFile(LEVELS.Important, formatter, filePath),
150
119
  warn: setupPrintToFile(LEVELS.Warning, formatter, filePath),
151
120
  err: setupPrintToFile(LEVELS.Error, formatter, filePath),
152
- [kJetLogger]: true,
153
121
  };
154
122
  }
155
- // Console (Default)
156
123
  return {
157
124
  info: setupPrintToConsole(LEVELS.Info, formatter),
158
125
  imp: setupPrintToConsole(LEVELS.Important, formatter),
159
126
  warn: setupPrintToConsole(LEVELS.Warning, formatter),
160
127
  err: setupPrintToConsole(LEVELS.Error, formatter),
161
- [kJetLogger]: true,
162
128
  };
163
129
  }
164
- /**
165
- * Print a log with a custom logger function.
166
- */
167
130
  function setupPrintWithCustomLogger(level, customLogFn) {
168
131
  return (content, printFull) => {
169
132
  let contentNew;
@@ -176,9 +139,6 @@ function setupPrintWithCustomLogger(level, customLogFn) {
176
139
  return customLogFn(new Date(), level.Prefix, contentNew);
177
140
  };
178
141
  }
179
- /**
180
- * Setup line format.
181
- */
182
142
  function setupLineFormatter(timestamp) {
183
143
  if (timestamp) {
184
144
  return (content, level) => {
@@ -192,9 +152,6 @@ function setupLineFormatter(timestamp) {
192
152
  };
193
153
  }
194
154
  }
195
- /**
196
- * Setup json format.
197
- */
198
155
  function setupJsonFormatter(timestamp) {
199
156
  if (timestamp) {
200
157
  return (content, level) => {
@@ -216,9 +173,6 @@ function setupJsonFormatter(timestamp) {
216
173
  };
217
174
  }
218
175
  }
219
- /**
220
- * Write to file.
221
- */
222
176
  function setupPrintToFile(level, formatter, filePath) {
223
177
  return (content, printFull) => {
224
178
  let contentNew;
@@ -236,9 +190,6 @@ function setupPrintToFile(level, formatter, filePath) {
236
190
  });
237
191
  };
238
192
  }
239
- /**
240
- * Print a log to the console.
241
- */
242
193
  function setupPrintToConsole(level, formatter) {
243
194
  return (content, printFull) => {
244
195
  let contentNew;
@@ -253,12 +204,7 @@ function setupPrintToConsole(level, formatter) {
253
204
  console.log(colorFn(contentNew));
254
205
  };
255
206
  }
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
207
  function addDatetimeToFileName(filePath) {
261
- // Get the date string
262
208
  const dateStr = new Date()
263
209
  .toISOString()
264
210
  .split('-')
@@ -266,20 +212,8 @@ function addDatetimeToFileName(filePath) {
266
212
  .split(':')
267
213
  .join('')
268
214
  .slice(0, 15);
269
- // Setup new file name
270
215
  const filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = dateStr + '_' + fileName;
271
- // Setup new file path
272
216
  filePathArr[lastIdx] = fileNameNew;
273
217
  return filePathArr.join('/');
274
218
  }
275
- /**
276
- * Check if an object is an instance of jetLogger
277
- */
278
- function instanceOf(arg) {
279
- return (typeof arg === 'object' &&
280
- arg[kJetLogger] === true);
281
- }
282
- /******************************************************************************
283
- Export
284
- ******************************************************************************/
285
219
  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", ' +
@@ -54,35 +44,23 @@ const Errors = {
54
44
  export const JetLogger = {
55
45
  Modes: LOGGER_MODES,
56
46
  Formats: FORMATS,
57
- instanceOf,
58
47
  };
59
- const kJetLogger = Symbol('k-jet-logger');
60
- /******************************************************************************
61
- Functions
62
- ******************************************************************************/
63
- /**
64
- * Default function
65
- */
66
48
  export function jetLogger(options) {
67
49
  let mode = DEFAULTS.mode, filePath = DEFAULTS.filePath, timestamp = DEFAULTS.timestamp, format = DEFAULTS.format, customLogFn = DEFAULTS.customLogFn;
68
- // Setup the mode
69
50
  if (options?.mode !== undefined) {
70
51
  mode = options.mode;
71
52
  }
72
53
  else if (!!process.env.JET_LOGGER_MODE) {
73
54
  mode = process.env.JET_LOGGER_MODE.toUpperCase();
74
55
  }
75
- // ** Logger Mode Off ** //
76
56
  if (mode === LOGGER_MODES.Off) {
77
57
  return {
78
58
  info: (_, __) => ({}),
79
59
  imp: (_, __) => ({}),
80
60
  warn: (_, __) => ({}),
81
61
  err: (_, __) => ({}),
82
- [kJetLogger]: true,
83
62
  };
84
63
  }
85
- // ** Custom Logger Function ** //
86
64
  if (mode === LOGGER_MODES.Custom) {
87
65
  if (options?.customLogger !== undefined) {
88
66
  customLogFn = options.customLogger;
@@ -95,17 +73,14 @@ export function jetLogger(options) {
95
73
  imp: setupPrintWithCustomLogger(LEVELS.Important, customLogFn),
96
74
  warn: setupPrintWithCustomLogger(LEVELS.Warning, customLogFn),
97
75
  err: setupPrintWithCustomLogger(LEVELS.Error, customLogFn),
98
- [kJetLogger]: true,
99
76
  };
100
77
  }
101
- // Filepath
102
78
  if (options?.filepath !== undefined) {
103
79
  filePath = options.filepath;
104
80
  }
105
81
  else if (!!process.env.JET_LOGGER_FILEPATH) {
106
82
  filePath = process.env.JET_LOGGER_FILEPATH;
107
83
  }
108
- // Timestamp
109
84
  if (options?.timestamp !== undefined) {
110
85
  timestamp = options.timestamp;
111
86
  }
@@ -113,14 +88,12 @@ export function jetLogger(options) {
113
88
  const envVar = process.env.JET_LOGGER_TIMESTAMP;
114
89
  timestamp = envVar.toUpperCase() === 'TRUE';
115
90
  }
116
- // Format
117
91
  if (options?.format !== undefined) {
118
92
  format = options.format;
119
93
  }
120
94
  else if (!!process.env.JET_LOGGER_FORMAT) {
121
95
  format = process.env.JET_LOGGER_FORMAT.toUpperCase();
122
96
  }
123
- // Setup the formatter
124
97
  let formatter = () => '';
125
98
  if (format === FORMATS.Line) {
126
99
  formatter = setupLineFormatter(timestamp);
@@ -128,9 +101,7 @@ export function jetLogger(options) {
128
101
  else if (format === FORMATS.Json) {
129
102
  formatter = setupJsonFormatter(timestamp);
130
103
  }
131
- // ** Print to File ** //
132
104
  if (mode === LOGGER_MODES.File) {
133
- // FilePath dateTime
134
105
  let filePathDatetime = true;
135
106
  if (options?.filepathDatetimeParam !== undefined) {
136
107
  filePathDatetime = options.filepathDatetimeParam;
@@ -139,31 +110,23 @@ export function jetLogger(options) {
139
110
  const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
140
111
  filePathDatetime = envVar.toUpperCase() === 'TRUE';
141
112
  }
142
- // Modify filepath if filepath datetime is true
143
113
  if (filePathDatetime) {
144
114
  filePath = addDatetimeToFileName(filePath);
145
115
  }
146
- // Return
147
116
  return {
148
117
  info: setupPrintToFile(LEVELS.Info, formatter, filePath),
149
118
  imp: setupPrintToFile(LEVELS.Important, formatter, filePath),
150
119
  warn: setupPrintToFile(LEVELS.Warning, formatter, filePath),
151
120
  err: setupPrintToFile(LEVELS.Error, formatter, filePath),
152
- [kJetLogger]: true,
153
121
  };
154
122
  }
155
- // Console (Default)
156
123
  return {
157
124
  info: setupPrintToConsole(LEVELS.Info, formatter),
158
125
  imp: setupPrintToConsole(LEVELS.Important, formatter),
159
126
  warn: setupPrintToConsole(LEVELS.Warning, formatter),
160
127
  err: setupPrintToConsole(LEVELS.Error, formatter),
161
- [kJetLogger]: true,
162
128
  };
163
129
  }
164
- /**
165
- * Print a log with a custom logger function.
166
- */
167
130
  function setupPrintWithCustomLogger(level, customLogFn) {
168
131
  return (content, printFull) => {
169
132
  let contentNew;
@@ -176,9 +139,6 @@ function setupPrintWithCustomLogger(level, customLogFn) {
176
139
  return customLogFn(new Date(), level.Prefix, contentNew);
177
140
  };
178
141
  }
179
- /**
180
- * Setup line format.
181
- */
182
142
  function setupLineFormatter(timestamp) {
183
143
  if (timestamp) {
184
144
  return (content, level) => {
@@ -192,9 +152,6 @@ function setupLineFormatter(timestamp) {
192
152
  };
193
153
  }
194
154
  }
195
- /**
196
- * Setup json format.
197
- */
198
155
  function setupJsonFormatter(timestamp) {
199
156
  if (timestamp) {
200
157
  return (content, level) => {
@@ -216,9 +173,6 @@ function setupJsonFormatter(timestamp) {
216
173
  };
217
174
  }
218
175
  }
219
- /**
220
- * Write to file.
221
- */
222
176
  function setupPrintToFile(level, formatter, filePath) {
223
177
  return (content, printFull) => {
224
178
  let contentNew;
@@ -236,9 +190,6 @@ function setupPrintToFile(level, formatter, filePath) {
236
190
  });
237
191
  };
238
192
  }
239
- /**
240
- * Print a log to the console.
241
- */
242
193
  function setupPrintToConsole(level, formatter) {
243
194
  return (content, printFull) => {
244
195
  let contentNew;
@@ -253,12 +204,7 @@ function setupPrintToConsole(level, formatter) {
253
204
  console.log(colorFn(contentNew));
254
205
  };
255
206
  }
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
207
  function addDatetimeToFileName(filePath) {
261
- // Get the date string
262
208
  const dateStr = new Date()
263
209
  .toISOString()
264
210
  .split('-')
@@ -266,20 +212,8 @@ function addDatetimeToFileName(filePath) {
266
212
  .split(':')
267
213
  .join('')
268
214
  .slice(0, 15);
269
- // Setup new file name
270
215
  const filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = dateStr + '_' + fileName;
271
- // Setup new file path
272
216
  filePathArr[lastIdx] = fileNameNew;
273
217
  return filePathArr.join('/');
274
218
  }
275
- /**
276
- * Check if an object is an instance of jetLogger
277
- */
278
- function instanceOf(arg) {
279
- return (typeof arg === 'object' &&
280
- arg[kJetLogger] === true);
281
- }
282
- /******************************************************************************
283
- Export
284
- ******************************************************************************/
285
219
  export default jetLogger();
@@ -22,9 +22,7 @@ export declare const JetLogger: {
22
22
  readonly Line: "LINE";
23
23
  readonly Json: "JSON";
24
24
  };
25
- readonly instanceOf: typeof instanceOf;
26
25
  };
27
- declare const kJetLogger: unique symbol;
28
26
  /******************************************************************************
29
27
  Types
30
28
  ******************************************************************************/
@@ -40,12 +38,6 @@ interface Options {
40
38
  format?: Formats;
41
39
  customLogger?: CustomLogger;
42
40
  }
43
- interface JetLogger {
44
- info: (content: unknown, print?: boolean) => void;
45
- imp: (content: unknown, print?: boolean) => void;
46
- warn: (content: unknown, print?: boolean) => void;
47
- err: (content: unknown, print?: boolean) => void;
48
- }
49
41
  /******************************************************************************
50
42
  Functions
51
43
  ******************************************************************************/
@@ -57,12 +49,7 @@ export declare function jetLogger(options?: Options): {
57
49
  readonly imp: LogFunction;
58
50
  readonly warn: LogFunction;
59
51
  readonly err: LogFunction;
60
- readonly [kJetLogger]: true;
61
52
  };
62
- /**
63
- * Check if an object is an instance of jetLogger
64
- */
65
- declare function instanceOf(arg: unknown): arg is JetLogger;
66
53
  /******************************************************************************
67
54
  Export
68
55
  ******************************************************************************/
@@ -71,6 +58,5 @@ declare const _default: {
71
58
  readonly imp: LogFunction;
72
59
  readonly warn: LogFunction;
73
60
  readonly err: LogFunction;
74
- readonly [kJetLogger]: true;
75
61
  };
76
62
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jet-logger",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
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",