jet-logger 1.0.3 → 1.1.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/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 `Logger` class. Logger can used statically or as an instance
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.
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.
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. Each has a static counterpart:
35
- - `info` or `Info`: prints green.
36
- - `imp` or `Imp`: prints magenta.
37
- - `warn` or `Warn`: prints yellow.
38
- - `err` or `Err`: prints red.
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, Logger will use node's `util` so that the full object gets printed. You should NOT normally
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 use a tool "dotenv" to set env variables)
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 Logger from 'jet-logger';
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 one interface `ICustomLogger` which has one method `sendLog()` that needs to implemented. If you created a class which implements this interface, and add it to Logger through a setter or the constructor and set the mode to `CUSTOM`, Logger will call whatever logic you created for `sendLog()`.
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 { CustomLoggerTool } from 'CustomLoggerTool';
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 = new Logger(LoggerModes.CUSTOM, '', true, customLoggerTool);
117
+ const logger = new Logger(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 { LoggerModes, Formats, ICustomLogger } from './constants';
2
- import Logger from './Logger';
3
- export default Logger;
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: Readonly<{
30
+ settings: {
31
+ mode: LoggerModes;
32
+ filePath: string;
33
+ timestamp: boolean;
34
+ format: Formats;
35
+ customLogger: TCustomLogger | undefined;
36
+ };
37
+ info: typeof info;
38
+ imp: typeof imp;
39
+ warn: typeof warn;
40
+ err: typeof err;
41
+ printLogHelper: typeof printLogHelper;
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): Readonly<{
48
+ settings: {
49
+ mode: LoggerModes;
50
+ filePath: string;
51
+ timestamp: boolean;
52
+ format: Formats;
53
+ customLogger: TCustomLogger | undefined;
54
+ };
55
+ info: typeof info;
56
+ imp: typeof imp;
57
+ warn: typeof warn;
58
+ err: typeof err;
59
+ printLogHelper: typeof printLogHelper;
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 printLogHelper(this: TJetLogger, content: any, printFull: boolean, level: TLevelProp): void;
package/lib/index.js CHANGED
@@ -1,7 +1,177 @@
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
- var Logger_1 = __importDefault(require("./Logger"));
7
- exports.default = Logger_1.default;
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
+ 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 Object.freeze({
54
+ settings: getSettings(mode, filePath, timestamp, format, customLogger),
55
+ info: info,
56
+ imp: imp,
57
+ warn: warn,
58
+ err: err,
59
+ printLogHelper: printLogHelper,
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.printLogHelper(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.info);
106
+ }
107
+ function imp(content, printFull) {
108
+ return this.printLogHelper(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.imp);
109
+ }
110
+ function warn(content, printFull) {
111
+ return this.printLogHelper(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.warn);
112
+ }
113
+ function err(content, printFull) {
114
+ return this.printLogHelper(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.err);
115
+ }
116
+ function printLogHelper(content, printFull, level) {
117
+ return printLog(content, printFull, level, this.settings.mode, this.settings.timestamp, this.settings.format, this.settings.filePath, this.settings.customLogger);
118
+ }
119
+ function printLog(content, printFull, level, mode, timestamp, format, filePath, customLogger) {
120
+ if (mode === LoggerModes.Off) {
121
+ return;
122
+ }
123
+ var jsonContent = {};
124
+ if (printFull) {
125
+ content = util_1.default.inspect(content);
126
+ }
127
+ if (format === Formats.Json) {
128
+ jsonContent.message = content;
129
+ }
130
+ if (mode !== LoggerModes.Custom) {
131
+ if (format === Formats.Line) {
132
+ content = level.prefix + ': ' + content;
133
+ }
134
+ else if (format === Formats.Json) {
135
+ jsonContent.level = level.prefix;
136
+ }
137
+ }
138
+ if (timestamp) {
139
+ if (format === Formats.Line) {
140
+ var time = '[' + new Date().toISOString() + '] ';
141
+ content = time + content;
142
+ }
143
+ else if (format === Formats.Json) {
144
+ jsonContent.timestamp = new Date().toISOString();
145
+ }
146
+ }
147
+ if (format === Formats.Json) {
148
+ content = JSON.stringify(jsonContent);
149
+ }
150
+ if (mode === LoggerModes.Console) {
151
+ var colorFn = colors_1.default[level.color];
152
+ console.log(colorFn(content));
153
+ }
154
+ else if (mode === LoggerModes.File) {
155
+ writeToFile(content + '\n', filePath).catch(function (err) {
156
+ console.log(err);
157
+ });
158
+ }
159
+ else if (mode === LoggerModes.Custom) {
160
+ if (!!customLogger) {
161
+ customLogger(new Date(), level.prefix, content);
162
+ }
163
+ else {
164
+ throw Error(errors.customLoggerErr);
165
+ }
166
+ }
167
+ else {
168
+ throw Error(errors.modeErr);
169
+ }
170
+ }
171
+ function writeToFile(content, filePath) {
172
+ return new Promise(function (res, rej) {
173
+ return fs_1.default.appendFile(filePath, content, function (err) {
174
+ return (!!err ? rej(err) : res());
175
+ });
176
+ });
177
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jet-logger",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
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": "^1.4.0"
49
+ "colors": "1.3.0"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@types/node": "^14.11.8",
package/lib/Logger.d.ts DELETED
@@ -1,54 +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
- private static CheckExists;
53
- }
54
- export default Logger;
package/lib/Logger.js DELETED
@@ -1,281 +0,0 @@
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
- var colors_1 = __importDefault(require("colors"));
7
- var fs_1 = __importDefault(require("fs"));
8
- var util_1 = __importDefault(require("util"));
9
- var Levels = {
10
- info: {
11
- color: 'green',
12
- prefix: 'INFO',
13
- },
14
- imp: {
15
- color: 'magenta',
16
- prefix: 'IMPORTANT',
17
- },
18
- warn: {
19
- color: 'yellow',
20
- prefix: 'WARNING',
21
- },
22
- err: {
23
- color: 'red',
24
- prefix: 'ERROR',
25
- }
26
- };
27
- var Logger = (function () {
28
- function Logger(mode, filePath, timestamp, format, customLogger) {
29
- this._mode = mode || Logger.initMode();
30
- this._filePath = filePath || Logger.initFilePath();
31
- this._timestamp = (timestamp !== undefined ? timestamp : Logger.initTimestamp());
32
- this._format = format || Logger.initFormat();
33
- this._customLogger = customLogger || Logger.customLogger;
34
- }
35
- Logger.initMode = function () {
36
- if (!!process.env.JET_LOGGER_MODE) {
37
- return process.env.JET_LOGGER_MODE.toLocaleUpperCase();
38
- }
39
- else {
40
- return "CONSOLE";
41
- }
42
- };
43
- Logger.initFilePath = function () {
44
- if (!!process.env.JET_LOGGER_FILEPATH) {
45
- return process.env.JET_LOGGER_FILEPATH;
46
- }
47
- else {
48
- return Logger.DEFAULT_LOG_FILE_NAME;
49
- }
50
- };
51
- Logger.initTimestamp = function () {
52
- if (!!process.env.JET_LOGGER_TIMESTAMP) {
53
- return (process.env.JET_LOGGER_TIMESTAMP.toLocaleUpperCase() === 'TRUE');
54
- }
55
- else {
56
- return true;
57
- }
58
- };
59
- Logger.initFormat = function () {
60
- if (!!process.env.JET_LOGGER_FORMAT) {
61
- return process.env.JET_LOGGER_FORMAT.toLocaleUpperCase();
62
- }
63
- else {
64
- return "LINE";
65
- }
66
- };
67
- Object.defineProperty(Logger, "mode", {
68
- get: function () {
69
- return Logger._mode;
70
- },
71
- set: function (mode) {
72
- Logger._mode = mode;
73
- },
74
- enumerable: false,
75
- configurable: true
76
- });
77
- Object.defineProperty(Logger.prototype, "mode", {
78
- get: function () {
79
- return this._mode;
80
- },
81
- set: function (mode) {
82
- this._mode = mode;
83
- },
84
- enumerable: false,
85
- configurable: true
86
- });
87
- Object.defineProperty(Logger, "filePath", {
88
- get: function () {
89
- return Logger._filePath;
90
- },
91
- set: function (filePath) {
92
- Logger._filePath = filePath;
93
- },
94
- enumerable: false,
95
- configurable: true
96
- });
97
- Object.defineProperty(Logger.prototype, "filePath", {
98
- get: function () {
99
- return this._filePath;
100
- },
101
- set: function (filePath) {
102
- this._filePath = filePath;
103
- },
104
- enumerable: false,
105
- configurable: true
106
- });
107
- Object.defineProperty(Logger, "timestamp", {
108
- get: function () {
109
- return Logger._timestamp;
110
- },
111
- set: function (timestamp) {
112
- Logger._timestamp = timestamp;
113
- },
114
- enumerable: false,
115
- configurable: true
116
- });
117
- Object.defineProperty(Logger.prototype, "timestamp", {
118
- get: function () {
119
- return this._timestamp;
120
- },
121
- set: function (timestamp) {
122
- this._timestamp = timestamp;
123
- },
124
- enumerable: false,
125
- configurable: true
126
- });
127
- Object.defineProperty(Logger, "format", {
128
- get: function () {
129
- return Logger._format;
130
- },
131
- set: function (format) {
132
- Logger._format = format;
133
- },
134
- enumerable: false,
135
- configurable: true
136
- });
137
- Object.defineProperty(Logger.prototype, "format", {
138
- get: function () {
139
- return this._format;
140
- },
141
- set: function (format) {
142
- this._format = format;
143
- },
144
- enumerable: false,
145
- configurable: true
146
- });
147
- Object.defineProperty(Logger, "customLogger", {
148
- get: function () {
149
- return Logger._customLogger;
150
- },
151
- set: function (customLogger) {
152
- Logger._customLogger = customLogger;
153
- },
154
- enumerable: false,
155
- configurable: true
156
- });
157
- Object.defineProperty(Logger.prototype, "customLogger", {
158
- get: function () {
159
- return this._customLogger;
160
- },
161
- set: function (customLogger) {
162
- this._customLogger = customLogger;
163
- },
164
- enumerable: false,
165
- configurable: true
166
- });
167
- Logger.Info = function (content, printFull) {
168
- Logger.PrintLogHelper(content, printFull || false, Levels.info);
169
- };
170
- Logger.Imp = function (content, printFull) {
171
- Logger.PrintLogHelper(content, printFull || false, Levels.imp);
172
- };
173
- Logger.Warn = function (content, printFull) {
174
- Logger.PrintLogHelper(content, printFull || false, Levels.warn);
175
- };
176
- Logger.Err = function (content, printFull) {
177
- Logger.PrintLogHelper(content, printFull || false, Levels.err);
178
- };
179
- Logger.PrintLogHelper = function (content, printFull, level) {
180
- Logger.PrintLog(content, printFull, level, Logger.mode, Logger.timestamp, Logger.format, Logger.filePath, Logger.customLogger);
181
- };
182
- Logger.prototype.info = function (content, printFull) {
183
- this.printLogHelper(content, printFull || false, Levels.info);
184
- };
185
- Logger.prototype.imp = function (content, printFull) {
186
- this.printLogHelper(content, printFull || false, Levels.imp);
187
- };
188
- Logger.prototype.warn = function (content, printFull) {
189
- this.printLogHelper(content, printFull || false, Levels.warn);
190
- };
191
- Logger.prototype.err = function (content, printFull) {
192
- this.printLogHelper(content, printFull || false, Levels.err);
193
- };
194
- Logger.prototype.printLogHelper = function (content, printFull, level) {
195
- Logger.PrintLog(content, printFull, level, this.mode, this.timestamp, this.format, this.filePath, this.customLogger);
196
- };
197
- Logger.PrintLog = function (content, printFull, level, mode, timestamp, format, filePath, customLogger) {
198
- if (mode === "OFF") {
199
- return;
200
- }
201
- var jsonContent = {};
202
- if (printFull) {
203
- content = util_1.default.inspect(content);
204
- }
205
- if (format === "JSON") {
206
- jsonContent.message = content;
207
- }
208
- if (mode !== "CUSTOM") {
209
- if (format === "LINE") {
210
- content = level.prefix + ': ' + content;
211
- }
212
- else if (format === "JSON") {
213
- jsonContent.level = level.prefix;
214
- }
215
- }
216
- if (timestamp) {
217
- if (format === "LINE") {
218
- var time = '[' + new Date().toISOString() + '] ';
219
- content = time + content;
220
- }
221
- else if (format === "JSON") {
222
- jsonContent.timestamp = new Date().toISOString();
223
- }
224
- }
225
- if (format === "JSON") {
226
- content = JSON.stringify(jsonContent);
227
- }
228
- if (mode === "CONSOLE") {
229
- var colorFn = colors_1.default[level.color];
230
- console.log(colorFn(content));
231
- }
232
- else if (mode === "FILE") {
233
- Logger.WriteToFile(content + '\n', filePath);
234
- }
235
- else if (mode === "CUSTOM") {
236
- if (!!customLogger) {
237
- customLogger.sendLog(new Date(), level.prefix, content);
238
- }
239
- else {
240
- throw Error(Logger.CUSTOM_LOGGER_ERR);
241
- }
242
- }
243
- else {
244
- throw Error('The correct logger mode was not specified: Must be "CUSTOM", "FILE", ' +
245
- '"OFF", or "CONSOLE".');
246
- }
247
- };
248
- Logger.WriteToFile = function (content, filePath) {
249
- try {
250
- var fileExists = Logger.CheckExists(filePath);
251
- if (fileExists) {
252
- fs_1.default.appendFileSync(filePath, content);
253
- }
254
- else {
255
- fs_1.default.writeFileSync(filePath, content);
256
- }
257
- }
258
- catch (err) {
259
- console.error(err);
260
- }
261
- };
262
- Logger.CheckExists = function (filePath) {
263
- try {
264
- fs_1.default.accessSync(filePath);
265
- return true;
266
- }
267
- catch (e) {
268
- return false;
269
- }
270
- };
271
- Logger.DEFAULT_LOG_FILE_NAME = 'jet-logger.log';
272
- Logger.CUSTOM_LOGGER_ERR = 'Custom logger mode set to true, but no ' +
273
- 'custom logger was provided.';
274
- Logger._mode = Logger.initMode();
275
- Logger._filePath = Logger.initFilePath();
276
- Logger._timestamp = Logger.initTimestamp();
277
- Logger._format = Logger.initFormat();
278
- Logger._customLogger = null;
279
- return Logger;
280
- }());
281
- exports.default = Logger;
@@ -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
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });