jet-logger 2.1.0 → 2.2.0

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/dist/cjs/index.js CHANGED
@@ -1 +1 @@
1
- export { LoggerModes, Formats, JetLogger, default as default, } from './JetLogger.js';
1
+ export { JetLogger, jetLogger, default, } from './jetLogger.js';
@@ -0,0 +1,285 @@
1
+ /* eslint-disable no-console */
2
+ /* eslint-disable no-process-env */
3
+ import colors from 'colors';
4
+ import fs from 'fs';
5
+ import util from 'util';
6
+ /******************************************************************************
7
+ Constants
8
+ ******************************************************************************/
9
+ // Options for printing a log.
10
+ const LOGGER_MODES = {
11
+ Console: 'CONSOLE',
12
+ File: 'FILE',
13
+ Custom: 'CUSTOM',
14
+ Off: 'OFF',
15
+ };
16
+ // Log formats
17
+ const FORMATS = {
18
+ Line: 'LINE',
19
+ Json: 'JSON',
20
+ };
21
+ // Note colors here need be a color from
22
+ // the colors library above.
23
+ const LEVELS = {
24
+ Info: {
25
+ Color: 'green',
26
+ Prefix: 'INFO',
27
+ },
28
+ Important: {
29
+ Color: 'magenta',
30
+ Prefix: 'IMPORTANT',
31
+ },
32
+ Warning: {
33
+ Color: 'yellow',
34
+ Prefix: 'WARNING',
35
+ },
36
+ Error: {
37
+ Color: 'red',
38
+ Prefix: 'ERROR',
39
+ },
40
+ };
41
+ const DEFAULTS = {
42
+ mode: LOGGER_MODES.Console,
43
+ filePath: 'jet-logger.log',
44
+ timestamp: true,
45
+ format: FORMATS.Line,
46
+ customLogFn: () => ({}),
47
+ };
48
+ // Errors
49
+ const Errors = {
50
+ CustomLogger: 'Custom logger mode set to true, but no custom logger was provided.',
51
+ Mode: 'The correct logger mode was not specified: Must be "CUSTOM", "FILE", ' +
52
+ '"OFF", or "CONSOLE".',
53
+ };
54
+ export const JetLogger = {
55
+ Modes: LOGGER_MODES,
56
+ Formats: FORMATS,
57
+ instanceOf,
58
+ };
59
+ const kJetLogger = Symbol('k-jet-logger');
60
+ /******************************************************************************
61
+ Functions
62
+ ******************************************************************************/
63
+ /**
64
+ * Default function
65
+ */
66
+ export function jetLogger(options) {
67
+ let mode = DEFAULTS.mode, filePath = DEFAULTS.filePath, timestamp = DEFAULTS.timestamp, format = DEFAULTS.format, customLogFn = DEFAULTS.customLogFn;
68
+ // Setup the mode
69
+ if (options?.mode !== undefined) {
70
+ mode = options.mode;
71
+ }
72
+ else if (!!process.env.JET_LOGGER_MODE) {
73
+ mode = process.env.JET_LOGGER_MODE.toUpperCase();
74
+ }
75
+ // ** Logger Mode Off ** //
76
+ if (mode === LOGGER_MODES.Off) {
77
+ return {
78
+ info: (_, __) => ({}),
79
+ imp: (_, __) => ({}),
80
+ warn: (_, __) => ({}),
81
+ err: (_, __) => ({}),
82
+ [kJetLogger]: true,
83
+ };
84
+ }
85
+ // ** Custom Logger Function ** //
86
+ if (mode === LOGGER_MODES.Custom) {
87
+ if (options?.customLogger !== undefined) {
88
+ customLogFn = options.customLogger;
89
+ }
90
+ if (!customLogFn) {
91
+ throw Error(Errors.CustomLogger);
92
+ }
93
+ return {
94
+ info: setupPrintWithCustomLogger(LEVELS.Info, customLogFn),
95
+ imp: setupPrintWithCustomLogger(LEVELS.Important, customLogFn),
96
+ warn: setupPrintWithCustomLogger(LEVELS.Warning, customLogFn),
97
+ err: setupPrintWithCustomLogger(LEVELS.Error, customLogFn),
98
+ [kJetLogger]: true,
99
+ };
100
+ }
101
+ // Filepath
102
+ if (options?.filepath !== undefined) {
103
+ filePath = options.filepath;
104
+ }
105
+ else if (!!process.env.JET_LOGGER_FILEPATH) {
106
+ filePath = process.env.JET_LOGGER_FILEPATH;
107
+ }
108
+ // Timestamp
109
+ if (options?.timestamp !== undefined) {
110
+ timestamp = options.timestamp;
111
+ }
112
+ else if (!!process.env.JET_LOGGER_TIMESTAMP) {
113
+ const envVar = process.env.JET_LOGGER_TIMESTAMP;
114
+ timestamp = envVar.toUpperCase() === 'TRUE';
115
+ }
116
+ // Format
117
+ if (options?.format !== undefined) {
118
+ format = options.format;
119
+ }
120
+ else if (!!process.env.JET_LOGGER_FORMAT) {
121
+ format = process.env.JET_LOGGER_FORMAT.toUpperCase();
122
+ }
123
+ // Setup the formatter
124
+ let formatter = () => '';
125
+ if (format === FORMATS.Line) {
126
+ formatter = setupLineFormatter(timestamp);
127
+ }
128
+ else if (format === FORMATS.Json) {
129
+ formatter = setupJsonFormatter(timestamp);
130
+ }
131
+ // ** Print to File ** //
132
+ if (mode === LOGGER_MODES.File) {
133
+ // FilePath dateTime
134
+ let filePathDatetime = true;
135
+ if (options?.filepathDatetimeParam !== undefined) {
136
+ filePathDatetime = options.filepathDatetimeParam;
137
+ }
138
+ else if (!!process.env.JET_LOGGER_FILEPATH_DATETIME) {
139
+ const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
140
+ filePathDatetime = envVar.toUpperCase() === 'TRUE';
141
+ }
142
+ // Modify filepath if filepath datetime is true
143
+ if (filePathDatetime) {
144
+ filePath = addDatetimeToFileName(filePath);
145
+ }
146
+ // Return
147
+ return {
148
+ info: setupPrintToFile(LEVELS.Info, formatter, filePath),
149
+ imp: setupPrintToFile(LEVELS.Important, formatter, filePath),
150
+ warn: setupPrintToFile(LEVELS.Warning, formatter, filePath),
151
+ err: setupPrintToFile(LEVELS.Error, formatter, filePath),
152
+ [kJetLogger]: true,
153
+ };
154
+ }
155
+ // Console (Default)
156
+ return {
157
+ info: setupPrintToConsole(LEVELS.Info, formatter),
158
+ imp: setupPrintToConsole(LEVELS.Important, formatter),
159
+ warn: setupPrintToConsole(LEVELS.Warning, formatter),
160
+ err: setupPrintToConsole(LEVELS.Error, formatter),
161
+ [kJetLogger]: true,
162
+ };
163
+ }
164
+ /**
165
+ * Print a log with a custom logger function.
166
+ */
167
+ function setupPrintWithCustomLogger(level, customLogFn) {
168
+ return (content, printFull) => {
169
+ let contentNew;
170
+ if (printFull) {
171
+ contentNew = util.inspect(content);
172
+ }
173
+ else {
174
+ contentNew = String(content);
175
+ }
176
+ return customLogFn(new Date(), level.Prefix, contentNew);
177
+ };
178
+ }
179
+ /**
180
+ * Setup line format.
181
+ */
182
+ function setupLineFormatter(timestamp) {
183
+ if (timestamp) {
184
+ return (content, level) => {
185
+ const contentNew = level.Prefix + ': ' + content, time = '[' + new Date().toISOString() + '] ';
186
+ return time + contentNew;
187
+ };
188
+ }
189
+ else {
190
+ return (content, level) => {
191
+ return level.Prefix + ': ' + content;
192
+ };
193
+ }
194
+ }
195
+ /**
196
+ * Setup json format.
197
+ */
198
+ function setupJsonFormatter(timestamp) {
199
+ if (timestamp) {
200
+ return (content, level) => {
201
+ const json = {
202
+ level: level.Prefix,
203
+ message: content,
204
+ };
205
+ json.timestamp = new Date().toISOString();
206
+ return JSON.stringify(json);
207
+ };
208
+ }
209
+ else {
210
+ return (content, level) => {
211
+ const json = {
212
+ level: level.Prefix,
213
+ message: content,
214
+ };
215
+ return JSON.stringify(json);
216
+ };
217
+ }
218
+ }
219
+ /**
220
+ * Write to file.
221
+ */
222
+ function setupPrintToFile(level, formatter, filePath) {
223
+ return (content, printFull) => {
224
+ let contentNew;
225
+ if (!!printFull) {
226
+ contentNew = util.inspect(content);
227
+ }
228
+ else {
229
+ contentNew = String(content);
230
+ }
231
+ contentNew = formatter(contentNew, level);
232
+ fs.appendFile(filePath, contentNew, (err) => {
233
+ if (!!err) {
234
+ console.error(err);
235
+ }
236
+ });
237
+ };
238
+ }
239
+ /**
240
+ * Print a log to the console.
241
+ */
242
+ function setupPrintToConsole(level, formatter) {
243
+ return (content, printFull) => {
244
+ let contentNew;
245
+ if (!!printFull) {
246
+ contentNew = util.inspect(content);
247
+ }
248
+ else {
249
+ contentNew = String(content);
250
+ }
251
+ const colorFn = colors[level.Color];
252
+ contentNew = formatter(contentNew, level);
253
+ console.log(colorFn(contentNew));
254
+ };
255
+ }
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
+ function addDatetimeToFileName(filePath) {
261
+ // Get the date string
262
+ const dateStr = new Date()
263
+ .toISOString()
264
+ .split('-')
265
+ .join('')
266
+ .split(':')
267
+ .join('')
268
+ .slice(0, 15);
269
+ // Setup new file name
270
+ const filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = dateStr + '_' + fileName;
271
+ // Setup new file path
272
+ filePathArr[lastIdx] = fileNameNew;
273
+ return filePathArr.join('/');
274
+ }
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
+ export default jetLogger();
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- export { LoggerModes, Formats, JetLogger, default as default, } from './JetLogger.js';
1
+ export { JetLogger, jetLogger, default, } from './jetLogger.js';
@@ -0,0 +1,285 @@
1
+ /* eslint-disable no-console */
2
+ /* eslint-disable no-process-env */
3
+ import colors from 'colors';
4
+ import fs from 'fs';
5
+ import util from 'util';
6
+ /******************************************************************************
7
+ Constants
8
+ ******************************************************************************/
9
+ // Options for printing a log.
10
+ const LOGGER_MODES = {
11
+ Console: 'CONSOLE',
12
+ File: 'FILE',
13
+ Custom: 'CUSTOM',
14
+ Off: 'OFF',
15
+ };
16
+ // Log formats
17
+ const FORMATS = {
18
+ Line: 'LINE',
19
+ Json: 'JSON',
20
+ };
21
+ // Note colors here need be a color from
22
+ // the colors library above.
23
+ const LEVELS = {
24
+ Info: {
25
+ Color: 'green',
26
+ Prefix: 'INFO',
27
+ },
28
+ Important: {
29
+ Color: 'magenta',
30
+ Prefix: 'IMPORTANT',
31
+ },
32
+ Warning: {
33
+ Color: 'yellow',
34
+ Prefix: 'WARNING',
35
+ },
36
+ Error: {
37
+ Color: 'red',
38
+ Prefix: 'ERROR',
39
+ },
40
+ };
41
+ const DEFAULTS = {
42
+ mode: LOGGER_MODES.Console,
43
+ filePath: 'jet-logger.log',
44
+ timestamp: true,
45
+ format: FORMATS.Line,
46
+ customLogFn: () => ({}),
47
+ };
48
+ // Errors
49
+ const Errors = {
50
+ CustomLogger: 'Custom logger mode set to true, but no custom logger was provided.',
51
+ Mode: 'The correct logger mode was not specified: Must be "CUSTOM", "FILE", ' +
52
+ '"OFF", or "CONSOLE".',
53
+ };
54
+ export const JetLogger = {
55
+ Modes: LOGGER_MODES,
56
+ Formats: FORMATS,
57
+ instanceOf,
58
+ };
59
+ const kJetLogger = Symbol('k-jet-logger');
60
+ /******************************************************************************
61
+ Functions
62
+ ******************************************************************************/
63
+ /**
64
+ * Default function
65
+ */
66
+ export function jetLogger(options) {
67
+ let mode = DEFAULTS.mode, filePath = DEFAULTS.filePath, timestamp = DEFAULTS.timestamp, format = DEFAULTS.format, customLogFn = DEFAULTS.customLogFn;
68
+ // Setup the mode
69
+ if (options?.mode !== undefined) {
70
+ mode = options.mode;
71
+ }
72
+ else if (!!process.env.JET_LOGGER_MODE) {
73
+ mode = process.env.JET_LOGGER_MODE.toUpperCase();
74
+ }
75
+ // ** Logger Mode Off ** //
76
+ if (mode === LOGGER_MODES.Off) {
77
+ return {
78
+ info: (_, __) => ({}),
79
+ imp: (_, __) => ({}),
80
+ warn: (_, __) => ({}),
81
+ err: (_, __) => ({}),
82
+ [kJetLogger]: true,
83
+ };
84
+ }
85
+ // ** Custom Logger Function ** //
86
+ if (mode === LOGGER_MODES.Custom) {
87
+ if (options?.customLogger !== undefined) {
88
+ customLogFn = options.customLogger;
89
+ }
90
+ if (!customLogFn) {
91
+ throw Error(Errors.CustomLogger);
92
+ }
93
+ return {
94
+ info: setupPrintWithCustomLogger(LEVELS.Info, customLogFn),
95
+ imp: setupPrintWithCustomLogger(LEVELS.Important, customLogFn),
96
+ warn: setupPrintWithCustomLogger(LEVELS.Warning, customLogFn),
97
+ err: setupPrintWithCustomLogger(LEVELS.Error, customLogFn),
98
+ [kJetLogger]: true,
99
+ };
100
+ }
101
+ // Filepath
102
+ if (options?.filepath !== undefined) {
103
+ filePath = options.filepath;
104
+ }
105
+ else if (!!process.env.JET_LOGGER_FILEPATH) {
106
+ filePath = process.env.JET_LOGGER_FILEPATH;
107
+ }
108
+ // Timestamp
109
+ if (options?.timestamp !== undefined) {
110
+ timestamp = options.timestamp;
111
+ }
112
+ else if (!!process.env.JET_LOGGER_TIMESTAMP) {
113
+ const envVar = process.env.JET_LOGGER_TIMESTAMP;
114
+ timestamp = envVar.toUpperCase() === 'TRUE';
115
+ }
116
+ // Format
117
+ if (options?.format !== undefined) {
118
+ format = options.format;
119
+ }
120
+ else if (!!process.env.JET_LOGGER_FORMAT) {
121
+ format = process.env.JET_LOGGER_FORMAT.toUpperCase();
122
+ }
123
+ // Setup the formatter
124
+ let formatter = () => '';
125
+ if (format === FORMATS.Line) {
126
+ formatter = setupLineFormatter(timestamp);
127
+ }
128
+ else if (format === FORMATS.Json) {
129
+ formatter = setupJsonFormatter(timestamp);
130
+ }
131
+ // ** Print to File ** //
132
+ if (mode === LOGGER_MODES.File) {
133
+ // FilePath dateTime
134
+ let filePathDatetime = true;
135
+ if (options?.filepathDatetimeParam !== undefined) {
136
+ filePathDatetime = options.filepathDatetimeParam;
137
+ }
138
+ else if (!!process.env.JET_LOGGER_FILEPATH_DATETIME) {
139
+ const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
140
+ filePathDatetime = envVar.toUpperCase() === 'TRUE';
141
+ }
142
+ // Modify filepath if filepath datetime is true
143
+ if (filePathDatetime) {
144
+ filePath = addDatetimeToFileName(filePath);
145
+ }
146
+ // Return
147
+ return {
148
+ info: setupPrintToFile(LEVELS.Info, formatter, filePath),
149
+ imp: setupPrintToFile(LEVELS.Important, formatter, filePath),
150
+ warn: setupPrintToFile(LEVELS.Warning, formatter, filePath),
151
+ err: setupPrintToFile(LEVELS.Error, formatter, filePath),
152
+ [kJetLogger]: true,
153
+ };
154
+ }
155
+ // Console (Default)
156
+ return {
157
+ info: setupPrintToConsole(LEVELS.Info, formatter),
158
+ imp: setupPrintToConsole(LEVELS.Important, formatter),
159
+ warn: setupPrintToConsole(LEVELS.Warning, formatter),
160
+ err: setupPrintToConsole(LEVELS.Error, formatter),
161
+ [kJetLogger]: true,
162
+ };
163
+ }
164
+ /**
165
+ * Print a log with a custom logger function.
166
+ */
167
+ function setupPrintWithCustomLogger(level, customLogFn) {
168
+ return (content, printFull) => {
169
+ let contentNew;
170
+ if (printFull) {
171
+ contentNew = util.inspect(content);
172
+ }
173
+ else {
174
+ contentNew = String(content);
175
+ }
176
+ return customLogFn(new Date(), level.Prefix, contentNew);
177
+ };
178
+ }
179
+ /**
180
+ * Setup line format.
181
+ */
182
+ function setupLineFormatter(timestamp) {
183
+ if (timestamp) {
184
+ return (content, level) => {
185
+ const contentNew = level.Prefix + ': ' + content, time = '[' + new Date().toISOString() + '] ';
186
+ return time + contentNew;
187
+ };
188
+ }
189
+ else {
190
+ return (content, level) => {
191
+ return level.Prefix + ': ' + content;
192
+ };
193
+ }
194
+ }
195
+ /**
196
+ * Setup json format.
197
+ */
198
+ function setupJsonFormatter(timestamp) {
199
+ if (timestamp) {
200
+ return (content, level) => {
201
+ const json = {
202
+ level: level.Prefix,
203
+ message: content,
204
+ };
205
+ json.timestamp = new Date().toISOString();
206
+ return JSON.stringify(json);
207
+ };
208
+ }
209
+ else {
210
+ return (content, level) => {
211
+ const json = {
212
+ level: level.Prefix,
213
+ message: content,
214
+ };
215
+ return JSON.stringify(json);
216
+ };
217
+ }
218
+ }
219
+ /**
220
+ * Write to file.
221
+ */
222
+ function setupPrintToFile(level, formatter, filePath) {
223
+ return (content, printFull) => {
224
+ let contentNew;
225
+ if (!!printFull) {
226
+ contentNew = util.inspect(content);
227
+ }
228
+ else {
229
+ contentNew = String(content);
230
+ }
231
+ contentNew = formatter(contentNew, level);
232
+ fs.appendFile(filePath, contentNew, (err) => {
233
+ if (!!err) {
234
+ console.error(err);
235
+ }
236
+ });
237
+ };
238
+ }
239
+ /**
240
+ * Print a log to the console.
241
+ */
242
+ function setupPrintToConsole(level, formatter) {
243
+ return (content, printFull) => {
244
+ let contentNew;
245
+ if (!!printFull) {
246
+ contentNew = util.inspect(content);
247
+ }
248
+ else {
249
+ contentNew = String(content);
250
+ }
251
+ const colorFn = colors[level.Color];
252
+ contentNew = formatter(contentNew, level);
253
+ console.log(colorFn(contentNew));
254
+ };
255
+ }
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
+ function addDatetimeToFileName(filePath) {
261
+ // Get the date string
262
+ const dateStr = new Date()
263
+ .toISOString()
264
+ .split('-')
265
+ .join('')
266
+ .split(':')
267
+ .join('')
268
+ .slice(0, 15);
269
+ // Setup new file name
270
+ const filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = dateStr + '_' + fileName;
271
+ // Setup new file path
272
+ filePathArr[lastIdx] = fileNameNew;
273
+ return filePathArr.join('/');
274
+ }
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
+ export default jetLogger();
@@ -1 +1 @@
1
- export { LoggerModes, Formats, type TCustomLoggerFunction, JetLogger, default as default, } from './JetLogger.js';
1
+ export { type CustomLogger, JetLogger, jetLogger, default, } from './jetLogger.js';
@@ -0,0 +1,76 @@
1
+ /******************************************************************************
2
+ Constants
3
+ ******************************************************************************/
4
+ declare const LOGGER_MODES: {
5
+ readonly Console: "CONSOLE";
6
+ readonly File: "FILE";
7
+ readonly Custom: "CUSTOM";
8
+ readonly Off: "OFF";
9
+ };
10
+ declare const FORMATS: {
11
+ readonly Line: "LINE";
12
+ readonly Json: "JSON";
13
+ };
14
+ export declare const JetLogger: {
15
+ readonly Modes: {
16
+ readonly Console: "CONSOLE";
17
+ readonly File: "FILE";
18
+ readonly Custom: "CUSTOM";
19
+ readonly Off: "OFF";
20
+ };
21
+ readonly Formats: {
22
+ readonly Line: "LINE";
23
+ readonly Json: "JSON";
24
+ };
25
+ readonly instanceOf: typeof instanceOf;
26
+ };
27
+ declare const kJetLogger: unique symbol;
28
+ /******************************************************************************
29
+ Types
30
+ ******************************************************************************/
31
+ type LoggerModes = (typeof LOGGER_MODES)[keyof typeof LOGGER_MODES];
32
+ type Formats = (typeof FORMATS)[keyof typeof FORMATS];
33
+ type LogFunction = (content: unknown, printFull?: boolean) => void;
34
+ export type CustomLogger = (timestamp: Date, prefix: string, content: unknown) => void;
35
+ interface Options {
36
+ mode?: LoggerModes;
37
+ filepath?: string;
38
+ filepathDatetimeParam?: boolean;
39
+ timestamp?: boolean;
40
+ format?: Formats;
41
+ customLogger?: CustomLogger;
42
+ }
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
+ /******************************************************************************
50
+ Functions
51
+ ******************************************************************************/
52
+ /**
53
+ * Default function
54
+ */
55
+ export declare function jetLogger(options?: Options): {
56
+ readonly info: LogFunction;
57
+ readonly imp: LogFunction;
58
+ readonly warn: LogFunction;
59
+ readonly err: LogFunction;
60
+ readonly [kJetLogger]: true;
61
+ };
62
+ /**
63
+ * Check if an object is an instance of jetLogger
64
+ */
65
+ declare function instanceOf(arg: unknown): arg is JetLogger;
66
+ /******************************************************************************
67
+ Export
68
+ ******************************************************************************/
69
+ declare const _default: {
70
+ readonly info: LogFunction;
71
+ readonly imp: LogFunction;
72
+ readonly warn: LogFunction;
73
+ readonly err: LogFunction;
74
+ readonly [kJetLogger]: true;
75
+ };
76
+ export default _default;
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "jet-logger",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "A super quick, easy to setup logging tool for NodeJS/TypeScript.",
5
5
  "type": "module",
6
- "main": "./dist/cjs/index.cjs",
6
+ "main": "./dist/cjs/index.js",
7
7
  "module": "./dist/esm/index.js",
8
8
  "browser": "./dist/esm/index.js",
9
9
  "types": "./dist/types/index.d.ts",
10
10
  "exports": {
11
11
  ".": {
12
12
  "import": "./dist/esm/index.js",
13
- "require": "./dist/cjs/index.cjs",
13
+ "require": "./dist/cjs/index.js",
14
14
  "types": "./dist/types/index.d.ts"
15
15
  }
16
16
  },
@@ -59,11 +59,12 @@
59
59
  },
60
60
  "homepage": "https://github.com/seanpmaxwell/jet-logger#readme",
61
61
  "dependencies": {
62
- "colors": "1.3.0"
62
+ "colors": "1.4.0"
63
63
  },
64
64
  "devDependencies": {
65
65
  "@eslint/js": "^9.26.0",
66
66
  "@stylistic/eslint-plugin": "^5.6.1",
67
+ "@trivago/prettier-plugin-sort-imports": "^6.0.1",
67
68
  "@types/node": "^22.8.1",
68
69
  "eslint": "^9.26.0",
69
70
  "eslint-config-prettier": "^10.1.8",
@@ -1,239 +0,0 @@
1
- /* eslint-disable no-process-env */
2
- import util from 'util';
3
- import colors from 'colors';
4
- import fs from 'fs';
5
- /******************************************************************************
6
- Variables
7
- ******************************************************************************/
8
- // Options for printing a log.
9
- export const LoggerModes = {
10
- Console: 'CONSOLE',
11
- File: 'FILE',
12
- Custom: 'CUSTOM',
13
- Off: 'OFF',
14
- };
15
- // Log formats
16
- export const Formats = {
17
- Line: 'LINE',
18
- Json: 'JSON',
19
- };
20
- // Note colors here need be a color from
21
- // the colors library above.
22
- const Levels = {
23
- Info: {
24
- Color: 'green',
25
- Prefix: 'INFO',
26
- },
27
- Important: {
28
- Color: 'magenta',
29
- Prefix: 'IMPORTANT',
30
- },
31
- Warning: {
32
- Color: 'yellow',
33
- Prefix: 'WARNING',
34
- },
35
- Error: {
36
- Color: 'red',
37
- Prefix: 'ERROR',
38
- },
39
- };
40
- // Errors
41
- const Errors = {
42
- CustomLogFn: 'Custom logger mode set to true, but no custom logger was ' + 'provided.',
43
- Mode: 'The correct logger mode was not specified: Must be "CUSTOM", ' +
44
- '"FILE", "OFF", or "CONSOLE".',
45
- };
46
- /******************************************************************************
47
- Classes
48
- ******************************************************************************/
49
- export class JetLogger {
50
- mode = LoggerModes.Console;
51
- filePath = 'jet-logger.log';
52
- timestamp = true;
53
- format = Formats.Line;
54
- customLogFn = () => ({});
55
- /**
56
- * Constructor
57
- */
58
- constructor(mode, filepath, filepathDatetimeParam, timestamp, format, customLogFn) {
59
- // Setup the mode
60
- if (mode !== undefined) {
61
- this.mode = mode;
62
- }
63
- else if (!!process.env.JET_LOGGER_MODE) {
64
- this.mode = process.env.JET_LOGGER_MODE.toUpperCase();
65
- }
66
- // Filepath
67
- if (filepath !== undefined) {
68
- this.filePath = filepath;
69
- }
70
- else if (!!process.env.JET_LOGGER_FILEPATH) {
71
- this.filePath = process.env.JET_LOGGER_FILEPATH;
72
- }
73
- // FilePath dateTime
74
- let filePathDatetime = true;
75
- if (filepathDatetimeParam !== undefined) {
76
- filePathDatetime = filepathDatetimeParam;
77
- }
78
- else if (!!process.env.JET_LOGGER_FILEPATH_DATETIME) {
79
- const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
80
- filePathDatetime = envVar.toUpperCase() === 'TRUE';
81
- }
82
- // Timestamp
83
- if (timestamp !== undefined) {
84
- this.timestamp = timestamp;
85
- }
86
- else if (!!process.env.JET_LOGGER_TIMESTAMP) {
87
- const envVar = process.env.JET_LOGGER_TIMESTAMP;
88
- this.timestamp = envVar.toUpperCase() === 'TRUE';
89
- }
90
- // Format
91
- if (format !== undefined) {
92
- this.format = format;
93
- }
94
- else if (!!process.env.JET_LOGGER_FORMAT) {
95
- this.format = process.env.JET_LOGGER_FORMAT.toUpperCase();
96
- }
97
- // Modify filepath if filepath datetime is true
98
- if (filePathDatetime) {
99
- this.filePath = this.addDatetimeToFileName(this.filePath);
100
- }
101
- // Custom Logger Function
102
- if (customLogFn !== undefined) {
103
- this.customLogFn = customLogFn;
104
- }
105
- }
106
- /**
107
- * Prepend the filename in the file path with a timestamp.
108
- * i.e. '/home/jet-logger.log' => '/home/20220805T033709_jet-logger.log'
109
- */
110
- addDatetimeToFileName(filePath) {
111
- // Get the date string
112
- const dateStr = new Date()
113
- .toISOString()
114
- .split('-')
115
- .join('')
116
- .split(':')
117
- .join('')
118
- .slice(0, 15);
119
- // Setup new file name
120
- const filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = dateStr + '_' + fileName;
121
- // Setup new file path
122
- filePathArr[lastIdx] = fileNameNew;
123
- return filePathArr.join('/');
124
- }
125
- /**
126
- * Print information.
127
- */
128
- info(content, printFull) {
129
- this.printLog(content, !!printFull, Levels.Info);
130
- }
131
- /**
132
- * Print important information.
133
- */
134
- imp(content, printFull) {
135
- this.printLog(content, !!printFull, Levels.Important);
136
- }
137
- /**
138
- * Print important information.
139
- */
140
- warn(content, printFull) {
141
- this.printLog(content, !!printFull, Levels.Warning);
142
- }
143
- /**
144
- * Print important information.
145
- */
146
- err(content, printFull) {
147
- this.printLog(content, !!printFull, Levels.Error);
148
- }
149
- /**
150
- * Print the log using the provided settings.
151
- */
152
- printLog(contentParam, printFull, level) {
153
- // Do nothing if turned off
154
- if (this.mode === LoggerModes.Off) {
155
- return;
156
- }
157
- // Print full
158
- let content;
159
- if (printFull) {
160
- content = util.inspect(contentParam);
161
- }
162
- else {
163
- content = String(contentParam);
164
- }
165
- // Fire the custom logger if that's the option
166
- if (this.mode === LoggerModes.Custom) {
167
- if (!!this.customLogFn) {
168
- return this.customLogFn(new Date(), level.Prefix, content);
169
- }
170
- else {
171
- throw Error(Errors.CustomLogFn);
172
- }
173
- }
174
- // Print line or json
175
- if (this.format === Formats.Line) {
176
- content = this.setupLineFormat(content, level);
177
- }
178
- else if (this.format === Formats.Json) {
179
- content = this.setupJsonFormat(content, level);
180
- }
181
- // Print Console
182
- if (this.mode === LoggerModes.Console) {
183
- const colorFn = colors[level.Color];
184
- // eslint-disable-next-line no-console
185
- console.log(colorFn(content));
186
- // Print File
187
- }
188
- else if (this.mode === LoggerModes.File) {
189
- this.writeToFile(content + '\n');
190
- // If reach this point, mode setting was bad
191
- }
192
- else {
193
- throw Error(Errors.Mode);
194
- }
195
- }
196
- /**
197
- * Setup line format.
198
- */
199
- setupLineFormat(content, level) {
200
- // Format
201
- content = level.Prefix + ': ' + content;
202
- if (this.timestamp) {
203
- const time = '[' + new Date().toISOString() + '] ';
204
- return time + content;
205
- }
206
- // Return
207
- return content;
208
- }
209
- /**
210
- * Setup json format.
211
- */
212
- setupJsonFormat(content, level) {
213
- // Format
214
- const json = {
215
- level: level.Prefix,
216
- message: content,
217
- };
218
- if (this.timestamp) {
219
- json.timestamp = new Date().toISOString();
220
- }
221
- // Return
222
- return JSON.stringify(json);
223
- }
224
- /**
225
- * Write to file.
226
- */
227
- writeToFile(content) {
228
- fs.appendFile(this.filePath, content, (err) => {
229
- if (!!err) {
230
- // eslint-disable-next-line no-console
231
- console.error(err);
232
- }
233
- });
234
- }
235
- }
236
- /******************************************************************************
237
- Export
238
- ******************************************************************************/
239
- export default new JetLogger();
@@ -1,239 +0,0 @@
1
- /* eslint-disable no-process-env */
2
- import util from 'util';
3
- import colors from 'colors';
4
- import fs from 'fs';
5
- /******************************************************************************
6
- Variables
7
- ******************************************************************************/
8
- // Options for printing a log.
9
- export const LoggerModes = {
10
- Console: 'CONSOLE',
11
- File: 'FILE',
12
- Custom: 'CUSTOM',
13
- Off: 'OFF',
14
- };
15
- // Log formats
16
- export const Formats = {
17
- Line: 'LINE',
18
- Json: 'JSON',
19
- };
20
- // Note colors here need be a color from
21
- // the colors library above.
22
- const Levels = {
23
- Info: {
24
- Color: 'green',
25
- Prefix: 'INFO',
26
- },
27
- Important: {
28
- Color: 'magenta',
29
- Prefix: 'IMPORTANT',
30
- },
31
- Warning: {
32
- Color: 'yellow',
33
- Prefix: 'WARNING',
34
- },
35
- Error: {
36
- Color: 'red',
37
- Prefix: 'ERROR',
38
- },
39
- };
40
- // Errors
41
- const Errors = {
42
- CustomLogFn: 'Custom logger mode set to true, but no custom logger was ' + 'provided.',
43
- Mode: 'The correct logger mode was not specified: Must be "CUSTOM", ' +
44
- '"FILE", "OFF", or "CONSOLE".',
45
- };
46
- /******************************************************************************
47
- Classes
48
- ******************************************************************************/
49
- export class JetLogger {
50
- mode = LoggerModes.Console;
51
- filePath = 'jet-logger.log';
52
- timestamp = true;
53
- format = Formats.Line;
54
- customLogFn = () => ({});
55
- /**
56
- * Constructor
57
- */
58
- constructor(mode, filepath, filepathDatetimeParam, timestamp, format, customLogFn) {
59
- // Setup the mode
60
- if (mode !== undefined) {
61
- this.mode = mode;
62
- }
63
- else if (!!process.env.JET_LOGGER_MODE) {
64
- this.mode = process.env.JET_LOGGER_MODE.toUpperCase();
65
- }
66
- // Filepath
67
- if (filepath !== undefined) {
68
- this.filePath = filepath;
69
- }
70
- else if (!!process.env.JET_LOGGER_FILEPATH) {
71
- this.filePath = process.env.JET_LOGGER_FILEPATH;
72
- }
73
- // FilePath dateTime
74
- let filePathDatetime = true;
75
- if (filepathDatetimeParam !== undefined) {
76
- filePathDatetime = filepathDatetimeParam;
77
- }
78
- else if (!!process.env.JET_LOGGER_FILEPATH_DATETIME) {
79
- const envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
80
- filePathDatetime = envVar.toUpperCase() === 'TRUE';
81
- }
82
- // Timestamp
83
- if (timestamp !== undefined) {
84
- this.timestamp = timestamp;
85
- }
86
- else if (!!process.env.JET_LOGGER_TIMESTAMP) {
87
- const envVar = process.env.JET_LOGGER_TIMESTAMP;
88
- this.timestamp = envVar.toUpperCase() === 'TRUE';
89
- }
90
- // Format
91
- if (format !== undefined) {
92
- this.format = format;
93
- }
94
- else if (!!process.env.JET_LOGGER_FORMAT) {
95
- this.format = process.env.JET_LOGGER_FORMAT.toUpperCase();
96
- }
97
- // Modify filepath if filepath datetime is true
98
- if (filePathDatetime) {
99
- this.filePath = this.addDatetimeToFileName(this.filePath);
100
- }
101
- // Custom Logger Function
102
- if (customLogFn !== undefined) {
103
- this.customLogFn = customLogFn;
104
- }
105
- }
106
- /**
107
- * Prepend the filename in the file path with a timestamp.
108
- * i.e. '/home/jet-logger.log' => '/home/20220805T033709_jet-logger.log'
109
- */
110
- addDatetimeToFileName(filePath) {
111
- // Get the date string
112
- const dateStr = new Date()
113
- .toISOString()
114
- .split('-')
115
- .join('')
116
- .split(':')
117
- .join('')
118
- .slice(0, 15);
119
- // Setup new file name
120
- const filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = dateStr + '_' + fileName;
121
- // Setup new file path
122
- filePathArr[lastIdx] = fileNameNew;
123
- return filePathArr.join('/');
124
- }
125
- /**
126
- * Print information.
127
- */
128
- info(content, printFull) {
129
- this.printLog(content, !!printFull, Levels.Info);
130
- }
131
- /**
132
- * Print important information.
133
- */
134
- imp(content, printFull) {
135
- this.printLog(content, !!printFull, Levels.Important);
136
- }
137
- /**
138
- * Print important information.
139
- */
140
- warn(content, printFull) {
141
- this.printLog(content, !!printFull, Levels.Warning);
142
- }
143
- /**
144
- * Print important information.
145
- */
146
- err(content, printFull) {
147
- this.printLog(content, !!printFull, Levels.Error);
148
- }
149
- /**
150
- * Print the log using the provided settings.
151
- */
152
- printLog(contentParam, printFull, level) {
153
- // Do nothing if turned off
154
- if (this.mode === LoggerModes.Off) {
155
- return;
156
- }
157
- // Print full
158
- let content;
159
- if (printFull) {
160
- content = util.inspect(contentParam);
161
- }
162
- else {
163
- content = String(contentParam);
164
- }
165
- // Fire the custom logger if that's the option
166
- if (this.mode === LoggerModes.Custom) {
167
- if (!!this.customLogFn) {
168
- return this.customLogFn(new Date(), level.Prefix, content);
169
- }
170
- else {
171
- throw Error(Errors.CustomLogFn);
172
- }
173
- }
174
- // Print line or json
175
- if (this.format === Formats.Line) {
176
- content = this.setupLineFormat(content, level);
177
- }
178
- else if (this.format === Formats.Json) {
179
- content = this.setupJsonFormat(content, level);
180
- }
181
- // Print Console
182
- if (this.mode === LoggerModes.Console) {
183
- const colorFn = colors[level.Color];
184
- // eslint-disable-next-line no-console
185
- console.log(colorFn(content));
186
- // Print File
187
- }
188
- else if (this.mode === LoggerModes.File) {
189
- this.writeToFile(content + '\n');
190
- // If reach this point, mode setting was bad
191
- }
192
- else {
193
- throw Error(Errors.Mode);
194
- }
195
- }
196
- /**
197
- * Setup line format.
198
- */
199
- setupLineFormat(content, level) {
200
- // Format
201
- content = level.Prefix + ': ' + content;
202
- if (this.timestamp) {
203
- const time = '[' + new Date().toISOString() + '] ';
204
- return time + content;
205
- }
206
- // Return
207
- return content;
208
- }
209
- /**
210
- * Setup json format.
211
- */
212
- setupJsonFormat(content, level) {
213
- // Format
214
- const json = {
215
- level: level.Prefix,
216
- message: content,
217
- };
218
- if (this.timestamp) {
219
- json.timestamp = new Date().toISOString();
220
- }
221
- // Return
222
- return JSON.stringify(json);
223
- }
224
- /**
225
- * Write to file.
226
- */
227
- writeToFile(content) {
228
- fs.appendFile(this.filePath, content, (err) => {
229
- if (!!err) {
230
- // eslint-disable-next-line no-console
231
- console.error(err);
232
- }
233
- });
234
- }
235
- }
236
- /******************************************************************************
237
- Export
238
- ******************************************************************************/
239
- export default new JetLogger();
@@ -1,75 +0,0 @@
1
- /******************************************************************************
2
- Variables
3
- ******************************************************************************/
4
- export declare const LoggerModes: {
5
- readonly Console: "CONSOLE";
6
- readonly File: "FILE";
7
- readonly Custom: "CUSTOM";
8
- readonly Off: "OFF";
9
- };
10
- export declare const Formats: {
11
- readonly Line: "LINE";
12
- readonly Json: "JSON";
13
- };
14
- /******************************************************************************
15
- Types
16
- ******************************************************************************/
17
- type TLoggerModes = (typeof LoggerModes)[keyof typeof LoggerModes];
18
- type TFormats = (typeof Formats)[keyof typeof Formats];
19
- export type TCustomLoggerFunction = (timestamp: Date, prefix: string, content: unknown) => void;
20
- /******************************************************************************
21
- Classes
22
- ******************************************************************************/
23
- export declare class JetLogger {
24
- private mode;
25
- private filePath;
26
- private timestamp;
27
- private format;
28
- private customLogFn;
29
- /**
30
- * Constructor
31
- */
32
- constructor(mode?: TLoggerModes, filepath?: string, filepathDatetimeParam?: boolean, timestamp?: boolean, format?: TFormats, customLogFn?: TCustomLoggerFunction);
33
- /**
34
- * Prepend the filename in the file path with a timestamp.
35
- * i.e. '/home/jet-logger.log' => '/home/20220805T033709_jet-logger.log'
36
- */
37
- private addDatetimeToFileName;
38
- /**
39
- * Print information.
40
- */
41
- info(content: unknown, printFull?: boolean): void;
42
- /**
43
- * Print important information.
44
- */
45
- imp(content: unknown, printFull?: boolean): void;
46
- /**
47
- * Print important information.
48
- */
49
- warn(content: unknown, printFull?: boolean): void;
50
- /**
51
- * Print important information.
52
- */
53
- err(content: unknown, printFull?: boolean): void;
54
- /**
55
- * Print the log using the provided settings.
56
- */
57
- private printLog;
58
- /**
59
- * Setup line format.
60
- */
61
- private setupLineFormat;
62
- /**
63
- * Setup json format.
64
- */
65
- private setupJsonFormat;
66
- /**
67
- * Write to file.
68
- */
69
- private writeToFile;
70
- }
71
- /******************************************************************************
72
- Export
73
- ******************************************************************************/
74
- declare const _default: JetLogger;
75
- export default _default;