jet-logger 1.2.6 → 1.3.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
@@ -4,7 +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 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.
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, written in 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.
8
8
  <br/>
9
9
 
10
10
  ### Installation
@@ -13,20 +13,16 @@ $ npm install --save jet-logger
13
13
  ```
14
14
 
15
15
  ### Guide
16
- The logger package's main export is the `logger` object. Logger can used statically or as an instance
17
- object with settings configured through a constructor. Variables passed through the constructor will
18
- take priority over environment variables. Note that file writes happen asynchronously.
16
+ The logger package's main export is the default `logger` object. This default export will use of course, all the default settings. If you wish to pass your own settings you can import the `jetLogger()` function and pass parameters to configure your own custom logging objects.
19
17
 
20
- - The four environment variables are:
18
+ - The five environment variables are:
21
19
  - `JET_LOGGER_MODE`: can be `'CONSOLE'`(default), `'FILE'`, `'CUSTOM'`, and `'OFF'`.
22
20
  - `JET_LOGGER_FILEPATH`: the file-path for file mode. Default is `_home_dir/jet-logger.log_`.
23
21
  - `JET_LOGGER_FILEPATH_DATETIME`: prepend the log file name with the datetime. Can be `'TRUE'` (default) or `'FALSE'`.
24
22
  - `JET_LOGGER_TIMESTAMP`: adds a timestamp next to each log. Can be `'TRUE'` (default) or `'FALSE'`.
25
23
  - `JET_LOGGER_FORMAT`: formats log as a line or JSON object. Can be `'LINE'` (default) or `'JSON'`.
26
24
 
27
- _logger_ has an export `LoggerModes` which is an enum that provides all the modes if you want to
28
- use them in code. I would recommend using `Console` for local development, `File` for remote development,
29
- 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.
25
+ _logger_ has an export `LoggerModes` which is an enum that provides all the modes if you want to use them in code. I would recommend using `Console` for local development, `File` for remote development, 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.
30
26
  <br>
31
27
 
32
28
  - There are 4 functions on Logger to print logs.
@@ -35,12 +31,9 @@ and `Custom` or `Off` for production. If you want to change the settings in code
35
31
  - `warn`: prints yellow.
36
32
  - `err`: prints red.
37
33
 
38
- There is an optional second param to each method which is a `boolean`. If you pass `true` as the second
39
- param, JetLogger will use node's `util` so that the full object gets printed. You should NOT normally
40
- use this param, but it is especially useful when debugging errors so that you can print out the full
41
- error object and observe the stack trace.<br>
34
+ There is an optional second param to each method which is a `boolean`. If you pass `true` as the second param, JetLogger will use node's `util` so that the full object gets printed. You should NOT normally use this param, but it is especially useful when debugging errors so that you can print out the full error object and observe the stack trace.<br>
42
35
 
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).
36
+ Let's look at some sample code in an express route.
44
37
 
45
38
 
46
39
  ````typescript
@@ -59,8 +52,8 @@ import { OK } from 'http-status-codes';
59
52
  import { Router, Request, Response } from 'express';
60
53
  import logger from 'jet-logger';
61
54
 
62
- const router = Router();
63
55
 
56
+ const router = Router();
64
57
 
65
58
  router.get('api/users/alt', async (req: Request, res: Reponse) => {
66
59
  logger.info(req.params.msg);
@@ -97,24 +90,24 @@ router.get('api/users/alt', async (req: Request, res: Reponse) => {
97
90
 
98
91
 
99
92
  ### Using a custom 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()`.
93
+ For production you'll probably have some third party logging tool like ElasticSearch or Splunk. _logger_ exports a type `TCustomLogFn` 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 it.
101
94
 
102
95
 
103
96
  ````typescript
104
97
  // In the route file
105
98
  import { OK } from 'http-status-codes';
106
99
  import { Router, Request, Response } from 'express';
107
- import { JetLogger, ICustomLogger } from 'jet-logger';
100
+ import { jetLogger, TCustomLogFn, ILogger } from 'jet-logger';
108
101
  import { thirdPartyLoggingApp } from 'thirdPartyLoggingApplicationLib';
109
102
 
110
103
 
111
104
  // Needs to be implemented
112
- const customSend: TCustomLogger = (timestamp: Date, level: string, content: any) => {
105
+ const customSend: TCustomLogFn = (timestamp: Date, level: string, content: any) => {
113
106
  thirdPartyLoggingApp.doStuff(...);
114
107
  }
115
108
 
116
109
  router.get('api/users', async (req: Request, res: Reponse) => {
117
- const logger = JetLogger(LoggerModes.CUSTOM, '', true, true, undefined, customSend);
110
+ const logger: ILogger = jetLogger(LoggerModes.CUSTOM, '', true, true, undefined, customSend);
118
111
  logger.rmTimestamp = true;
119
112
  logger.info(req.params.msg);
120
113
  return res.status(OK).json({
package/lib/index.d.ts CHANGED
@@ -1,16 +1 @@
1
- export { LoggerModes, Formats, TCustomLogger, JetLogger, } from './JetLogger';
2
- declare const _default: {
3
- readonly settings: {
4
- readonly mode: import("./JetLogger").LoggerModes;
5
- readonly filepath: string;
6
- readonly filepathDatetime: boolean;
7
- readonly timestamp: boolean;
8
- readonly format: import("./JetLogger").Formats;
9
- readonly customLogger: import("./JetLogger").TCustomLogger | undefined;
10
- };
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;
15
- };
16
- export default _default;
1
+ export { LoggerModes, Formats, TCustomLogFn, ILogger, jetLogger, default as default, } from './jetLogger';
package/lib/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.JetLogger = exports.Formats = exports.LoggerModes = void 0;
4
- var JetLogger_1 = require("./JetLogger");
5
- var JetLogger_2 = require("./JetLogger");
6
- Object.defineProperty(exports, "LoggerModes", { enumerable: true, get: function () { return JetLogger_2.LoggerModes; } });
7
- Object.defineProperty(exports, "Formats", { enumerable: true, get: function () { return JetLogger_2.Formats; } });
8
- Object.defineProperty(exports, "JetLogger", { enumerable: true, get: function () { return JetLogger_2.JetLogger; } });
9
- exports.default = (0, JetLogger_1.JetLogger)();
6
+ exports.default = exports.jetLogger = exports.Formats = exports.LoggerModes = void 0;
7
+ var jetLogger_1 = require("./jetLogger");
8
+ Object.defineProperty(exports, "LoggerModes", { enumerable: true, get: function () { return jetLogger_1.LoggerModes; } });
9
+ Object.defineProperty(exports, "Formats", { enumerable: true, get: function () { return jetLogger_1.Formats; } });
10
+ Object.defineProperty(exports, "jetLogger", { enumerable: true, get: function () { return jetLogger_1.jetLogger; } });
11
+ Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(jetLogger_1).default; } });
@@ -0,0 +1,30 @@
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 TLogFn = (content: any, printFull?: boolean) => void;
12
+ export declare type TCustomLogFn = (timestamp: Date, prefix: string, content: unknown) => void;
13
+ export interface ILogger {
14
+ settings: ISettings;
15
+ info: TLogFn;
16
+ imp: TLogFn;
17
+ warn: TLogFn;
18
+ err: TLogFn;
19
+ }
20
+ interface ISettings {
21
+ mode: LoggerModes;
22
+ filepath: string;
23
+ filepathDatetime: boolean;
24
+ timestamp: boolean;
25
+ format: Formats;
26
+ customLogFn?: TCustomLogFn;
27
+ }
28
+ export declare function jetLogger(mode?: LoggerModes, filepath?: string, filepathDatetime?: boolean, timestamp?: boolean, format?: Formats, customLogFn?: TCustomLogFn): ILogger;
29
+ declare const _default: ILogger;
30
+ export default _default;
@@ -3,7 +3,7 @@ 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
- exports.JetLogger = exports.Formats = exports.LoggerModes = void 0;
6
+ exports.jetLogger = exports.Formats = exports.LoggerModes = void 0;
7
7
  var util_1 = __importDefault(require("util"));
8
8
  var colors_1 = __importDefault(require("colors"));
9
9
  var fs_1 = __importDefault(require("fs"));
@@ -19,48 +19,49 @@ var Formats;
19
19
  Formats["Line"] = "LINE";
20
20
  Formats["Json"] = "JSON";
21
21
  })(Formats = exports.Formats || (exports.Formats = {}));
22
- var levels = {
23
- info: {
24
- color: 'green',
25
- prefix: 'INFO',
22
+ var Levels = {
23
+ Info: {
24
+ Color: 'green',
25
+ Prefix: 'INFO',
26
26
  },
27
- imp: {
28
- color: 'magenta',
29
- prefix: 'IMPORTANT',
27
+ Imp: {
28
+ Color: 'magenta',
29
+ Prefix: 'IMPORTANT',
30
30
  },
31
- warn: {
32
- color: 'yellow',
33
- prefix: 'WARNING',
31
+ Warn: {
32
+ Color: 'yellow',
33
+ Prefix: 'WARNING',
34
34
  },
35
- err: {
36
- color: 'red',
37
- prefix: 'ERROR',
35
+ Err: {
36
+ Color: 'red',
37
+ Prefix: 'ERROR',
38
38
  }
39
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".'
40
+ var Errors = {
41
+ customLogFn: 'Custom logger mode set to true, but no custom logger was ' +
42
+ 'provided.',
43
+ Mode: 'The correct logger mode was not specified: Must be "CUSTOM", ' +
44
+ '"FILE", "OFF", or "CONSOLE".'
44
45
  };
45
- var defaults = {
46
- filepath: 'jet-logger.log',
47
- mode: LoggerModes.Console,
48
- timestamp: true,
49
- filepathDatetime: true,
50
- format: Formats.Line,
46
+ var Defaults = {
47
+ Filepath: 'jet-logger.log',
48
+ Mode: LoggerModes.Console,
49
+ Timestamp: true,
50
+ FilepathDatetime: true,
51
+ Format: Formats.Line,
51
52
  };
52
- function JetLogger(mode, filepath, filepathDatetime, timestamp, format, customLogger) {
53
- var settings = getSettings(mode, filepath, timestamp, filepathDatetime, format, customLogger);
53
+ function jetLogger(mode, filepath, filepathDatetime, timestamp, format, customLogFn) {
54
+ var settings = getSettings(mode, filepath, timestamp, filepathDatetime, format, customLogFn);
54
55
  return { settings: settings, info: info, imp: imp, warn: warn, err: err };
55
56
  }
56
- exports.JetLogger = JetLogger;
57
- function getSettings(mode, filepath, filepathDatetime, timestamp, format, customLogger) {
57
+ exports.jetLogger = jetLogger;
58
+ function getSettings(mode, filepath, filepathDatetime, timestamp, format, customLogFn) {
58
59
  if (!mode) {
59
60
  if (!!process.env.JET_LOGGER_MODE) {
60
61
  mode = process.env.JET_LOGGER_MODE.toUpperCase();
61
62
  }
62
63
  else {
63
- mode = defaults.mode;
64
+ mode = Defaults.Mode;
64
65
  }
65
66
  }
66
67
  if (!filepath) {
@@ -68,7 +69,7 @@ function getSettings(mode, filepath, filepathDatetime, timestamp, format, custom
68
69
  filepath = process.env.JET_LOGGER_FILEPATH;
69
70
  }
70
71
  else {
71
- filepath = defaults.filepath;
72
+ filepath = Defaults.Filepath;
72
73
  }
73
74
  }
74
75
  if (filepathDatetime === undefined || filepathDatetime === null) {
@@ -77,7 +78,7 @@ function getSettings(mode, filepath, filepathDatetime, timestamp, format, custom
77
78
  filepathDatetime = (envVar.toUpperCase() === 'TRUE');
78
79
  }
79
80
  else {
80
- filepathDatetime = defaults.filepathDatetime;
81
+ filepathDatetime = Defaults.FilepathDatetime;
81
82
  }
82
83
  }
83
84
  if (timestamp === undefined || timestamp === null) {
@@ -85,7 +86,7 @@ function getSettings(mode, filepath, filepathDatetime, timestamp, format, custom
85
86
  timestamp = (process.env.JET_LOGGER_TIMESTAMP.toUpperCase() === 'TRUE');
86
87
  }
87
88
  else {
88
- timestamp = defaults.timestamp;
89
+ timestamp = Defaults.Timestamp;
89
90
  }
90
91
  }
91
92
  if (!format) {
@@ -93,7 +94,7 @@ function getSettings(mode, filepath, filepathDatetime, timestamp, format, custom
93
94
  format = process.env.JET_LOGGER_FORMAT.toUpperCase();
94
95
  }
95
96
  else {
96
- format = defaults.format;
97
+ format = Defaults.Format;
97
98
  }
98
99
  }
99
100
  if (filepathDatetime) {
@@ -105,7 +106,7 @@ function getSettings(mode, filepath, filepathDatetime, timestamp, format, custom
105
106
  filepathDatetime: filepathDatetime,
106
107
  timestamp: timestamp,
107
108
  format: format,
108
- customLogger: customLogger,
109
+ customLogFn: customLogFn,
109
110
  };
110
111
  }
111
112
  function addDatetimeToFileName(filePath) {
@@ -118,19 +119,19 @@ function addDatetimeToFileName(filePath) {
118
119
  return filePathArr.join('/');
119
120
  }
120
121
  function info(content, printFull) {
121
- return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.info, this.settings);
122
+ return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, Levels.Info, this.settings);
122
123
  }
123
124
  function imp(content, printFull) {
124
- return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.imp, this.settings);
125
+ return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, Levels.Imp, this.settings);
125
126
  }
126
127
  function warn(content, printFull) {
127
- return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.warn, this.settings);
128
+ return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, Levels.Warn, this.settings);
128
129
  }
129
130
  function err(content, printFull) {
130
- return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, levels.err, this.settings);
131
+ return printLog(content, printFull !== null && printFull !== void 0 ? printFull : false, Levels.Err, this.settings);
131
132
  }
132
133
  function printLog(content, printFull, level, settings) {
133
- var mode = settings.mode, format = settings.format, timestamp = settings.timestamp, filepath = settings.filepath, customLogger = settings.customLogger;
134
+ var mode = settings.mode, format = settings.format, timestamp = settings.timestamp, filepath = settings.filepath, customLogFn = settings.customLogFn;
134
135
  if (mode === LoggerModes.Off) {
135
136
  return;
136
137
  }
@@ -138,11 +139,11 @@ function printLog(content, printFull, level, settings) {
138
139
  content = util_1.default.inspect(content);
139
140
  }
140
141
  if (mode === LoggerModes.Custom) {
141
- if (!!customLogger) {
142
- return customLogger(new Date(), level.prefix, content);
142
+ if (!!customLogFn) {
143
+ return customLogFn(new Date(), level.Prefix, content);
143
144
  }
144
145
  else {
145
- throw Error(errors.customLoggerErr);
146
+ throw Error(Errors.customLogFn);
146
147
  }
147
148
  }
148
149
  if (format === Formats.Line) {
@@ -152,7 +153,7 @@ function printLog(content, printFull, level, settings) {
152
153
  content = setupJsonFormat(content, timestamp, level);
153
154
  }
154
155
  if (mode === LoggerModes.Console) {
155
- var colorFn = colors_1.default[level.color];
156
+ var colorFn = colors_1.default[level.Color];
156
157
  console.log(colorFn(content));
157
158
  }
158
159
  else if (mode === LoggerModes.File) {
@@ -160,11 +161,11 @@ function printLog(content, printFull, level, settings) {
160
161
  .catch(function (err) { return console.log(err); });
161
162
  }
162
163
  else {
163
- throw Error(errors.modeErr);
164
+ throw Error(Errors.Mode);
164
165
  }
165
166
  }
166
167
  function setupLineFormat(content, timestamp, level) {
167
- content = (level.prefix + ': ' + content);
168
+ content = (level.Prefix + ': ' + content);
168
169
  if (timestamp) {
169
170
  var time = '[' + new Date().toISOString() + '] ';
170
171
  return (time + content);
@@ -173,7 +174,7 @@ function setupLineFormat(content, timestamp, level) {
173
174
  }
174
175
  function setupJsonFormat(content, timestamp, level) {
175
176
  var json = {
176
- level: level.prefix,
177
+ level: level.Prefix,
177
178
  message: content,
178
179
  };
179
180
  if (timestamp) {
@@ -186,3 +187,4 @@ function writeToFile(content, filePath) {
186
187
  return fs_1.default.appendFile(filePath, content, (function (err) { return !!err ? rej(err) : res(); }));
187
188
  });
188
189
  }
190
+ exports.default = jetLogger();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jet-logger",
3
- "version": "1.2.6",
3
+ "version": "1.3.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",
@@ -8,7 +8,8 @@
8
8
  "lib": "lib"
9
9
  },
10
10
  "scripts": {
11
- "test": "echo \"Error: no test specified\" && exit 1"
11
+ "test": "npx ts-node ./test",
12
+ "build": "tsc -p tsconfig.build.json"
12
13
  },
13
14
  "repository": {
14
15
  "type": "git",
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "exclude": [
4
+ "./node_modules",
5
+ "test"
6
+ ]
7
+ }
@@ -1,31 +0,0 @@
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 {};