jet-logger 1.2.2 → 1.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.
@@ -0,0 +1,31 @@
1
+ export declare enum LoggerModes {
2
+ Console = "CONSOLE",
3
+ File = "FILE",
4
+ Custom = "CUSTOM",
5
+ Off = "OFF"
6
+ }
7
+ export declare enum Formats {
8
+ Line = "LINE",
9
+ Json = "JSON"
10
+ }
11
+ declare type TJetLogger = ReturnType<typeof JetLogger>;
12
+ export declare type TCustomLogger = (timestamp: Date, prefix: string, content: any) => void;
13
+ export declare function JetLogger(mode?: LoggerModes, filepath?: string, filepathDatetime?: boolean, timestamp?: boolean, format?: Formats, customLogger?: TCustomLogger): {
14
+ readonly settings: {
15
+ readonly mode: LoggerModes;
16
+ readonly filepath: string;
17
+ readonly filepathDatetime: boolean;
18
+ readonly timestamp: boolean;
19
+ readonly format: Formats;
20
+ readonly customLogger: TCustomLogger | undefined;
21
+ };
22
+ readonly info: typeof info;
23
+ readonly imp: typeof imp;
24
+ readonly warn: typeof warn;
25
+ readonly err: typeof err;
26
+ };
27
+ declare function info(this: TJetLogger, content: any, printFull?: boolean): void;
28
+ declare function imp(this: TJetLogger, content: any, printFull?: boolean): void;
29
+ declare function warn(this: TJetLogger, content: any, printFull?: boolean): void;
30
+ declare function err(this: TJetLogger, content: any, printFull?: boolean): void;
31
+ export {};
@@ -0,0 +1,189 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.JetLogger = exports.Formats = exports.LoggerModes = void 0;
7
+ var util_1 = __importDefault(require("util"));
8
+ var colors_1 = __importDefault(require("colors"));
9
+ var fs_1 = __importDefault(require("fs"));
10
+ var LoggerModes;
11
+ (function (LoggerModes) {
12
+ LoggerModes["Console"] = "CONSOLE";
13
+ LoggerModes["File"] = "FILE";
14
+ LoggerModes["Custom"] = "CUSTOM";
15
+ LoggerModes["Off"] = "OFF";
16
+ })(LoggerModes = exports.LoggerModes || (exports.LoggerModes = {}));
17
+ var Formats;
18
+ (function (Formats) {
19
+ Formats["Line"] = "LINE";
20
+ Formats["Json"] = "JSON";
21
+ })(Formats = exports.Formats || (exports.Formats = {}));
22
+ var levels = {
23
+ info: {
24
+ color: 'green',
25
+ prefix: 'INFO',
26
+ },
27
+ imp: {
28
+ color: 'magenta',
29
+ prefix: 'IMPORTANT',
30
+ },
31
+ warn: {
32
+ color: 'yellow',
33
+ prefix: 'WARNING',
34
+ },
35
+ err: {
36
+ color: 'red',
37
+ prefix: 'ERROR',
38
+ }
39
+ };
40
+ var errors = {
41
+ customLoggerErr: 'Custom logger mode set to true, but no custom logger was provided.',
42
+ modeErr: 'The correct logger mode was not specified: Must be "CUSTOM", "FILE", ' +
43
+ '"OFF", or "CONSOLE".'
44
+ };
45
+ var defaults = {
46
+ filepath: 'jet-logger.log',
47
+ mode: LoggerModes.Console,
48
+ timestamp: true,
49
+ filepathDatetime: true,
50
+ format: Formats.Line,
51
+ };
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 };
55
+ }
56
+ exports.JetLogger = JetLogger;
57
+ function getSettings(mode, filepath, filepathDatetime, timestamp, format, customLogger) {
58
+ if (!mode) {
59
+ if (!!process.env.JET_LOGGER_MODE) {
60
+ mode = process.env.JET_LOGGER_MODE.toUpperCase();
61
+ }
62
+ else {
63
+ mode = defaults.mode;
64
+ }
65
+ }
66
+ if (!filepath) {
67
+ if (!!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');
78
+ }
79
+ else {
80
+ filepathDatetime = defaults.filepathDatetime;
81
+ }
82
+ }
83
+ if (timestamp === undefined || timestamp === null) {
84
+ if (!!process.env.JET_LOGGER_TIMESTAMP) {
85
+ timestamp = (process.env.JET_LOGGER_TIMESTAMP.toUpperCase() === 'TRUE');
86
+ }
87
+ else {
88
+ timestamp = defaults.timestamp;
89
+ }
90
+ }
91
+ if (!format) {
92
+ if (!!process.env.JET_LOGGER_FORMAT) {
93
+ format = process.env.JET_LOGGER_FORMAT.toUpperCase();
94
+ }
95
+ else {
96
+ format = defaults.format;
97
+ }
98
+ }
99
+ if (filepathDatetime) {
100
+ filepath = addDatetimeToFileName(filepath);
101
+ }
102
+ return {
103
+ mode: mode,
104
+ filepath: filepath,
105
+ filepathDatetime: filepathDatetime,
106
+ timestamp: timestamp,
107
+ format: format,
108
+ customLogger: customLogger,
109
+ };
110
+ }
111
+ function addDatetimeToFileName(filePath) {
112
+ var dateStr = new Date().toISOString()
113
+ .split('-').join('')
114
+ .split(':').join('')
115
+ .slice(0, 15);
116
+ var filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = (dateStr + '_' + fileName);
117
+ filePathArr[lastIdx] = fileNameNew;
118
+ return filePathArr.join('/');
119
+ }
120
+ function info(content, printFull) {
121
+ return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.info, this.settings);
122
+ }
123
+ function imp(content, printFull) {
124
+ return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.imp, this.settings);
125
+ }
126
+ function warn(content, printFull) {
127
+ return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.warn, this.settings);
128
+ }
129
+ function err(content, printFull) {
130
+ return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.err, this.settings);
131
+ }
132
+ function printLog(content, printFull, level, settings) {
133
+ var mode = settings.mode, format = settings.format, timestamp = settings.timestamp, filepath = settings.filepath, customLogger = settings.customLogger;
134
+ if (mode === LoggerModes.Off) {
135
+ return;
136
+ }
137
+ var jsonContent = {};
138
+ if (printFull) {
139
+ content = util_1.default.inspect(content);
140
+ }
141
+ if (format === Formats.Json) {
142
+ jsonContent.message = content;
143
+ }
144
+ if (mode !== LoggerModes.Custom) {
145
+ if (format === Formats.Line) {
146
+ content = level.prefix + ': ' + content;
147
+ }
148
+ else if (format === Formats.Json) {
149
+ jsonContent.level = level.prefix;
150
+ }
151
+ }
152
+ if (timestamp) {
153
+ if (format === Formats.Line) {
154
+ var time = '[' + new Date().toISOString() + '] ';
155
+ content = (time + content);
156
+ }
157
+ else if (format === Formats.Json) {
158
+ jsonContent.timestamp = new Date().toISOString();
159
+ }
160
+ }
161
+ if (format === Formats.Json) {
162
+ content = JSON.stringify(jsonContent);
163
+ }
164
+ if (mode === LoggerModes.Console) {
165
+ var colorFn = colors_1.default[level.color];
166
+ console.log(colorFn(content));
167
+ }
168
+ else if (mode === LoggerModes.File) {
169
+ writeToFile(content + '\n', filepath)
170
+ .catch(function (err) { return console.log(err); });
171
+ }
172
+ else if (mode === LoggerModes.Custom) {
173
+ if (!!customLogger) {
174
+ customLogger(new Date(), level.prefix, content);
175
+ }
176
+ else {
177
+ throw Error(errors.customLoggerErr);
178
+ }
179
+ }
180
+ else {
181
+ throw Error(errors.modeErr);
182
+ }
183
+ }
184
+ function writeToFile(content, filePath) {
185
+ return new Promise(function (res, rej) {
186
+ var fn = (function (err) { return !!err ? rej(err) : res(); });
187
+ return fs_1.default.appendFile(filePath, content, fn);
188
+ });
189
+ }
package/lib/index.d.ts CHANGED
@@ -1,45 +1,16 @@
1
- export declare enum LoggerModes {
2
- Console = "CONSOLE",
3
- File = "FILE",
4
- Custom = "CUSTOM",
5
- Off = "OFF"
6
- }
7
- export declare enum Formats {
8
- Line = "LINE",
9
- Json = "JSON"
10
- }
11
- declare type TJetLogger = ReturnType<typeof JetLogger>;
12
- export declare type TCustomLogger = (timestamp: Date, prefix: string, content: any) => void;
13
- export declare function JetLogger(mode?: LoggerModes, filepath?: string, filepathDatetime?: boolean, timestamp?: boolean, format?: Formats, customLogger?: TCustomLogger): {
14
- readonly settings: {
15
- readonly mode: LoggerModes;
16
- readonly filepath: string;
17
- readonly filepathDatetime: boolean;
18
- readonly timestamp: boolean;
19
- readonly format: Formats;
20
- readonly customLogger: TCustomLogger | undefined;
21
- };
22
- readonly info: typeof info;
23
- readonly imp: typeof imp;
24
- readonly warn: typeof warn;
25
- readonly err: typeof err;
26
- };
27
- declare function info(this: TJetLogger, content: any, printFull?: boolean): void;
28
- declare function imp(this: TJetLogger, content: any, printFull?: boolean): void;
29
- declare function warn(this: TJetLogger, content: any, printFull?: boolean): void;
30
- declare function err(this: TJetLogger, content: any, printFull?: boolean): void;
1
+ export { LoggerModes, Formats, TCustomLogger, } from './JetLogger';
31
2
  declare const _default: {
32
3
  readonly settings: {
33
- readonly mode: LoggerModes;
4
+ readonly mode: import("./JetLogger").LoggerModes;
34
5
  readonly filepath: string;
35
6
  readonly filepathDatetime: boolean;
36
7
  readonly timestamp: boolean;
37
- readonly format: Formats;
38
- readonly customLogger: TCustomLogger | undefined;
8
+ readonly format: import("./JetLogger").Formats;
9
+ readonly customLogger: import("./JetLogger").TCustomLogger | undefined;
39
10
  };
40
- readonly info: typeof info;
41
- readonly imp: typeof imp;
42
- readonly warn: typeof warn;
43
- readonly err: typeof err;
11
+ readonly info: (this: any, content: any, printFull?: boolean | undefined) => void;
12
+ readonly imp: (this: any, content: any, printFull?: boolean | undefined) => void;
13
+ readonly warn: (this: any, content: any, printFull?: boolean | undefined) => void;
14
+ readonly err: (this: any, content: any, printFull?: boolean | undefined) => void;
44
15
  };
45
16
  export default _default;
package/lib/index.js CHANGED
@@ -1,190 +1,8 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.JetLogger = exports.Formats = exports.LoggerModes = void 0;
7
- var util_1 = __importDefault(require("util"));
8
- var colors_1 = __importDefault(require("colors"));
9
- var fs_1 = __importDefault(require("fs"));
10
- var LoggerModes;
11
- (function (LoggerModes) {
12
- LoggerModes["Console"] = "CONSOLE";
13
- LoggerModes["File"] = "FILE";
14
- LoggerModes["Custom"] = "CUSTOM";
15
- LoggerModes["Off"] = "OFF";
16
- })(LoggerModes = exports.LoggerModes || (exports.LoggerModes = {}));
17
- var Formats;
18
- (function (Formats) {
19
- Formats["Line"] = "LINE";
20
- Formats["Json"] = "JSON";
21
- })(Formats = exports.Formats || (exports.Formats = {}));
22
- var levels = {
23
- info: {
24
- color: 'green',
25
- prefix: 'INFO',
26
- },
27
- imp: {
28
- color: 'magenta',
29
- prefix: 'IMPORTANT',
30
- },
31
- warn: {
32
- color: 'yellow',
33
- prefix: 'WARNING',
34
- },
35
- err: {
36
- color: 'red',
37
- prefix: 'ERROR',
38
- }
39
- };
40
- var errors = {
41
- customLoggerErr: 'Custom logger mode set to true, but no custom logger was provided.',
42
- modeErr: 'The correct logger mode was not specified: Must be "CUSTOM", "FILE", ' +
43
- '"OFF", or "CONSOLE".'
44
- };
45
- var defaults = {
46
- filepath: 'jet-logger.log',
47
- mode: LoggerModes.Console,
48
- timestamp: true,
49
- filepathDatetime: true,
50
- format: Formats.Line,
51
- };
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 };
55
- }
56
- exports.JetLogger = JetLogger;
57
- function getSettings(mode, filepath, filepathDatetime, timestamp, format, customLogger) {
58
- if (!mode) {
59
- if (!!process.env.JET_LOGGER_MODE) {
60
- mode = process.env.JET_LOGGER_MODE.toUpperCase();
61
- }
62
- else {
63
- mode = defaults.mode;
64
- }
65
- }
66
- if (!filepath) {
67
- if (!!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');
78
- }
79
- else {
80
- filepathDatetime = defaults.filepathDatetime;
81
- }
82
- }
83
- if (timestamp === undefined || timestamp === null) {
84
- if (!!process.env.JET_LOGGER_TIMESTAMP) {
85
- timestamp = (process.env.JET_LOGGER_TIMESTAMP.toUpperCase() === 'TRUE');
86
- }
87
- else {
88
- timestamp = defaults.timestamp;
89
- }
90
- }
91
- if (!format) {
92
- if (!!process.env.JET_LOGGER_FORMAT) {
93
- format = process.env.JET_LOGGER_FORMAT.toUpperCase();
94
- }
95
- else {
96
- format = defaults.format;
97
- }
98
- }
99
- if (filepathDatetime) {
100
- filepath = addDatetimeToFileName(filepath);
101
- }
102
- return {
103
- mode: mode,
104
- filepath: filepath,
105
- filepathDatetime: filepathDatetime,
106
- timestamp: timestamp,
107
- format: format,
108
- customLogger: customLogger,
109
- };
110
- }
111
- function info(content, printFull) {
112
- return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.info, this.settings);
113
- }
114
- function imp(content, printFull) {
115
- return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.imp, this.settings);
116
- }
117
- function warn(content, printFull) {
118
- return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.warn, this.settings);
119
- }
120
- function err(content, printFull) {
121
- return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.err, this.settings);
122
- }
123
- function printLog(content, printFull, level, settings) {
124
- var mode = settings.mode, format = settings.format, timestamp = settings.timestamp, filepath = settings.filepath, customLogger = settings.customLogger;
125
- if (mode === LoggerModes.Off) {
126
- return;
127
- }
128
- var jsonContent = {};
129
- if (printFull) {
130
- content = util_1.default.inspect(content);
131
- }
132
- if (format === Formats.Json) {
133
- jsonContent.message = content;
134
- }
135
- if (mode !== LoggerModes.Custom) {
136
- if (format === Formats.Line) {
137
- content = level.prefix + ': ' + content;
138
- }
139
- else if (format === Formats.Json) {
140
- jsonContent.level = level.prefix;
141
- }
142
- }
143
- if (timestamp) {
144
- if (format === Formats.Line) {
145
- var time = '[' + new Date().toISOString() + '] ';
146
- content = (time + content);
147
- }
148
- else if (format === Formats.Json) {
149
- jsonContent.timestamp = new Date().toISOString();
150
- }
151
- }
152
- if (format === Formats.Json) {
153
- content = JSON.stringify(jsonContent);
154
- }
155
- if (mode === LoggerModes.Console) {
156
- var colorFn = colors_1.default[level.color];
157
- console.log(colorFn(content));
158
- }
159
- else if (mode === LoggerModes.File) {
160
- writeToFile(content + '\n', filepath)
161
- .catch(function (err) { return console.log(err); });
162
- }
163
- else if (mode === LoggerModes.Custom) {
164
- if (!!customLogger) {
165
- customLogger(new Date(), level.prefix, content);
166
- }
167
- else {
168
- throw Error(errors.customLoggerErr);
169
- }
170
- }
171
- else {
172
- throw Error(errors.modeErr);
173
- }
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
- }
184
- function writeToFile(content, filePath) {
185
- return new Promise(function (res, rej) {
186
- var fn = (function (err) { return !!err ? rej(err) : res(); });
187
- return fs_1.default.appendFile(filePath, content, fn);
188
- });
189
- }
190
- exports.default = JetLogger();
3
+ exports.Formats = exports.LoggerModes = void 0;
4
+ var JetLogger_1 = require("./JetLogger");
5
+ var JetLogger_2 = require("./JetLogger");
6
+ Object.defineProperty(exports, "LoggerModes", { enumerable: true, get: function () { return JetLogger_2.LoggerModes; } });
7
+ Object.defineProperty(exports, "Formats", { enumerable: true, get: function () { return JetLogger_2.Formats; } });
8
+ exports.default = (0, JetLogger_1.JetLogger)();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jet-logger",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
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",