jet-logger 1.2.1 → 1.2.4
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/README.md +29 -30
- package/lib/JetLogger.d.ts +31 -0
- package/lib/JetLogger.js +188 -0
- package/lib/index.d.ts +8 -37
- package/lib/index.js +6 -189
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,9 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
## What is it
|
|
7
|
-
Jet-Logger is an easy to configure logging tool that allows you change settings via the environment
|
|
8
|
-
variables (recommended) or manually in code. You can easily switch your logs to be printed out to the command line, a file, sent through your own custom logging logic, or turned off completely. Logs printed to the console also are printed out in different colors depending on whether they're info, a warning, an error, etc. The file for holding logs can be specified manually or left as the default. You can also have
|
|
9
|
-
logs formatted as lines for easy reading or as JSON objects.
|
|
7
|
+
Jet-Logger is an easy to configure logging tool that allows you change settings via the environment variables (recommended) or manually in code. You can easily switch your logs to be printed out to the command line, a file, sent through your own custom logging logic, or turned off completely. Logs printed to the console also are printed out in different colors depending on whether they're info, a warning, an error, etc. The file for holding logs can be specified manually or left as the default. You can also have logs formatted as lines for easy reading or as JSON objects.
|
|
10
8
|
<br/>
|
|
11
9
|
|
|
12
10
|
### Installation
|
|
@@ -17,14 +15,14 @@ $ npm install --save jet-logger
|
|
|
17
15
|
### Guide
|
|
18
16
|
The logger package's main export is the `logger` object. Logger can used statically or as an instance
|
|
19
17
|
object with settings configured through a constructor. Variables passed through the constructor will
|
|
20
|
-
take priority over environment variables. Note that file writes happen asynchronously.
|
|
18
|
+
take priority over environment variables. Note that file writes happen asynchronously.
|
|
21
19
|
|
|
22
20
|
- The four environment variables are:
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
- `JET_LOGGER_MODE`: can be `'CONSOLE'`(default), `'FILE'`, `'CUSTOM'`, and `'OFF'`.
|
|
22
|
+
- `JET_LOGGER_FILEPATH`: the file-path for file mode. Default is `_home_dir/jet-logger.log_`.
|
|
23
|
+
- `JET_LOGGER_FILEPATH_DATETIME`: prepend the log file name with the datetime. Can be `'TRUE'` (default) or `'FALSE'`.
|
|
24
|
+
- `JET_LOGGER_TIMESTAMP`: adds a timestamp next to each log. Can be `'TRUE'` (default) or `'FALSE'`.
|
|
25
|
+
- `JET_LOGGER_FORMAT`: formats log as a line or JSON object. Can be `'LINE'` (default) or `'JSON'`.
|
|
28
26
|
|
|
29
27
|
_logger_ has an export `LoggerModes` which is an enum that provides all the modes if you want to
|
|
30
28
|
use them in code. I would recommend using `Console` for local development, `File` for remote development,
|
|
@@ -32,17 +30,18 @@ and `Custom` or `Off` for production. If you want to change the settings in code
|
|
|
32
30
|
<br>
|
|
33
31
|
|
|
34
32
|
- There are 4 functions on Logger to print logs.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
- `info`: prints green.
|
|
34
|
+
- `imp`: prints magenta.
|
|
35
|
+
- `warn`: prints yellow.
|
|
36
|
+
- `err`: prints red.
|
|
39
37
|
|
|
40
38
|
There is an optional second param to each method which is a `boolean`. If you pass `true` as the second
|
|
41
39
|
param, JetLogger will use node's `util` so that the full object gets printed. You should NOT normally
|
|
42
40
|
use this param, but it is especially useful when debugging errors so that you can print out the full
|
|
43
41
|
error object and observe the stack trace.<br>
|
|
44
42
|
|
|
45
|
-
Let's look at some sample code in an express route
|
|
43
|
+
Let's look at some sample code in an express route. Here's a link to a sample-project which displays similiar to what's below (https://github.com/seanpmaxwell/jet-logger-sample).
|
|
44
|
+
|
|
46
45
|
|
|
47
46
|
````typescript
|
|
48
47
|
/* Some script that is run before the route script */
|
|
@@ -64,15 +63,15 @@ const router = Router();
|
|
|
64
63
|
|
|
65
64
|
|
|
66
65
|
router.get('api/users/alt', async (req: Request, res: Reponse) => {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
66
|
+
logger.info(req.params.msg);
|
|
67
|
+
logger.imp(req.params.msg);
|
|
68
|
+
logger.warn(req.params.msg);
|
|
69
|
+
logger.err(req.params.msg);
|
|
70
|
+
logger.err(new Error('printing out an error'));
|
|
71
|
+
logger.err(new Error('printing out an error full'), true); // <-- print the full Error object
|
|
72
|
+
return res.status(OK).json({
|
|
73
|
+
message: 'console_mode',
|
|
74
|
+
});
|
|
76
75
|
});
|
|
77
76
|
````
|
|
78
77
|
|
|
@@ -111,15 +110,15 @@ import { thirdPartyLoggingApp } from 'thirdPartyLoggingApplicationLib';
|
|
|
111
110
|
|
|
112
111
|
// Needs to be implemented
|
|
113
112
|
const customSend: TCustomLogger = (timestamp: Date, level: string, content: any) => {
|
|
114
|
-
|
|
113
|
+
thirdPartyLoggingApp.doStuff(...);
|
|
115
114
|
}
|
|
116
115
|
|
|
117
116
|
router.get('api/users', async (req: Request, res: Reponse) => {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
117
|
+
const logger = JetLogger(LoggerModes.CUSTOM, '', true, true, undefined, customSend);
|
|
118
|
+
logger.rmTimestamp = true;
|
|
119
|
+
logger.info(req.params.msg);
|
|
120
|
+
return res.status(OK).json({
|
|
121
|
+
message: 'console_mode',
|
|
122
|
+
});
|
|
124
123
|
});
|
|
125
124
|
````
|
|
@@ -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 {};
|
package/lib/JetLogger.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
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
|
+
else if (mode === LoggerModes.Custom) {
|
|
138
|
+
if (!!customLogger) {
|
|
139
|
+
return customLogger(new Date(), level.prefix, content);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
throw Error(errors.customLoggerErr);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (printFull) {
|
|
146
|
+
content = util_1.default.inspect(content);
|
|
147
|
+
}
|
|
148
|
+
if (format === Formats.Line) {
|
|
149
|
+
content = setupLineFormat(content, timestamp, level);
|
|
150
|
+
}
|
|
151
|
+
else if (format === Formats.Json) {
|
|
152
|
+
content = setupJsonFormat(content, timestamp, level);
|
|
153
|
+
}
|
|
154
|
+
if (mode === LoggerModes.Console) {
|
|
155
|
+
var colorFn = colors_1.default[level.color];
|
|
156
|
+
console.log(colorFn(content));
|
|
157
|
+
}
|
|
158
|
+
else if (mode === LoggerModes.File) {
|
|
159
|
+
writeToFile(content + '\n', filepath)
|
|
160
|
+
.catch(function (err) { return console.log(err); });
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
throw Error(errors.modeErr);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function setupLineFormat(content, timestamp, level) {
|
|
167
|
+
content = (level.prefix + ': ' + content);
|
|
168
|
+
if (timestamp) {
|
|
169
|
+
var time = '[' + new Date().toISOString() + '] ';
|
|
170
|
+
return (time + content);
|
|
171
|
+
}
|
|
172
|
+
return content;
|
|
173
|
+
}
|
|
174
|
+
function setupJsonFormat(content, timestamp, level) {
|
|
175
|
+
var json = {
|
|
176
|
+
level: level.prefix,
|
|
177
|
+
message: content,
|
|
178
|
+
};
|
|
179
|
+
if (timestamp) {
|
|
180
|
+
json.timestamp = new Date().toISOString();
|
|
181
|
+
}
|
|
182
|
+
return JSON.stringify(json);
|
|
183
|
+
}
|
|
184
|
+
function writeToFile(content, filePath) {
|
|
185
|
+
return new Promise(function (res, rej) {
|
|
186
|
+
return fs_1.default.appendFile(filePath, content, (function (err) { return !!err ? rej(err) : res(); }));
|
|
187
|
+
});
|
|
188
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,45 +1,16 @@
|
|
|
1
|
-
export
|
|
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, JetLogger, } 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:
|
|
41
|
-
readonly imp:
|
|
42
|
-
readonly warn:
|
|
43
|
-
readonly 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,192 +1,9 @@
|
|
|
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
3
|
exports.JetLogger = exports.Formats = exports.LoggerModes = void 0;
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
(function (
|
|
12
|
-
|
|
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
|
-
return {
|
|
100
|
-
mode: mode,
|
|
101
|
-
filepath: filepath,
|
|
102
|
-
filepathDatetime: filepathDatetime,
|
|
103
|
-
timestamp: timestamp,
|
|
104
|
-
format: format,
|
|
105
|
-
customLogger: customLogger,
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
function info(content, printFull) {
|
|
109
|
-
return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.info, this.settings);
|
|
110
|
-
}
|
|
111
|
-
function imp(content, printFull) {
|
|
112
|
-
return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.imp, this.settings);
|
|
113
|
-
}
|
|
114
|
-
function warn(content, printFull) {
|
|
115
|
-
return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.warn, this.settings);
|
|
116
|
-
}
|
|
117
|
-
function err(content, printFull) {
|
|
118
|
-
return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.err, this.settings);
|
|
119
|
-
}
|
|
120
|
-
function printLog(content, printFull, level, settings) {
|
|
121
|
-
var mode = settings.mode, format = settings.format, timestamp = settings.timestamp, filepath = settings.filepath, filepathDatetime = settings.filepathDatetime, customLogger = settings.customLogger;
|
|
122
|
-
if (mode === LoggerModes.Off) {
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
var jsonContent = {};
|
|
126
|
-
if (printFull) {
|
|
127
|
-
content = util_1.default.inspect(content);
|
|
128
|
-
}
|
|
129
|
-
if (format === Formats.Json) {
|
|
130
|
-
jsonContent.message = content;
|
|
131
|
-
}
|
|
132
|
-
if (mode !== LoggerModes.Custom) {
|
|
133
|
-
if (format === Formats.Line) {
|
|
134
|
-
content = level.prefix + ': ' + content;
|
|
135
|
-
}
|
|
136
|
-
else if (format === Formats.Json) {
|
|
137
|
-
jsonContent.level = level.prefix;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
if (timestamp) {
|
|
141
|
-
if (format === Formats.Line) {
|
|
142
|
-
var time = '[' + new Date().toISOString() + '] ';
|
|
143
|
-
content = time + content;
|
|
144
|
-
}
|
|
145
|
-
else if (format === Formats.Json) {
|
|
146
|
-
jsonContent.timestamp = new Date().toISOString();
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
if (format === Formats.Json) {
|
|
150
|
-
content = JSON.stringify(jsonContent);
|
|
151
|
-
}
|
|
152
|
-
if (mode === LoggerModes.Console) {
|
|
153
|
-
var colorFn = colors_1.default[level.color];
|
|
154
|
-
console.log(colorFn(content));
|
|
155
|
-
}
|
|
156
|
-
else if (mode === LoggerModes.File) {
|
|
157
|
-
var filePathFinal = filepath;
|
|
158
|
-
if (filepathDatetime) {
|
|
159
|
-
filePathFinal = addDatetimeToFileName(filepath);
|
|
160
|
-
}
|
|
161
|
-
writeToFile(content + '\n', filePathFinal)
|
|
162
|
-
.catch(function (err) { return console.log(err); });
|
|
163
|
-
}
|
|
164
|
-
else if (mode === LoggerModes.Custom) {
|
|
165
|
-
if (!!customLogger) {
|
|
166
|
-
customLogger(new Date(), level.prefix, content);
|
|
167
|
-
}
|
|
168
|
-
else {
|
|
169
|
-
throw Error(errors.customLoggerErr);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
throw Error(errors.modeErr);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
function addDatetimeToFileName(filePath) {
|
|
177
|
-
var dateStr = new Date().toISOString()
|
|
178
|
-
.split('-').join('')
|
|
179
|
-
.split(':').join('')
|
|
180
|
-
.slice(0, 15);
|
|
181
|
-
var filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = (dateStr + '_' + fileName);
|
|
182
|
-
filePathArr[lastIdx] = fileNameNew;
|
|
183
|
-
return filePathArr.join('/');
|
|
184
|
-
}
|
|
185
|
-
function writeToFile(content, filePath) {
|
|
186
|
-
return new Promise(function (res, rej) {
|
|
187
|
-
return fs_1.default.appendFile(filePath, content, function (err) {
|
|
188
|
-
return (!!err ? rej(err) : res());
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
exports.default = JetLogger();
|
|
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
|
+
Object.defineProperty(exports, "JetLogger", { enumerable: true, get: function () { return JetLogger_2.JetLogger; } });
|
|
9
|
+
exports.default = (0, JetLogger_1.JetLogger)();
|