jet-logger 1.0.5 → 1.1.2
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 +19 -47
- package/lib/index.d.ts +65 -3
- package/lib/index.js +170 -2
- package/package.json +2 -2
- package/lib/Logger.d.ts +0 -53
- package/lib/Logger.js +0 -307
- package/lib/constants.d.ts +0 -13
- package/lib/constants.js +0 -2
package/README.md
CHANGED
|
@@ -15,9 +15,9 @@ $ npm install --save jet-logger
|
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
### Guide
|
|
18
|
-
The logger package's main export is the `
|
|
18
|
+
The logger package's main export is the `logger` object. Logger can used statically or as an instance
|
|
19
19
|
object with settings configured through a constructor. Variables passed through the constructor will
|
|
20
|
-
take priority over environment variables. Note that file
|
|
20
|
+
take priority over environment variables. Note that file writes happen asynchronously.
|
|
21
21
|
|
|
22
22
|
- The four environment variables are:
|
|
23
23
|
- `JET_LOGGER_MODE`: can be `'CONSOLE'`(default), `'FILE'`, `'CUSTOM'`, and `'OFF'`.
|
|
@@ -27,18 +27,17 @@ take priority over environment variables. Note that file-writes happend asynchro
|
|
|
27
27
|
|
|
28
28
|
_logger_ has an export `LoggerModes` which is an enum that provides all the modes if you want to
|
|
29
29
|
use them in code. I would recommend using `Console` for local development, `File` for remote development,
|
|
30
|
-
and `Custom` or `Off` for production. If you want to change the settings in code, you can do so via
|
|
31
|
-
the constructor or getters/setters.
|
|
30
|
+
and `Custom` or `Off` for production. If you want to change the settings in code, you can do so via importing the `JetLogger` function and calling it with whatever options you want.
|
|
32
31
|
<br>
|
|
33
32
|
|
|
34
|
-
- There are 4 functions on Logger to print logs.
|
|
35
|
-
- `info
|
|
36
|
-
- `imp
|
|
37
|
-
- `warn
|
|
38
|
-
- `err
|
|
33
|
+
- There are 4 functions on Logger to print logs.
|
|
34
|
+
- `info`: prints green.
|
|
35
|
+
- `imp`: prints magenta.
|
|
36
|
+
- `warn`: prints yellow.
|
|
37
|
+
- `err`: prints red.
|
|
39
38
|
|
|
40
39
|
There is an optional second param to each method which is a `boolean`. If you pass `true` as the second
|
|
41
|
-
param,
|
|
40
|
+
param, JetLogger will use node's `util` so that the full object gets printed. You should NOT normally
|
|
42
41
|
use this param, but it is especially useful when debugging errors so that you can print out the full
|
|
43
42
|
error object and observe the stack trace.<br>
|
|
44
43
|
|
|
@@ -47,7 +46,7 @@ Let's look at some sample code in an express route:
|
|
|
47
46
|
````typescript
|
|
48
47
|
/* Some script that is run before the route script */
|
|
49
48
|
|
|
50
|
-
// Apply logger settings (Note you could also
|
|
49
|
+
// Apply logger settings (Note you could also using a tool "dotenv" to set env variables)
|
|
51
50
|
// These must be set before logger is imported
|
|
52
51
|
const logFilePath = path.join(__dirname, '../sampleProject.log');
|
|
53
52
|
process.env.JET_LOGGER_MODE = LoggerModes.File; // Can also be Console, Custom, or Off
|
|
@@ -58,25 +57,12 @@ process.env.JET_LOGGER_FILEPATH = logFilePath;
|
|
|
58
57
|
|
|
59
58
|
import { OK } from 'http-status-codes';
|
|
60
59
|
import { Router, Request, Response } from 'express';
|
|
61
|
-
import
|
|
60
|
+
import logger from 'jet-logger';
|
|
62
61
|
|
|
63
62
|
const router = Router();
|
|
64
63
|
|
|
65
64
|
|
|
66
|
-
router.get('api/users', async (req: Request, res: Reponse) => {
|
|
67
|
-
Logger.Info(req.params.msg);
|
|
68
|
-
Logger.Imp(req.params.msg);
|
|
69
|
-
Logger.Warn(req.params.msg);
|
|
70
|
-
Logger.Err(req.params.msg);
|
|
71
|
-
Logger.Err(new Error('printing out an error'));
|
|
72
|
-
Logger.Err(new Error('printing out an error full'), true); // <-- print the full Error object
|
|
73
|
-
return res.status(OK).json({
|
|
74
|
-
message: 'static_console_mode',
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
|
|
78
65
|
router.get('api/users/alt', async (req: Request, res: Reponse) => {
|
|
79
|
-
logger = new Logger();
|
|
80
66
|
logger.info(req.params.msg);
|
|
81
67
|
logger.imp(req.params.msg);
|
|
82
68
|
logger.warn(req.params.msg);
|
|
@@ -111,38 +97,24 @@ router.get('api/users/alt', async (req: Request, res: Reponse) => {
|
|
|
111
97
|
|
|
112
98
|
|
|
113
99
|
### Using a custom logger
|
|
114
|
-
For production you'll probably have some third party logging tool like ElasticSearch or Splunk. _logger_ exports
|
|
115
|
-
|
|
116
|
-
````typescript
|
|
117
|
-
// CustomLoggerTool.ts
|
|
118
|
-
import { ICustomLogger } from 'jet-logger';
|
|
100
|
+
For production you'll probably have some third party logging tool like ElasticSearch or Splunk. _logger_ exports a type `TCustomLogger` that needs to implemented. If you implement this function and pass it to JetLogger and set the mode to `CUSTOM`, Logger will call whatever logic you created for `sendLog()`.
|
|
119
101
|
|
|
120
|
-
export class CustomLoggerTool implements ICustomLogger {
|
|
121
|
-
|
|
122
|
-
private readonly thirdPartyLoggingApplication: ThirdPartyLoggingApplication;
|
|
123
|
-
|
|
124
|
-
constructor() {
|
|
125
|
-
this.thirdPartyLoggingApplication = new ThirdPartyLoggingApplication();
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// Needs to be implemented
|
|
129
|
-
public sendLog(timestamp: Date, prefix: string, content: any): void {
|
|
130
|
-
this.thirdPartyLoggingApplication.doStuff(...);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
````
|
|
134
102
|
|
|
135
103
|
````typescript
|
|
136
104
|
// In the route file
|
|
137
105
|
import { OK } from 'http-status-codes';
|
|
138
106
|
import { Router, Request, Response } from 'express';
|
|
139
|
-
import {
|
|
107
|
+
import { JetLogger, ICustomLogger } from 'jet-logger';
|
|
108
|
+
import { thirdPartyLoggingApp } from 'thirdPartyLoggingApplicationLib';
|
|
140
109
|
|
|
141
|
-
const customLoggerTool = new CustomLoggerTool();
|
|
142
110
|
|
|
111
|
+
// Needs to be implemented
|
|
112
|
+
const customSend: TCustomLogger = (timestamp: Date, level: string, content: any) => {
|
|
113
|
+
thirdPartyLoggingApp.doStuff(...);
|
|
114
|
+
}
|
|
143
115
|
|
|
144
116
|
router.get('api/users', async (req: Request, res: Reponse) => {
|
|
145
|
-
const logger =
|
|
117
|
+
const logger = JetLogger(LoggerModes.CUSTOM, '', true, customSend);
|
|
146
118
|
logger.rmTimestamp = true;
|
|
147
119
|
logger.info(req.params.msg);
|
|
148
120
|
return res.status(OK).json({
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,65 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
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 const levels: {
|
|
12
|
+
info: {
|
|
13
|
+
color: string;
|
|
14
|
+
prefix: string;
|
|
15
|
+
};
|
|
16
|
+
imp: {
|
|
17
|
+
color: string;
|
|
18
|
+
prefix: string;
|
|
19
|
+
};
|
|
20
|
+
warn: {
|
|
21
|
+
color: string;
|
|
22
|
+
prefix: string;
|
|
23
|
+
};
|
|
24
|
+
err: {
|
|
25
|
+
color: string;
|
|
26
|
+
prefix: string;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
declare const _default: {
|
|
30
|
+
readonly settings: {
|
|
31
|
+
readonly mode: LoggerModes;
|
|
32
|
+
readonly filePath: string;
|
|
33
|
+
readonly timestamp: boolean;
|
|
34
|
+
readonly format: Formats;
|
|
35
|
+
readonly customLogger: TCustomLogger | undefined;
|
|
36
|
+
};
|
|
37
|
+
readonly info: typeof info;
|
|
38
|
+
readonly imp: typeof imp;
|
|
39
|
+
readonly warn: typeof warn;
|
|
40
|
+
readonly err: typeof err;
|
|
41
|
+
readonly printLog: typeof printLog;
|
|
42
|
+
};
|
|
43
|
+
export default _default;
|
|
44
|
+
declare type TLevelProp = typeof levels[keyof typeof levels];
|
|
45
|
+
declare type TJetLogger = ReturnType<typeof JetLogger>;
|
|
46
|
+
export declare type TCustomLogger = (timestamp: Date, prefix: string, content: any) => void;
|
|
47
|
+
export declare function JetLogger(mode?: LoggerModes, filePath?: string, timestamp?: boolean, format?: Formats, customLogger?: TCustomLogger): {
|
|
48
|
+
readonly settings: {
|
|
49
|
+
readonly mode: LoggerModes;
|
|
50
|
+
readonly filePath: string;
|
|
51
|
+
readonly timestamp: boolean;
|
|
52
|
+
readonly format: Formats;
|
|
53
|
+
readonly customLogger: TCustomLogger | undefined;
|
|
54
|
+
};
|
|
55
|
+
readonly info: typeof info;
|
|
56
|
+
readonly imp: typeof imp;
|
|
57
|
+
readonly warn: typeof warn;
|
|
58
|
+
readonly err: typeof err;
|
|
59
|
+
readonly printLog: typeof printLog;
|
|
60
|
+
};
|
|
61
|
+
declare function info(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
62
|
+
declare function imp(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
63
|
+
declare function warn(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
64
|
+
declare function err(this: TJetLogger, content: any, printFull?: boolean): void;
|
|
65
|
+
declare function printLog(this: TJetLogger, content: any, printFull: boolean, level: TLevelProp): void;
|
package/lib/index.js
CHANGED
|
@@ -3,5 +3,173 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
7
|
-
|
|
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
|
+
fileName: 'jet-logger.log',
|
|
47
|
+
mode: LoggerModes.Console,
|
|
48
|
+
timestamp: true,
|
|
49
|
+
format: Formats.Line,
|
|
50
|
+
};
|
|
51
|
+
exports.default = JetLogger();
|
|
52
|
+
function JetLogger(mode, filePath, timestamp, format, customLogger) {
|
|
53
|
+
return {
|
|
54
|
+
settings: getSettings(mode, filePath, timestamp, format, customLogger),
|
|
55
|
+
info: info,
|
|
56
|
+
imp: imp,
|
|
57
|
+
warn: warn,
|
|
58
|
+
err: err,
|
|
59
|
+
printLog: printLog,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
exports.JetLogger = JetLogger;
|
|
63
|
+
function getSettings(mode, filePath, timestamp, format, customLogger) {
|
|
64
|
+
if (!mode) {
|
|
65
|
+
if (!!process.env.JET_LOGGER_MODE) {
|
|
66
|
+
mode = process.env.JET_LOGGER_MODE.toUpperCase();
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
mode = defaults.mode;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (!filePath) {
|
|
73
|
+
if (!!process.env.JET_LOGGER_FILEPATH) {
|
|
74
|
+
filePath = process.env.JET_LOGGER_FILEPATH;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
filePath = defaults.fileName;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (!timestamp) {
|
|
81
|
+
if (!!process.env.JET_LOGGER_TIMESTAMP) {
|
|
82
|
+
timestamp = (process.env.JET_LOGGER_TIMESTAMP.toUpperCase() === 'TRUE');
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
timestamp = defaults.timestamp;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (!format) {
|
|
89
|
+
if (!!process.env.JET_LOGGER_FORMAT) {
|
|
90
|
+
format = process.env.JET_LOGGER_FORMAT.toUpperCase();
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
format = defaults.format;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
mode: mode,
|
|
98
|
+
filePath: filePath,
|
|
99
|
+
timestamp: timestamp,
|
|
100
|
+
format: format,
|
|
101
|
+
customLogger: customLogger,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function info(content, printFull) {
|
|
105
|
+
return this.printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.info);
|
|
106
|
+
}
|
|
107
|
+
function imp(content, printFull) {
|
|
108
|
+
return this.printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.imp);
|
|
109
|
+
}
|
|
110
|
+
function warn(content, printFull) {
|
|
111
|
+
return this.printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.warn);
|
|
112
|
+
}
|
|
113
|
+
function err(content, printFull) {
|
|
114
|
+
return this.printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.err);
|
|
115
|
+
}
|
|
116
|
+
function printLog(content, printFull, level) {
|
|
117
|
+
var _a = this.settings, mode = _a.mode, format = _a.format, timestamp = _a.timestamp, filePath = _a.filePath, customLogger = _a.customLogger;
|
|
118
|
+
if (mode === LoggerModes.Off) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
var jsonContent = {};
|
|
122
|
+
if (printFull) {
|
|
123
|
+
content = util_1.default.inspect(content);
|
|
124
|
+
}
|
|
125
|
+
if (format === Formats.Json) {
|
|
126
|
+
jsonContent.message = content;
|
|
127
|
+
}
|
|
128
|
+
if (mode !== LoggerModes.Custom) {
|
|
129
|
+
if (format === Formats.Line) {
|
|
130
|
+
content = level.prefix + ': ' + content;
|
|
131
|
+
}
|
|
132
|
+
else if (format === Formats.Json) {
|
|
133
|
+
jsonContent.level = level.prefix;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (timestamp) {
|
|
137
|
+
if (format === Formats.Line) {
|
|
138
|
+
var time = '[' + new Date().toISOString() + '] ';
|
|
139
|
+
content = time + content;
|
|
140
|
+
}
|
|
141
|
+
else if (format === Formats.Json) {
|
|
142
|
+
jsonContent.timestamp = new Date().toISOString();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (format === Formats.Json) {
|
|
146
|
+
content = JSON.stringify(jsonContent);
|
|
147
|
+
}
|
|
148
|
+
if (mode === LoggerModes.Console) {
|
|
149
|
+
var colorFn = colors_1.default[level.color];
|
|
150
|
+
console.log(colorFn(content));
|
|
151
|
+
}
|
|
152
|
+
else if (mode === LoggerModes.File) {
|
|
153
|
+
writeToFile(content + '\n', filePath).catch(function (err) {
|
|
154
|
+
console.log(err);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
else if (mode === LoggerModes.Custom) {
|
|
158
|
+
if (!!customLogger) {
|
|
159
|
+
customLogger(new Date(), level.prefix, content);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
throw Error(errors.customLoggerErr);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
throw Error(errors.modeErr);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function writeToFile(content, filePath) {
|
|
170
|
+
return new Promise(function (res, rej) {
|
|
171
|
+
return fs_1.default.appendFile(filePath, content, function (err) {
|
|
172
|
+
return (!!err ? rej(err) : res());
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jet-logger",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.2",
|
|
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",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
},
|
|
47
47
|
"homepage": "https://github.com/seanpmaxwell/jet-logger#readme",
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"colors": "
|
|
49
|
+
"colors": "1.3.0"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@types/node": "^14.11.8",
|
package/lib/Logger.d.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { LoggerModes, Formats, ICustomLogger } from './constants';
|
|
2
|
-
declare class Logger {
|
|
3
|
-
static readonly DEFAULT_LOG_FILE_NAME = "jet-logger.log";
|
|
4
|
-
static readonly CUSTOM_LOGGER_ERR: string;
|
|
5
|
-
private static _mode;
|
|
6
|
-
private static _filePath;
|
|
7
|
-
private static _timestamp;
|
|
8
|
-
private static _format;
|
|
9
|
-
private static _customLogger;
|
|
10
|
-
private _mode;
|
|
11
|
-
private _filePath;
|
|
12
|
-
private _timestamp;
|
|
13
|
-
private _format;
|
|
14
|
-
private _customLogger;
|
|
15
|
-
constructor(mode?: LoggerModes, filePath?: string, timestamp?: boolean, format?: Formats, customLogger?: ICustomLogger);
|
|
16
|
-
private static initMode;
|
|
17
|
-
private static initFilePath;
|
|
18
|
-
private static initTimestamp;
|
|
19
|
-
private static initFormat;
|
|
20
|
-
static get mode(): LoggerModes;
|
|
21
|
-
static set mode(mode: LoggerModes);
|
|
22
|
-
get mode(): LoggerModes;
|
|
23
|
-
set mode(mode: LoggerModes);
|
|
24
|
-
static get filePath(): string;
|
|
25
|
-
static set filePath(filePath: string);
|
|
26
|
-
get filePath(): string;
|
|
27
|
-
set filePath(filePath: string);
|
|
28
|
-
static get timestamp(): boolean;
|
|
29
|
-
static set timestamp(timestamp: boolean);
|
|
30
|
-
get timestamp(): boolean;
|
|
31
|
-
set timestamp(timestamp: boolean);
|
|
32
|
-
static get format(): Formats;
|
|
33
|
-
static set format(format: Formats);
|
|
34
|
-
get format(): Formats;
|
|
35
|
-
set format(format: Formats);
|
|
36
|
-
static set customLogger(customLogger: ICustomLogger | null);
|
|
37
|
-
static get customLogger(): ICustomLogger | null;
|
|
38
|
-
set customLogger(customLogger: ICustomLogger | null);
|
|
39
|
-
get customLogger(): ICustomLogger | null;
|
|
40
|
-
static Info(content: any, printFull?: boolean): void;
|
|
41
|
-
static Imp(content: any, printFull?: boolean): void;
|
|
42
|
-
static Warn(content: any, printFull?: boolean): void;
|
|
43
|
-
static Err(content: any, printFull?: boolean): void;
|
|
44
|
-
private static PrintLogHelper;
|
|
45
|
-
info(content: any, printFull?: boolean): void;
|
|
46
|
-
imp(content: any, printFull?: boolean): void;
|
|
47
|
-
warn(content: any, printFull?: boolean): void;
|
|
48
|
-
err(content: any, printFull?: boolean): void;
|
|
49
|
-
private printLogHelper;
|
|
50
|
-
private static PrintLog;
|
|
51
|
-
private static WriteToFile;
|
|
52
|
-
}
|
|
53
|
-
export default Logger;
|
package/lib/Logger.js
DELETED
|
@@ -1,307 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (_) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
-
};
|
|
41
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
var colors_1 = __importDefault(require("colors"));
|
|
43
|
-
var fs_1 = __importDefault(require("fs"));
|
|
44
|
-
var util_1 = __importDefault(require("util"));
|
|
45
|
-
var Levels = {
|
|
46
|
-
info: {
|
|
47
|
-
color: 'green',
|
|
48
|
-
prefix: 'INFO',
|
|
49
|
-
},
|
|
50
|
-
imp: {
|
|
51
|
-
color: 'magenta',
|
|
52
|
-
prefix: 'IMPORTANT',
|
|
53
|
-
},
|
|
54
|
-
warn: {
|
|
55
|
-
color: 'yellow',
|
|
56
|
-
prefix: 'WARNING',
|
|
57
|
-
},
|
|
58
|
-
err: {
|
|
59
|
-
color: 'red',
|
|
60
|
-
prefix: 'ERROR',
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
var Logger = (function () {
|
|
64
|
-
function Logger(mode, filePath, timestamp, format, customLogger) {
|
|
65
|
-
this._mode = mode || Logger.initMode();
|
|
66
|
-
this._filePath = filePath || Logger.initFilePath();
|
|
67
|
-
this._timestamp = (timestamp !== undefined ? timestamp : Logger.initTimestamp());
|
|
68
|
-
this._format = format || Logger.initFormat();
|
|
69
|
-
this._customLogger = customLogger || Logger.customLogger;
|
|
70
|
-
}
|
|
71
|
-
Logger.initMode = function () {
|
|
72
|
-
if (!!process.env.JET_LOGGER_MODE) {
|
|
73
|
-
return process.env.JET_LOGGER_MODE.toLocaleUpperCase();
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
return "CONSOLE";
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
Logger.initFilePath = function () {
|
|
80
|
-
if (!!process.env.JET_LOGGER_FILEPATH) {
|
|
81
|
-
return process.env.JET_LOGGER_FILEPATH;
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
return Logger.DEFAULT_LOG_FILE_NAME;
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
Logger.initTimestamp = function () {
|
|
88
|
-
if (!!process.env.JET_LOGGER_TIMESTAMP) {
|
|
89
|
-
return (process.env.JET_LOGGER_TIMESTAMP.toLocaleUpperCase() === 'TRUE');
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
return true;
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
Logger.initFormat = function () {
|
|
96
|
-
if (!!process.env.JET_LOGGER_FORMAT) {
|
|
97
|
-
return process.env.JET_LOGGER_FORMAT.toLocaleUpperCase();
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
return "LINE";
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
Object.defineProperty(Logger, "mode", {
|
|
104
|
-
get: function () {
|
|
105
|
-
return Logger._mode;
|
|
106
|
-
},
|
|
107
|
-
set: function (mode) {
|
|
108
|
-
Logger._mode = mode;
|
|
109
|
-
},
|
|
110
|
-
enumerable: false,
|
|
111
|
-
configurable: true
|
|
112
|
-
});
|
|
113
|
-
Object.defineProperty(Logger.prototype, "mode", {
|
|
114
|
-
get: function () {
|
|
115
|
-
return this._mode;
|
|
116
|
-
},
|
|
117
|
-
set: function (mode) {
|
|
118
|
-
this._mode = mode;
|
|
119
|
-
},
|
|
120
|
-
enumerable: false,
|
|
121
|
-
configurable: true
|
|
122
|
-
});
|
|
123
|
-
Object.defineProperty(Logger, "filePath", {
|
|
124
|
-
get: function () {
|
|
125
|
-
return Logger._filePath;
|
|
126
|
-
},
|
|
127
|
-
set: function (filePath) {
|
|
128
|
-
Logger._filePath = filePath;
|
|
129
|
-
},
|
|
130
|
-
enumerable: false,
|
|
131
|
-
configurable: true
|
|
132
|
-
});
|
|
133
|
-
Object.defineProperty(Logger.prototype, "filePath", {
|
|
134
|
-
get: function () {
|
|
135
|
-
return this._filePath;
|
|
136
|
-
},
|
|
137
|
-
set: function (filePath) {
|
|
138
|
-
this._filePath = filePath;
|
|
139
|
-
},
|
|
140
|
-
enumerable: false,
|
|
141
|
-
configurable: true
|
|
142
|
-
});
|
|
143
|
-
Object.defineProperty(Logger, "timestamp", {
|
|
144
|
-
get: function () {
|
|
145
|
-
return Logger._timestamp;
|
|
146
|
-
},
|
|
147
|
-
set: function (timestamp) {
|
|
148
|
-
Logger._timestamp = timestamp;
|
|
149
|
-
},
|
|
150
|
-
enumerable: false,
|
|
151
|
-
configurable: true
|
|
152
|
-
});
|
|
153
|
-
Object.defineProperty(Logger.prototype, "timestamp", {
|
|
154
|
-
get: function () {
|
|
155
|
-
return this._timestamp;
|
|
156
|
-
},
|
|
157
|
-
set: function (timestamp) {
|
|
158
|
-
this._timestamp = timestamp;
|
|
159
|
-
},
|
|
160
|
-
enumerable: false,
|
|
161
|
-
configurable: true
|
|
162
|
-
});
|
|
163
|
-
Object.defineProperty(Logger, "format", {
|
|
164
|
-
get: function () {
|
|
165
|
-
return Logger._format;
|
|
166
|
-
},
|
|
167
|
-
set: function (format) {
|
|
168
|
-
Logger._format = format;
|
|
169
|
-
},
|
|
170
|
-
enumerable: false,
|
|
171
|
-
configurable: true
|
|
172
|
-
});
|
|
173
|
-
Object.defineProperty(Logger.prototype, "format", {
|
|
174
|
-
get: function () {
|
|
175
|
-
return this._format;
|
|
176
|
-
},
|
|
177
|
-
set: function (format) {
|
|
178
|
-
this._format = format;
|
|
179
|
-
},
|
|
180
|
-
enumerable: false,
|
|
181
|
-
configurable: true
|
|
182
|
-
});
|
|
183
|
-
Object.defineProperty(Logger, "customLogger", {
|
|
184
|
-
get: function () {
|
|
185
|
-
return Logger._customLogger;
|
|
186
|
-
},
|
|
187
|
-
set: function (customLogger) {
|
|
188
|
-
Logger._customLogger = customLogger;
|
|
189
|
-
},
|
|
190
|
-
enumerable: false,
|
|
191
|
-
configurable: true
|
|
192
|
-
});
|
|
193
|
-
Object.defineProperty(Logger.prototype, "customLogger", {
|
|
194
|
-
get: function () {
|
|
195
|
-
return this._customLogger;
|
|
196
|
-
},
|
|
197
|
-
set: function (customLogger) {
|
|
198
|
-
this._customLogger = customLogger;
|
|
199
|
-
},
|
|
200
|
-
enumerable: false,
|
|
201
|
-
configurable: true
|
|
202
|
-
});
|
|
203
|
-
Logger.Info = function (content, printFull) {
|
|
204
|
-
Logger.PrintLogHelper(content, printFull || false, Levels.info);
|
|
205
|
-
};
|
|
206
|
-
Logger.Imp = function (content, printFull) {
|
|
207
|
-
Logger.PrintLogHelper(content, printFull || false, Levels.imp);
|
|
208
|
-
};
|
|
209
|
-
Logger.Warn = function (content, printFull) {
|
|
210
|
-
Logger.PrintLogHelper(content, printFull || false, Levels.warn);
|
|
211
|
-
};
|
|
212
|
-
Logger.Err = function (content, printFull) {
|
|
213
|
-
Logger.PrintLogHelper(content, printFull || false, Levels.err);
|
|
214
|
-
};
|
|
215
|
-
Logger.PrintLogHelper = function (content, printFull, level) {
|
|
216
|
-
Logger.PrintLog(content, printFull, level, Logger.mode, Logger.timestamp, Logger.format, Logger.filePath, Logger.customLogger);
|
|
217
|
-
};
|
|
218
|
-
Logger.prototype.info = function (content, printFull) {
|
|
219
|
-
this.printLogHelper(content, printFull || false, Levels.info);
|
|
220
|
-
};
|
|
221
|
-
Logger.prototype.imp = function (content, printFull) {
|
|
222
|
-
this.printLogHelper(content, printFull || false, Levels.imp);
|
|
223
|
-
};
|
|
224
|
-
Logger.prototype.warn = function (content, printFull) {
|
|
225
|
-
this.printLogHelper(content, printFull || false, Levels.warn);
|
|
226
|
-
};
|
|
227
|
-
Logger.prototype.err = function (content, printFull) {
|
|
228
|
-
this.printLogHelper(content, printFull || false, Levels.err);
|
|
229
|
-
};
|
|
230
|
-
Logger.prototype.printLogHelper = function (content, printFull, level) {
|
|
231
|
-
Logger.PrintLog(content, printFull, level, this.mode, this.timestamp, this.format, this.filePath, this.customLogger);
|
|
232
|
-
};
|
|
233
|
-
Logger.PrintLog = function (content, printFull, level, mode, timestamp, format, filePath, customLogger) {
|
|
234
|
-
if (mode === "OFF") {
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
var jsonContent = {};
|
|
238
|
-
if (printFull) {
|
|
239
|
-
content = util_1.default.inspect(content);
|
|
240
|
-
}
|
|
241
|
-
if (format === "JSON") {
|
|
242
|
-
jsonContent.message = content;
|
|
243
|
-
}
|
|
244
|
-
if (mode !== "CUSTOM") {
|
|
245
|
-
if (format === "LINE") {
|
|
246
|
-
content = level.prefix + ': ' + content;
|
|
247
|
-
}
|
|
248
|
-
else if (format === "JSON") {
|
|
249
|
-
jsonContent.level = level.prefix;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
if (timestamp) {
|
|
253
|
-
if (format === "LINE") {
|
|
254
|
-
var time = '[' + new Date().toISOString() + '] ';
|
|
255
|
-
content = time + content;
|
|
256
|
-
}
|
|
257
|
-
else if (format === "JSON") {
|
|
258
|
-
jsonContent.timestamp = new Date().toISOString();
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
if (format === "JSON") {
|
|
262
|
-
content = JSON.stringify(jsonContent);
|
|
263
|
-
}
|
|
264
|
-
if (mode === "CONSOLE") {
|
|
265
|
-
var colorFn = colors_1.default[level.color];
|
|
266
|
-
console.log(colorFn(content));
|
|
267
|
-
}
|
|
268
|
-
else if (mode === "FILE") {
|
|
269
|
-
Logger.WriteToFile(content + '\n', filePath).catch(function (err) {
|
|
270
|
-
console.log(err);
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
else if (mode === "CUSTOM") {
|
|
274
|
-
if (!!customLogger) {
|
|
275
|
-
customLogger.sendLog(new Date(), level.prefix, content);
|
|
276
|
-
}
|
|
277
|
-
else {
|
|
278
|
-
throw Error(Logger.CUSTOM_LOGGER_ERR);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
else {
|
|
282
|
-
throw Error('The correct logger mode was not specified: Must be "CUSTOM", "FILE", ' +
|
|
283
|
-
'"OFF", or "CONSOLE".');
|
|
284
|
-
}
|
|
285
|
-
};
|
|
286
|
-
Logger.WriteToFile = function (content, filePath) {
|
|
287
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
288
|
-
return __generator(this, function (_a) {
|
|
289
|
-
return [2, new Promise(function (res, rej) {
|
|
290
|
-
return fs_1.default.appendFile(filePath, content, function (err) {
|
|
291
|
-
return (!!err ? rej(err) : res());
|
|
292
|
-
});
|
|
293
|
-
})];
|
|
294
|
-
});
|
|
295
|
-
});
|
|
296
|
-
};
|
|
297
|
-
Logger.DEFAULT_LOG_FILE_NAME = 'jet-logger.log';
|
|
298
|
-
Logger.CUSTOM_LOGGER_ERR = 'Custom logger mode set to true, but no ' +
|
|
299
|
-
'custom logger was provided.';
|
|
300
|
-
Logger._mode = Logger.initMode();
|
|
301
|
-
Logger._filePath = Logger.initFilePath();
|
|
302
|
-
Logger._timestamp = Logger.initTimestamp();
|
|
303
|
-
Logger._format = Logger.initFormat();
|
|
304
|
-
Logger._customLogger = null;
|
|
305
|
-
return Logger;
|
|
306
|
-
}());
|
|
307
|
-
exports.default = Logger;
|
package/lib/constants.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export interface ICustomLogger {
|
|
2
|
-
sendLog(timestamp: Date, prefix: string, content: any): void;
|
|
3
|
-
}
|
|
4
|
-
export declare const enum LoggerModes {
|
|
5
|
-
Console = "CONSOLE",
|
|
6
|
-
File = "FILE",
|
|
7
|
-
Custom = "CUSTOM",
|
|
8
|
-
Off = "OFF"
|
|
9
|
-
}
|
|
10
|
-
export declare const enum Formats {
|
|
11
|
-
Line = "LINE",
|
|
12
|
-
Json = "JSON"
|
|
13
|
-
}
|
package/lib/constants.js
DELETED