jet-logger 2.2.2 → 2.2.3

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,59 +1,59 @@
1
1
  import colors from 'colors';
2
2
  import fs from 'fs';
3
3
  import util from 'util';
4
- const LOGGER_MODES = {
5
- Console: 'CONSOLE',
6
- File: 'FILE',
7
- Custom: 'CUSTOM',
8
- Off: 'OFF',
4
+ const Modes = {
5
+ CONSOLE: 'console',
6
+ FILE: 'file',
7
+ CUSTOM: 'custom',
8
+ OFF: 'off',
9
9
  };
10
- const FORMATS = {
11
- Line: 'LINE',
12
- Json: 'JSON',
10
+ const Formats = {
11
+ LINE: 'line',
12
+ JSON: 'json',
13
13
  };
14
- const LEVELS = {
14
+ const Levels = {
15
15
  Info: {
16
- Color: 'green',
17
- Prefix: 'INFO',
16
+ COLOR: 'green',
17
+ PREFIX: 'INFO',
18
18
  },
19
19
  Important: {
20
- Color: 'magenta',
21
- Prefix: 'IMPORTANT',
20
+ COLOR: 'magenta',
21
+ PREFIX: 'IMPORTANT',
22
22
  },
23
23
  Warning: {
24
- Color: 'yellow',
25
- Prefix: 'WARNING',
24
+ COLOR: 'yellow',
25
+ PREFIX: 'WARNING',
26
26
  },
27
27
  Error: {
28
- Color: 'red',
29
- Prefix: 'ERROR',
28
+ COLOR: 'red',
29
+ PREFIX: 'ERROR',
30
30
  },
31
31
  };
32
- const DEFAULTS = {
33
- mode: LOGGER_MODES.Console,
34
- filePath: 'jet-logger.log',
35
- timestamp: true,
36
- format: FORMATS.Line,
37
- customLogFn: () => ({}),
32
+ const Defaults = {
33
+ MODE: Modes.CONSOLE,
34
+ FILE_PATH: 'jet-logger.log',
35
+ TIMESTAMP: true,
36
+ FORMAT: Formats.LINE,
37
+ CUSTOM_LOGGER_FUNCTION: () => ({}),
38
38
  };
39
39
  const Errors = {
40
- CustomLogger: 'Custom logger mode set to true, but no custom logger was provided.',
41
- Mode: 'The correct logger mode was not specified: Must be "CUSTOM", "FILE", ' +
42
- '"OFF", or "CONSOLE".',
40
+ CUSTOM_LOGGER: 'Custom logger mode set to true, but no custom logger was provided.',
41
+ MODE: 'The correct logger mode was not specified: Must be "custom", "file", ' +
42
+ '"off", or "console".',
43
43
  };
44
44
  export const JetLogger = {
45
- Modes: LOGGER_MODES,
46
- Formats: FORMATS,
45
+ Modes,
46
+ Formats,
47
47
  };
48
48
  export function jetLogger(options) {
49
- let mode = DEFAULTS.mode, filePath = DEFAULTS.filePath, timestamp = DEFAULTS.timestamp, format = DEFAULTS.format, customLogFn = DEFAULTS.customLogFn;
49
+ let mode = Defaults.MODE, filePath = Defaults.FILE_PATH, timestamp = Defaults.TIMESTAMP, format = Defaults.FORMAT, customLogFn = Defaults.CUSTOM_LOGGER_FUNCTION;
50
50
  if (options?.mode !== undefined) {
51
51
  mode = options.mode;
52
52
  }
53
53
  else if (!!process.env.JET_LOGGER_MODE) {
54
- mode = process.env.JET_LOGGER_MODE.toUpperCase();
54
+ mode = process.env.JET_LOGGER_MODE.toLowerCase();
55
55
  }
56
- if (mode === LOGGER_MODES.Off) {
56
+ if (mode === Modes.OFF) {
57
57
  return {
58
58
  info: (_, __) => ({}),
59
59
  imp: (_, __) => ({}),
@@ -61,18 +61,18 @@ export function jetLogger(options) {
61
61
  err: (_, __) => ({}),
62
62
  };
63
63
  }
64
- if (mode === LOGGER_MODES.Custom) {
64
+ if (mode === Modes.CUSTOM) {
65
65
  if (options?.customLogger !== undefined) {
66
66
  customLogFn = options.customLogger;
67
67
  }
68
68
  if (!customLogFn) {
69
- throw Error(Errors.CustomLogger);
69
+ throw Error(Errors.CUSTOM_LOGGER);
70
70
  }
71
71
  return {
72
- info: setupPrintWithCustomLogger(LEVELS.Info, customLogFn),
73
- imp: setupPrintWithCustomLogger(LEVELS.Important, customLogFn),
74
- warn: setupPrintWithCustomLogger(LEVELS.Warning, customLogFn),
75
- err: setupPrintWithCustomLogger(LEVELS.Error, customLogFn),
72
+ info: setupPrintWithCustomLogger(Levels.Info, customLogFn),
73
+ imp: setupPrintWithCustomLogger(Levels.Important, customLogFn),
74
+ warn: setupPrintWithCustomLogger(Levels.Warning, customLogFn),
75
+ err: setupPrintWithCustomLogger(Levels.Error, customLogFn),
76
76
  };
77
77
  }
78
78
  if (options?.filepath !== undefined) {
@@ -86,45 +86,45 @@ export function jetLogger(options) {
86
86
  }
87
87
  else if (!!process.env.JET_LOGGER_TIMESTAMP) {
88
88
  const envVar = process.env.JET_LOGGER_TIMESTAMP;
89
- timestamp = envVar.toUpperCase() === 'TRUE';
89
+ timestamp = envVar.toLowerCase() === 'true';
90
90
  }
91
91
  if (options?.format !== undefined) {
92
92
  format = options.format;
93
93
  }
94
94
  else if (!!process.env.JET_LOGGER_FORMAT) {
95
- format = process.env.JET_LOGGER_FORMAT.toUpperCase();
95
+ format = process.env.JET_LOGGER_FORMAT.toLowerCase();
96
96
  }
97
97
  let formatter = () => '';
98
- if (format === FORMATS.Line) {
98
+ if (format === Formats.LINE) {
99
99
  formatter = setupLineFormatter(timestamp);
100
100
  }
101
- else if (format === FORMATS.Json) {
101
+ else if (format === Formats.JSON) {
102
102
  formatter = setupJsonFormatter(timestamp);
103
103
  }
104
- if (mode === LOGGER_MODES.File) {
104
+ if (mode === Modes.FILE) {
105
105
  let filePathDatetime = true;
106
106
  if (options?.filepathDatetimeParam !== undefined) {
107
107
  filePathDatetime = options.filepathDatetimeParam;
108
108
  }
109
109
  else if (!!process.env.JET_LOGGER_FILEPATH_DATETIME) {
110
110
  const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
111
- filePathDatetime = envVar.toUpperCase() === 'TRUE';
111
+ filePathDatetime = envVar.toLowerCase() === 'true';
112
112
  }
113
113
  if (filePathDatetime) {
114
114
  filePath = addDatetimeToFileName(filePath);
115
115
  }
116
116
  return {
117
- info: setupPrintToFile(LEVELS.Info, formatter, filePath),
118
- imp: setupPrintToFile(LEVELS.Important, formatter, filePath),
119
- warn: setupPrintToFile(LEVELS.Warning, formatter, filePath),
120
- err: setupPrintToFile(LEVELS.Error, formatter, filePath),
117
+ info: setupPrintToFile(Levels.Info, formatter, filePath),
118
+ imp: setupPrintToFile(Levels.Important, formatter, filePath),
119
+ warn: setupPrintToFile(Levels.Warning, formatter, filePath),
120
+ err: setupPrintToFile(Levels.Error, formatter, filePath),
121
121
  };
122
122
  }
123
123
  return {
124
- info: setupPrintToConsole(LEVELS.Info, formatter),
125
- imp: setupPrintToConsole(LEVELS.Important, formatter),
126
- warn: setupPrintToConsole(LEVELS.Warning, formatter),
127
- err: setupPrintToConsole(LEVELS.Error, formatter),
124
+ info: setupPrintToConsole(Levels.Info, formatter),
125
+ imp: setupPrintToConsole(Levels.Important, formatter),
126
+ warn: setupPrintToConsole(Levels.Warning, formatter),
127
+ err: setupPrintToConsole(Levels.Error, formatter),
128
128
  };
129
129
  }
130
130
  function setupPrintWithCustomLogger(level, customLogFn) {
@@ -136,19 +136,19 @@ function setupPrintWithCustomLogger(level, customLogFn) {
136
136
  else {
137
137
  contentNew = String(content);
138
138
  }
139
- return customLogFn(new Date(), level.Prefix, contentNew);
139
+ return customLogFn(new Date(), level.PREFIX, contentNew);
140
140
  };
141
141
  }
142
142
  function setupLineFormatter(timestamp) {
143
143
  if (timestamp) {
144
144
  return (content, level) => {
145
- const contentNew = level.Prefix + ': ' + content, time = '[' + new Date().toISOString() + '] ';
145
+ const contentNew = level.PREFIX + ': ' + content, time = '[' + new Date().toISOString() + '] ';
146
146
  return time + contentNew;
147
147
  };
148
148
  }
149
149
  else {
150
150
  return (content, level) => {
151
- return level.Prefix + ': ' + content;
151
+ return level.PREFIX + ': ' + content;
152
152
  };
153
153
  }
154
154
  }
@@ -156,7 +156,7 @@ function setupJsonFormatter(timestamp) {
156
156
  if (timestamp) {
157
157
  return (content, level) => {
158
158
  const json = {
159
- level: level.Prefix,
159
+ level: level.PREFIX,
160
160
  message: content,
161
161
  };
162
162
  json.timestamp = new Date().toISOString();
@@ -166,7 +166,7 @@ function setupJsonFormatter(timestamp) {
166
166
  else {
167
167
  return (content, level) => {
168
168
  const json = {
169
- level: level.Prefix,
169
+ level: level.PREFIX,
170
170
  message: content,
171
171
  };
172
172
  return JSON.stringify(json);
@@ -183,7 +183,7 @@ function setupPrintToFile(level, formatter, filePath) {
183
183
  contentNew = String(content);
184
184
  }
185
185
  contentNew = formatter(contentNew, level);
186
- fs.appendFile(filePath, contentNew, (err) => {
186
+ fs.appendFile(filePath, contentNew + '\n', (err) => {
187
187
  if (!!err) {
188
188
  console.error(err);
189
189
  }
@@ -199,7 +199,7 @@ function setupPrintToConsole(level, formatter) {
199
199
  else {
200
200
  contentNew = String(content);
201
201
  }
202
- const colorFn = colors[level.Color];
202
+ const colorFn = colors[level.COLOR];
203
203
  contentNew = formatter(contentNew, level);
204
204
  console.log(colorFn(contentNew));
205
205
  };
@@ -1,59 +1,59 @@
1
1
  import colors from 'colors';
2
2
  import fs from 'fs';
3
3
  import util from 'util';
4
- const LOGGER_MODES = {
5
- Console: 'CONSOLE',
6
- File: 'FILE',
7
- Custom: 'CUSTOM',
8
- Off: 'OFF',
4
+ const Modes = {
5
+ CONSOLE: 'console',
6
+ FILE: 'file',
7
+ CUSTOM: 'custom',
8
+ OFF: 'off',
9
9
  };
10
- const FORMATS = {
11
- Line: 'LINE',
12
- Json: 'JSON',
10
+ const Formats = {
11
+ LINE: 'line',
12
+ JSON: 'json',
13
13
  };
14
- const LEVELS = {
14
+ const Levels = {
15
15
  Info: {
16
- Color: 'green',
17
- Prefix: 'INFO',
16
+ COLOR: 'green',
17
+ PREFIX: 'INFO',
18
18
  },
19
19
  Important: {
20
- Color: 'magenta',
21
- Prefix: 'IMPORTANT',
20
+ COLOR: 'magenta',
21
+ PREFIX: 'IMPORTANT',
22
22
  },
23
23
  Warning: {
24
- Color: 'yellow',
25
- Prefix: 'WARNING',
24
+ COLOR: 'yellow',
25
+ PREFIX: 'WARNING',
26
26
  },
27
27
  Error: {
28
- Color: 'red',
29
- Prefix: 'ERROR',
28
+ COLOR: 'red',
29
+ PREFIX: 'ERROR',
30
30
  },
31
31
  };
32
- const DEFAULTS = {
33
- mode: LOGGER_MODES.Console,
34
- filePath: 'jet-logger.log',
35
- timestamp: true,
36
- format: FORMATS.Line,
37
- customLogFn: () => ({}),
32
+ const Defaults = {
33
+ MODE: Modes.CONSOLE,
34
+ FILE_PATH: 'jet-logger.log',
35
+ TIMESTAMP: true,
36
+ FORMAT: Formats.LINE,
37
+ CUSTOM_LOGGER_FUNCTION: () => ({}),
38
38
  };
39
39
  const Errors = {
40
- CustomLogger: 'Custom logger mode set to true, but no custom logger was provided.',
41
- Mode: 'The correct logger mode was not specified: Must be "CUSTOM", "FILE", ' +
42
- '"OFF", or "CONSOLE".',
40
+ CUSTOM_LOGGER: 'Custom logger mode set to true, but no custom logger was provided.',
41
+ MODE: 'The correct logger mode was not specified: Must be "custom", "file", ' +
42
+ '"off", or "console".',
43
43
  };
44
44
  export const JetLogger = {
45
- Modes: LOGGER_MODES,
46
- Formats: FORMATS,
45
+ Modes,
46
+ Formats,
47
47
  };
48
48
  export function jetLogger(options) {
49
- let mode = DEFAULTS.mode, filePath = DEFAULTS.filePath, timestamp = DEFAULTS.timestamp, format = DEFAULTS.format, customLogFn = DEFAULTS.customLogFn;
49
+ let mode = Defaults.MODE, filePath = Defaults.FILE_PATH, timestamp = Defaults.TIMESTAMP, format = Defaults.FORMAT, customLogFn = Defaults.CUSTOM_LOGGER_FUNCTION;
50
50
  if (options?.mode !== undefined) {
51
51
  mode = options.mode;
52
52
  }
53
53
  else if (!!process.env.JET_LOGGER_MODE) {
54
- mode = process.env.JET_LOGGER_MODE.toUpperCase();
54
+ mode = process.env.JET_LOGGER_MODE.toLowerCase();
55
55
  }
56
- if (mode === LOGGER_MODES.Off) {
56
+ if (mode === Modes.OFF) {
57
57
  return {
58
58
  info: (_, __) => ({}),
59
59
  imp: (_, __) => ({}),
@@ -61,18 +61,18 @@ export function jetLogger(options) {
61
61
  err: (_, __) => ({}),
62
62
  };
63
63
  }
64
- if (mode === LOGGER_MODES.Custom) {
64
+ if (mode === Modes.CUSTOM) {
65
65
  if (options?.customLogger !== undefined) {
66
66
  customLogFn = options.customLogger;
67
67
  }
68
68
  if (!customLogFn) {
69
- throw Error(Errors.CustomLogger);
69
+ throw Error(Errors.CUSTOM_LOGGER);
70
70
  }
71
71
  return {
72
- info: setupPrintWithCustomLogger(LEVELS.Info, customLogFn),
73
- imp: setupPrintWithCustomLogger(LEVELS.Important, customLogFn),
74
- warn: setupPrintWithCustomLogger(LEVELS.Warning, customLogFn),
75
- err: setupPrintWithCustomLogger(LEVELS.Error, customLogFn),
72
+ info: setupPrintWithCustomLogger(Levels.Info, customLogFn),
73
+ imp: setupPrintWithCustomLogger(Levels.Important, customLogFn),
74
+ warn: setupPrintWithCustomLogger(Levels.Warning, customLogFn),
75
+ err: setupPrintWithCustomLogger(Levels.Error, customLogFn),
76
76
  };
77
77
  }
78
78
  if (options?.filepath !== undefined) {
@@ -86,45 +86,45 @@ export function jetLogger(options) {
86
86
  }
87
87
  else if (!!process.env.JET_LOGGER_TIMESTAMP) {
88
88
  const envVar = process.env.JET_LOGGER_TIMESTAMP;
89
- timestamp = envVar.toUpperCase() === 'TRUE';
89
+ timestamp = envVar.toLowerCase() === 'true';
90
90
  }
91
91
  if (options?.format !== undefined) {
92
92
  format = options.format;
93
93
  }
94
94
  else if (!!process.env.JET_LOGGER_FORMAT) {
95
- format = process.env.JET_LOGGER_FORMAT.toUpperCase();
95
+ format = process.env.JET_LOGGER_FORMAT.toLowerCase();
96
96
  }
97
97
  let formatter = () => '';
98
- if (format === FORMATS.Line) {
98
+ if (format === Formats.LINE) {
99
99
  formatter = setupLineFormatter(timestamp);
100
100
  }
101
- else if (format === FORMATS.Json) {
101
+ else if (format === Formats.JSON) {
102
102
  formatter = setupJsonFormatter(timestamp);
103
103
  }
104
- if (mode === LOGGER_MODES.File) {
104
+ if (mode === Modes.FILE) {
105
105
  let filePathDatetime = true;
106
106
  if (options?.filepathDatetimeParam !== undefined) {
107
107
  filePathDatetime = options.filepathDatetimeParam;
108
108
  }
109
109
  else if (!!process.env.JET_LOGGER_FILEPATH_DATETIME) {
110
110
  const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
111
- filePathDatetime = envVar.toUpperCase() === 'TRUE';
111
+ filePathDatetime = envVar.toLowerCase() === 'true';
112
112
  }
113
113
  if (filePathDatetime) {
114
114
  filePath = addDatetimeToFileName(filePath);
115
115
  }
116
116
  return {
117
- info: setupPrintToFile(LEVELS.Info, formatter, filePath),
118
- imp: setupPrintToFile(LEVELS.Important, formatter, filePath),
119
- warn: setupPrintToFile(LEVELS.Warning, formatter, filePath),
120
- err: setupPrintToFile(LEVELS.Error, formatter, filePath),
117
+ info: setupPrintToFile(Levels.Info, formatter, filePath),
118
+ imp: setupPrintToFile(Levels.Important, formatter, filePath),
119
+ warn: setupPrintToFile(Levels.Warning, formatter, filePath),
120
+ err: setupPrintToFile(Levels.Error, formatter, filePath),
121
121
  };
122
122
  }
123
123
  return {
124
- info: setupPrintToConsole(LEVELS.Info, formatter),
125
- imp: setupPrintToConsole(LEVELS.Important, formatter),
126
- warn: setupPrintToConsole(LEVELS.Warning, formatter),
127
- err: setupPrintToConsole(LEVELS.Error, formatter),
124
+ info: setupPrintToConsole(Levels.Info, formatter),
125
+ imp: setupPrintToConsole(Levels.Important, formatter),
126
+ warn: setupPrintToConsole(Levels.Warning, formatter),
127
+ err: setupPrintToConsole(Levels.Error, formatter),
128
128
  };
129
129
  }
130
130
  function setupPrintWithCustomLogger(level, customLogFn) {
@@ -136,19 +136,19 @@ function setupPrintWithCustomLogger(level, customLogFn) {
136
136
  else {
137
137
  contentNew = String(content);
138
138
  }
139
- return customLogFn(new Date(), level.Prefix, contentNew);
139
+ return customLogFn(new Date(), level.PREFIX, contentNew);
140
140
  };
141
141
  }
142
142
  function setupLineFormatter(timestamp) {
143
143
  if (timestamp) {
144
144
  return (content, level) => {
145
- const contentNew = level.Prefix + ': ' + content, time = '[' + new Date().toISOString() + '] ';
145
+ const contentNew = level.PREFIX + ': ' + content, time = '[' + new Date().toISOString() + '] ';
146
146
  return time + contentNew;
147
147
  };
148
148
  }
149
149
  else {
150
150
  return (content, level) => {
151
- return level.Prefix + ': ' + content;
151
+ return level.PREFIX + ': ' + content;
152
152
  };
153
153
  }
154
154
  }
@@ -156,7 +156,7 @@ function setupJsonFormatter(timestamp) {
156
156
  if (timestamp) {
157
157
  return (content, level) => {
158
158
  const json = {
159
- level: level.Prefix,
159
+ level: level.PREFIX,
160
160
  message: content,
161
161
  };
162
162
  json.timestamp = new Date().toISOString();
@@ -166,7 +166,7 @@ function setupJsonFormatter(timestamp) {
166
166
  else {
167
167
  return (content, level) => {
168
168
  const json = {
169
- level: level.Prefix,
169
+ level: level.PREFIX,
170
170
  message: content,
171
171
  };
172
172
  return JSON.stringify(json);
@@ -183,7 +183,7 @@ function setupPrintToFile(level, formatter, filePath) {
183
183
  contentNew = String(content);
184
184
  }
185
185
  contentNew = formatter(contentNew, level);
186
- fs.appendFile(filePath, contentNew, (err) => {
186
+ fs.appendFile(filePath, contentNew + '\n', (err) => {
187
187
  if (!!err) {
188
188
  console.error(err);
189
189
  }
@@ -199,7 +199,7 @@ function setupPrintToConsole(level, formatter) {
199
199
  else {
200
200
  contentNew = String(content);
201
201
  }
202
- const colorFn = colors[level.Color];
202
+ const colorFn = colors[level.COLOR];
203
203
  contentNew = formatter(contentNew, level);
204
204
  console.log(colorFn(contentNew));
205
205
  };
@@ -1,37 +1,37 @@
1
1
  /******************************************************************************
2
2
  Constants
3
3
  ******************************************************************************/
4
- declare const LOGGER_MODES: {
5
- readonly Console: "CONSOLE";
6
- readonly File: "FILE";
7
- readonly Custom: "CUSTOM";
8
- readonly Off: "OFF";
4
+ declare const Modes: {
5
+ readonly CONSOLE: "console";
6
+ readonly FILE: "file";
7
+ readonly CUSTOM: "custom";
8
+ readonly OFF: "off";
9
9
  };
10
- declare const FORMATS: {
11
- readonly Line: "LINE";
12
- readonly Json: "JSON";
10
+ declare const Formats: {
11
+ readonly LINE: "line";
12
+ readonly JSON: "json";
13
13
  };
14
14
  export declare const JetLogger: {
15
15
  readonly Modes: {
16
- readonly Console: "CONSOLE";
17
- readonly File: "FILE";
18
- readonly Custom: "CUSTOM";
19
- readonly Off: "OFF";
16
+ readonly CONSOLE: "console";
17
+ readonly FILE: "file";
18
+ readonly CUSTOM: "custom";
19
+ readonly OFF: "off";
20
20
  };
21
21
  readonly Formats: {
22
- readonly Line: "LINE";
23
- readonly Json: "JSON";
22
+ readonly LINE: "line";
23
+ readonly JSON: "json";
24
24
  };
25
25
  };
26
26
  /******************************************************************************
27
27
  Types
28
28
  ******************************************************************************/
29
- type LoggerModes = (typeof LOGGER_MODES)[keyof typeof LOGGER_MODES];
30
- type Formats = (typeof FORMATS)[keyof typeof FORMATS];
29
+ type Modes = (typeof Modes)[keyof typeof Modes];
30
+ type Formats = (typeof Formats)[keyof typeof Formats];
31
31
  type LogFunction = (content: unknown, printFull?: boolean) => void;
32
32
  export type CustomLogger = (timestamp: Date, prefix: string, content: unknown) => void;
33
33
  interface Options {
34
- mode?: LoggerModes;
34
+ mode?: Modes;
35
35
  filepath?: string;
36
36
  filepathDatetimeParam?: boolean;
37
37
  timestamp?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jet-logger",
3
- "version": "2.2.2",
3
+ "version": "2.2.3",
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",