jet-logger 1.2.3 → 1.2.6
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.js +28 -29
- package/lib/index.d.ts +1 -1
- package/lib/index.js +2 -1
- 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 similar 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
|
````
|
package/lib/JetLogger.js
CHANGED
|
@@ -134,32 +134,22 @@ function printLog(content, printFull, level, settings) {
|
|
|
134
134
|
if (mode === LoggerModes.Off) {
|
|
135
135
|
return;
|
|
136
136
|
}
|
|
137
|
-
var jsonContent = {};
|
|
138
137
|
if (printFull) {
|
|
139
138
|
content = util_1.default.inspect(content);
|
|
140
139
|
}
|
|
141
|
-
if (
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
if (mode !== LoggerModes.Custom) {
|
|
145
|
-
if (format === Formats.Line) {
|
|
146
|
-
content = level.prefix + ': ' + content;
|
|
140
|
+
if (mode === LoggerModes.Custom) {
|
|
141
|
+
if (!!customLogger) {
|
|
142
|
+
return customLogger(new Date(), level.prefix, content);
|
|
147
143
|
}
|
|
148
|
-
else
|
|
149
|
-
|
|
144
|
+
else {
|
|
145
|
+
throw Error(errors.customLoggerErr);
|
|
150
146
|
}
|
|
151
147
|
}
|
|
152
|
-
if (
|
|
153
|
-
|
|
154
|
-
var time = '[' + new Date().toISOString() + '] ';
|
|
155
|
-
content = (time + content);
|
|
156
|
-
}
|
|
157
|
-
else if (format === Formats.Json) {
|
|
158
|
-
jsonContent.timestamp = new Date().toISOString();
|
|
159
|
-
}
|
|
148
|
+
if (format === Formats.Line) {
|
|
149
|
+
content = setupLineFormat(content, timestamp, level);
|
|
160
150
|
}
|
|
161
|
-
if (format === Formats.Json) {
|
|
162
|
-
content =
|
|
151
|
+
else if (format === Formats.Json) {
|
|
152
|
+
content = setupJsonFormat(content, timestamp, level);
|
|
163
153
|
}
|
|
164
154
|
if (mode === LoggerModes.Console) {
|
|
165
155
|
var colorFn = colors_1.default[level.color];
|
|
@@ -169,21 +159,30 @@ function printLog(content, printFull, level, settings) {
|
|
|
169
159
|
writeToFile(content + '\n', filepath)
|
|
170
160
|
.catch(function (err) { return console.log(err); });
|
|
171
161
|
}
|
|
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
162
|
else {
|
|
181
163
|
throw Error(errors.modeErr);
|
|
182
164
|
}
|
|
183
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
184
|
function writeToFile(content, filePath) {
|
|
185
185
|
return new Promise(function (res, rej) {
|
|
186
|
-
|
|
187
|
-
return fs_1.default.appendFile(filePath, content, fn);
|
|
186
|
+
return fs_1.default.appendFile(filePath, content, (function (err) { return !!err ? rej(err) : res(); }));
|
|
188
187
|
});
|
|
189
188
|
}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Formats = exports.LoggerModes = void 0;
|
|
3
|
+
exports.JetLogger = exports.Formats = exports.LoggerModes = void 0;
|
|
4
4
|
var JetLogger_1 = require("./JetLogger");
|
|
5
5
|
var JetLogger_2 = require("./JetLogger");
|
|
6
6
|
Object.defineProperty(exports, "LoggerModes", { enumerable: true, get: function () { return JetLogger_2.LoggerModes; } });
|
|
7
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; } });
|
|
8
9
|
exports.default = (0, JetLogger_1.JetLogger)();
|