@athenna/logger 1.0.7 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@athenna/logger",
3
- "version": "1.0.7",
3
+ "version": "1.1.0",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "author": "João Lenon <lenon@athenna.io>",
@@ -155,6 +155,7 @@
155
155
  "dependencies": {
156
156
  "@athenna/ioc": "1.0.9",
157
157
  "@secjs/utils": "1.8.0",
158
+ "chalk": "4.1.1",
158
159
  "reflect-metadata": "0.1.13",
159
160
  "tscpaths": "0.0.9"
160
161
  }
@@ -0,0 +1 @@
1
+ export declare type LevelTypes = 'info' | 'INFO' | 'debug' | 'DEBUG' | 'warn' | 'WARN' | 'error' | 'ERROR' | 'success' | 'SUCCESS';
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -16,15 +16,15 @@ class ConsoleDriver {
16
16
  const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
17
17
  this._formatter = configs.formatter || channelConfig.formatter;
18
18
  this._streamType = configs.streamType || channelConfig.streamType;
19
- this._formatterConfig =
20
- configs.formatterConfig || channelConfig.formatterConfig;
19
+ this._formatterConfig = Object.assign({}, channelConfig.formatterConfig, configs.formatterConfig);
21
20
  }
22
21
  transport(message, options) {
23
22
  options = Object.assign({}, {
24
23
  formatter: this._formatter,
25
24
  streamType: this._streamType,
26
25
  }, options);
27
- message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, options.formatterConfig || this._formatterConfig);
26
+ const formatterOptions = Object.assign({}, this._formatterConfig, options.formatterConfig);
27
+ message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, formatterOptions);
28
28
  process[options.streamType].write(`${message}\n`);
29
29
  }
30
30
  }
@@ -17,15 +17,15 @@ class DebugDriver {
17
17
  const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
18
18
  this._formatter = configs.formatter || channelConfig.formatter;
19
19
  this._namespace = configs.namespace || channelConfig.namespace;
20
- this._formatterConfig =
21
- configs.formatterConfig || channelConfig.formatterConfig;
20
+ this._formatterConfig = Object.assign({}, channelConfig.formatterConfig, configs.formatterConfig);
22
21
  }
23
22
  transport(message, options) {
24
23
  options = Object.assign({}, {
25
24
  formatter: this._formatter,
26
25
  namespace: this._namespace,
27
26
  }, options);
28
- message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, options.formatterConfig || this._formatterConfig);
27
+ const formatterOptions = Object.assign({}, this._formatterConfig, options.formatterConfig);
28
+ message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, formatterOptions);
29
29
  debug_1.debug(options.namespace)(message);
30
30
  }
31
31
  }
@@ -19,8 +19,7 @@ class FileDriver {
19
19
  const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
20
20
  this._filePath = configs.filePath || channelConfig.filePath;
21
21
  this._formatter = configs.formatter || channelConfig.formatter;
22
- this._formatterConfig =
23
- configs.formatterConfig || channelConfig.formatterConfig;
22
+ this._formatterConfig = Object.assign({}, channelConfig.formatterConfig, configs.formatterConfig);
24
23
  }
25
24
  async transport(message, options) {
26
25
  options = Object.assign({}, {
@@ -32,7 +31,8 @@ class FileDriver {
32
31
  if (!fs_1.existsSync(dir)) {
33
32
  fs_1.mkdirSync(dir, { recursive: true });
34
33
  }
35
- message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, options.formatterConfig || this._formatterConfig);
34
+ const formatterOptions = Object.assign({}, this._formatterConfig, options.formatterConfig);
35
+ message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, formatterOptions);
36
36
  return new Promise((resolve, reject) => {
37
37
  const stream = fs_1.createWriteStream(filePath, { flags: 'a' });
38
38
  stream.write(`${Color_1.Color.removeColors(message)}` + '\n');
@@ -9,12 +9,12 @@
9
9
  */
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
11
  exports.FormatterFactory = void 0;
12
- const LogFormatter_1 = require("../Formatters/LogFormatter");
13
12
  const JsonFormatter_1 = require("../Formatters/JsonFormatter");
14
- const DebugFormatter_1 = require("../Formatters/DebugFormatter");
15
- const ContextFormatter_1 = require("../Formatters/ContextFormatter");
13
+ const NestFormatter_1 = require("../Formatters/NestFormatter");
14
+ const SimpleFormatter_1 = require("../Formatters/SimpleFormatter");
16
15
  const NotFoundFormatterException_1 = require("../Exceptions/NotFoundFormatterException");
17
16
  const FormatterAlreadyExistException_1 = require("../Exceptions/FormatterAlreadyExistException");
17
+ const CliFormatter_1 = require("../Formatters/CliFormatter");
18
18
  class FormatterFactory {
19
19
  static availableFormatters() {
20
20
  const availableFormatters = [];
@@ -39,7 +39,7 @@ class FormatterFactory {
39
39
  }
40
40
  exports.FormatterFactory = FormatterFactory;
41
41
  FormatterFactory.formatters = new Map()
42
- .set('context', { Formatter: ContextFormatter_1.ContextFormatter })
43
- .set('debug', { Formatter: DebugFormatter_1.DebugFormatter })
42
+ .set('cli', { Formatter: CliFormatter_1.CliFormatter })
43
+ .set('nest', { Formatter: NestFormatter_1.NestFormatter })
44
44
  .set('json', { Formatter: JsonFormatter_1.JsonFormatter })
45
- .set('log', { Formatter: LogFormatter_1.LogFormatter });
45
+ .set('simple', { Formatter: SimpleFormatter_1.SimpleFormatter });
@@ -7,12 +7,13 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
  import { Chalk } from 'chalk';
10
+ import { LevelTypes } from '../Contracts/LevelTypes';
10
11
  import { FormatterContract } from '../Contracts/FormatterContract';
11
- export interface LogFormatterOptions {
12
- color: Chalk;
13
- level: 'INFO' | 'DEBUG' | 'WARN' | 'ERROR' | 'SUCCESS';
12
+ export interface CliFormatterOptions {
13
+ chalk: Chalk;
14
+ level: LevelTypes;
14
15
  }
15
- export declare class LogFormatter implements FormatterContract {
16
- format(message: string, options?: LogFormatterOptions): string;
16
+ export declare class CliFormatter implements FormatterContract {
17
17
  private static paintByLevel;
18
+ format(message: string, options: CliFormatterOptions): string;
18
19
  }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ /**
3
+ * @athenna/logger
4
+ *
5
+ * (c) João Lenon <lenon@athenna.io>
6
+ *
7
+ * For the full copyright and license information, please view the LICENSE
8
+ * file that was distributed with this source code.
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.CliFormatter = void 0;
12
+ const Color_1 = require("../Utils/Color");
13
+ class CliFormatter {
14
+ static paintByLevel(level) {
15
+ const levelColors = {
16
+ info: Color_1.Color.info,
17
+ debug: Color_1.Color.debug,
18
+ warn: Color_1.Color.warning,
19
+ error: Color_1.Color.error,
20
+ success: Color_1.Color.log,
21
+ };
22
+ return levelColors[level.toLowerCase()](`[ ${level.toLowerCase()} ]`);
23
+ }
24
+ format(message, options) {
25
+ const level = CliFormatter.paintByLevel(options.level);
26
+ return `${level} ${message}`;
27
+ }
28
+ }
29
+ exports.CliFormatter = CliFormatter;
@@ -9,8 +9,8 @@
9
9
  import { Chalk } from 'chalk';
10
10
  import { FormatterContract } from '../Contracts/FormatterContract';
11
11
  export interface JsonFormatterOptions {
12
- color: Chalk;
12
+ chalk: Chalk;
13
13
  }
14
14
  export declare class JsonFormatter implements FormatterContract {
15
- format(message: Record<any, unknown>, options?: JsonFormatterOptions): string;
15
+ format(message: Record<any, unknown>, options: JsonFormatterOptions): string;
16
16
  }
@@ -12,9 +12,8 @@ exports.JsonFormatter = void 0;
12
12
  const Color_1 = require("../Utils/Color");
13
13
  class JsonFormatter {
14
14
  format(message, options) {
15
- options = Object.assign({}, { color: Color_1.Color.green }, options);
16
15
  const pid = Color_1.Color.yellow(`[Athenna] - PID: ${process.pid}`);
17
- return `${pid} - ${Color_1.Color.bold('JSON:')} ${options.color(JSON.stringify(message, null, 2))}`;
16
+ return `${pid} - ${Color_1.Color.bold('JSON:')} ${options.chalk(JSON.stringify(message, null, 2))}`;
18
17
  }
19
18
  }
20
19
  exports.JsonFormatter = JsonFormatter;
@@ -9,11 +9,11 @@
9
9
  import { Chalk } from 'chalk';
10
10
  import { FormatterContract } from '../Contracts/FormatterContract';
11
11
  export interface ContextFormatterOptions {
12
- color: Chalk;
12
+ chalk: Chalk;
13
13
  context: string;
14
14
  }
15
- export declare class ContextFormatter implements FormatterContract {
16
- format(message: string, options?: ContextFormatterOptions): string;
15
+ export declare class NestFormatter implements FormatterContract {
17
16
  private static lastTimestamp?;
18
17
  private static getTimestampDiff;
18
+ format(message: string, options: ContextFormatterOptions): string;
19
19
  }
@@ -8,18 +8,10 @@
8
8
  * file that was distributed with this source code.
9
9
  */
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.ContextFormatter = void 0;
11
+ exports.NestFormatter = void 0;
12
12
  const Color_1 = require("../Utils/Color");
13
13
  const getTimestamp_1 = require("../Utils/getTimestamp");
14
- class ContextFormatter {
15
- format(message, options) {
16
- options = Object.assign({}, { color: Color_1.Color.green, context: 'Logger' }, options);
17
- const pid = Color_1.Color.yellow(`[Athenna] - PID: ${process.pid}`);
18
- const timestamp = getTimestamp_1.getTimestamp();
19
- const messageCtx = Color_1.Color.yellow(`[${options.context}] `);
20
- const timestampDiff = ContextFormatter.getTimestampDiff();
21
- return `${pid} - ${timestamp} ${messageCtx}${options.color(message)}${timestampDiff}`;
22
- }
14
+ class NestFormatter {
23
15
  static getTimestampDiff() {
24
16
  let result = '';
25
17
  if (this.lastTimestamp) {
@@ -28,5 +20,12 @@ class ContextFormatter {
28
20
  this.lastTimestamp = Date.now();
29
21
  return result;
30
22
  }
23
+ format(message, options) {
24
+ const pid = Color_1.Color.yellow(`[Athenna] - PID: ${process.pid}`);
25
+ const timestamp = getTimestamp_1.getTimestamp();
26
+ const messageCtx = Color_1.Color.yellow(`[${options.context}] `);
27
+ const timestampDiff = NestFormatter.getTimestampDiff();
28
+ return `${pid} - ${timestamp} ${messageCtx}${options.chalk(message)}${timestampDiff}`;
29
+ }
31
30
  }
32
- exports.ContextFormatter = ContextFormatter;
31
+ exports.NestFormatter = NestFormatter;
@@ -7,14 +7,15 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
  import { Chalk } from 'chalk';
10
+ import { LevelTypes } from '../Contracts/LevelTypes';
10
11
  import { FormatterContract } from '../Contracts/FormatterContract';
11
- export interface DebugFormatterOptions {
12
- color: Chalk;
13
- context: string;
14
- namespace: string;
12
+ export interface LogFormatterOptions {
13
+ chalk: Chalk;
14
+ level: LevelTypes;
15
15
  }
16
- export declare class DebugFormatter implements FormatterContract {
17
- format(message: string, options?: DebugFormatterOptions): string;
16
+ export declare class SimpleFormatter implements FormatterContract {
18
17
  private static lastTimestamp?;
19
18
  private static getTimestampDiff;
19
+ private static paintByLevel;
20
+ format(message: string, options: LogFormatterOptions): string;
20
21
  }
@@ -8,16 +8,17 @@
8
8
  * file that was distributed with this source code.
9
9
  */
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.LogFormatter = void 0;
11
+ exports.SimpleFormatter = void 0;
12
12
  const Color_1 = require("../Utils/Color");
13
13
  const getTimestamp_1 = require("../Utils/getTimestamp");
14
- class LogFormatter {
15
- format(message, options) {
16
- options = Object.assign({}, { color: Color_1.Color.green, level: 'info' }, options);
17
- const pid = Color_1.Color.yellow(`[Athenna] - PID: ${process.pid}`);
18
- const timestamp = getTimestamp_1.getTimestamp();
19
- const level = LogFormatter.paintByLevel(options.level);
20
- return `${pid} - ${timestamp} ${level} ${options.color(message)}`;
14
+ class SimpleFormatter {
15
+ static getTimestampDiff() {
16
+ let result = '';
17
+ if (this.lastTimestamp) {
18
+ result = Color_1.Color.yellow(` +${Date.now() - this.lastTimestamp}ms`);
19
+ }
20
+ this.lastTimestamp = Date.now();
21
+ return result;
21
22
  }
22
23
  static paintByLevel(level) {
23
24
  const levelColors = {
@@ -27,7 +28,13 @@ class LogFormatter {
27
28
  error: Color_1.Color.error,
28
29
  success: Color_1.Color.log,
29
30
  };
30
- return levelColors[level.toLowerCase()](`[${level}]`);
31
+ return levelColors[level.toLowerCase()](`[${level.toUpperCase()}]`);
32
+ }
33
+ format(message, options) {
34
+ const timestamp = getTimestamp_1.getTimestamp();
35
+ const timestampDiff = SimpleFormatter.getTimestampDiff();
36
+ const level = SimpleFormatter.paintByLevel(options.level);
37
+ return `${level} - ${timestamp} ${options.chalk(message)}${timestampDiff}`;
31
38
  }
32
39
  }
33
- exports.LogFormatter = LogFormatter;
40
+ exports.SimpleFormatter = SimpleFormatter;
package/src/Logger.d.ts CHANGED
@@ -9,6 +9,7 @@ export declare class Logger {
9
9
  static get formatters(): string[];
10
10
  static buildDriver(name: string, driver: new (channel: string, configs?: any) => DriverContract): void;
11
11
  static buildFormatter(name: string, formatter: new () => FormatterContract): void;
12
+ private static applyLogEngine;
12
13
  channel(channel: string, runtimeConfig?: any): Logger;
13
14
  log(message: any, options?: {}): void | Promise<void>;
14
15
  info(message: any, options?: {}): void | Promise<void>;
package/src/Logger.js CHANGED
@@ -1,6 +1,10 @@
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
6
  exports.Logger = void 0;
7
+ const chalk_1 = __importDefault(require("chalk"));
4
8
  const Color_1 = require("./Utils/Color");
5
9
  const utils_1 = require("@secjs/utils");
6
10
  const DriverFactory_1 = require("./Factories/DriverFactory");
@@ -24,6 +28,28 @@ class Logger {
24
28
  static buildFormatter(name, formatter) {
25
29
  FormatterFactory_1.FormatterFactory.createFormatter(name, formatter);
26
30
  }
31
+ static applyLogEngine(content) {
32
+ if (utils_1.Is.String(content)) {
33
+ const matches = content.match(/\({(.*?)} (.*?)\)/);
34
+ if (matches) {
35
+ const chalkMethodsString = matches[1].replace(/\s/g, '');
36
+ const chalkMethodsArray = chalkMethodsString.split(',');
37
+ const message = matches[2];
38
+ let chalk = chalk_1.default;
39
+ chalkMethodsArray.forEach(chalkMethod => {
40
+ if (!chalk[chalkMethod])
41
+ return;
42
+ chalk = chalk[chalkMethod];
43
+ });
44
+ content = content
45
+ .replace(`({${matches[1]}} `, '')
46
+ .replace(`({${matches[1]}}`, '')
47
+ .replace(`${matches[2]})`, chalk(message));
48
+ }
49
+ return content;
50
+ }
51
+ return content;
52
+ }
27
53
  channel(channel, runtimeConfig) {
28
54
  if (runtimeConfig)
29
55
  this.runtimeConfig = runtimeConfig;
@@ -34,6 +60,7 @@ class Logger {
34
60
  options = this.createOptions(options, {
35
61
  streamType: 'stdout',
36
62
  });
63
+ message = Logger.applyLogEngine(message);
37
64
  return this.driver.transport(message, options);
38
65
  }
39
66
  info(message, options = {}) {
@@ -41,9 +68,10 @@ class Logger {
41
68
  streamType: 'stdout',
42
69
  formatterConfig: {
43
70
  level: 'INFO',
44
- color: Color_1.Color.cyan,
71
+ chalk: Color_1.Color.cyan,
45
72
  },
46
73
  });
74
+ message = Logger.applyLogEngine(message);
47
75
  return this.driver.transport(message, options);
48
76
  }
49
77
  warn(message, options = {}) {
@@ -51,9 +79,10 @@ class Logger {
51
79
  streamType: 'stdout',
52
80
  formatterConfig: {
53
81
  level: 'WARN',
54
- color: Color_1.Color.orange,
82
+ chalk: Color_1.Color.orange,
55
83
  },
56
84
  });
85
+ message = Logger.applyLogEngine(message);
57
86
  return this.driver.transport(message, options);
58
87
  }
59
88
  error(message, options = {}) {
@@ -61,9 +90,10 @@ class Logger {
61
90
  streamType: 'stdout',
62
91
  formatterConfig: {
63
92
  level: 'ERROR',
64
- color: Color_1.Color.red,
93
+ chalk: Color_1.Color.red,
65
94
  },
66
95
  });
96
+ message = Logger.applyLogEngine(message);
67
97
  return this.driver.transport(message, options);
68
98
  }
69
99
  debug(message, options = {}) {
@@ -71,9 +101,10 @@ class Logger {
71
101
  streamType: 'stdout',
72
102
  formatterConfig: {
73
103
  level: 'DEBUG',
74
- color: Color_1.Color.purple,
104
+ chalk: Color_1.Color.purple,
75
105
  },
76
106
  });
107
+ message = Logger.applyLogEngine(message);
77
108
  return this.driver.transport(message, options);
78
109
  }
79
110
  success(message, options = {}) {
@@ -81,9 +112,10 @@ class Logger {
81
112
  streamType: 'stdout',
82
113
  formatterConfig: {
83
114
  level: 'SUCCESS',
84
- color: Color_1.Color.green,
115
+ chalk: Color_1.Color.green,
85
116
  },
86
117
  });
118
+ message = Logger.applyLogEngine(message);
87
119
  return this.driver.transport(message, options);
88
120
  }
89
121
  createOptions(options, defaultValues) {
@@ -6,11 +6,19 @@
6
6
  * For the full copyright and license information, please view the LICENSE
7
7
  * file that was distributed with this source code.
8
8
  */
9
- import { Chalk } from 'chalk';
9
+ import chalk, { Chalk } from 'chalk';
10
10
  declare type Methods = 'GET' | 'POST' | 'PUT' | 'DELETE';
11
11
  export declare class Color {
12
- static chalk: Chalk & {
13
- supportsColor: import("chalk").ColorSupport;
12
+ static chalk: chalk.Chalk & chalk.ChalkFunction & {
13
+ supportsColor: false | chalk.ColorSupport;
14
+ Level: chalk.Level;
15
+ Color: ("black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white" | "gray" | "grey" | "blackBright" | "redBright" | "greenBright" | "yellowBright" | "blueBright" | "magentaBright" | "cyanBright" | "whiteBright") | ("bgBlack" | "bgRed" | "bgGreen" | "bgYellow" | "bgBlue" | "bgMagenta" | "bgCyan" | "bgWhite" | "bgGray" | "bgGrey" | "bgBlackBright" | "bgRedBright" | "bgGreenBright" | "bgYellowBright" | "bgBlueBright" | "bgMagentaBright" | "bgCyanBright" | "bgWhiteBright");
16
+ ForegroundColor: "black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white" | "gray" | "grey" | "blackBright" | "redBright" | "greenBright" | "yellowBright" | "blueBright" | "magentaBright" | "cyanBright" | "whiteBright";
17
+ BackgroundColor: "bgBlack" | "bgRed" | "bgGreen" | "bgYellow" | "bgBlue" | "bgMagenta" | "bgCyan" | "bgWhite" | "bgGray" | "bgGrey" | "bgBlackBright" | "bgRedBright" | "bgGreenBright" | "bgYellowBright" | "bgBlueBright" | "bgMagentaBright" | "bgCyanBright" | "bgWhiteBright";
18
+ Modifiers: "reset" | "bold" | "dim" | "italic" | "underline" | "inverse" | "hidden" | "strikethrough" | "visible";
19
+ stderr: chalk.Chalk & {
20
+ supportsColor: false | chalk.ColorSupport;
21
+ };
14
22
  };
15
23
  static get bold(): Chalk;
16
24
  static get purple(): Chalk;
@@ -1,32 +0,0 @@
1
- "use strict";
2
- /**
3
- * @athenna/logger
4
- *
5
- * (c) João Lenon <lenon@athenna.io>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.DebugFormatter = void 0;
12
- const Color_1 = require("../Utils/Color");
13
- const getTimestamp_1 = require("../Utils/getTimestamp");
14
- class DebugFormatter {
15
- format(message, options) {
16
- options = Object.assign({}, { color: Color_1.Color.green, context: 'Debugger', namespace: 'api:main' }, options);
17
- const pid = Color_1.Color.yellow(`[Athenna Debugger] - PID: ${process.pid}`);
18
- const timestamp = Color_1.Color.white(getTimestamp_1.getTimestamp());
19
- const messageCtx = Color_1.Color.yellow(`[${options.context}] `);
20
- const timestampDiff = DebugFormatter.getTimestampDiff();
21
- return `${pid} - ${timestamp} ${messageCtx}${options.color(message)}${timestampDiff}`;
22
- }
23
- static getTimestampDiff() {
24
- let result = '';
25
- if (this.lastTimestamp) {
26
- result = Color_1.Color.yellow(` +${Date.now() - this.lastTimestamp}ms`);
27
- }
28
- this.lastTimestamp = Date.now();
29
- return result;
30
- }
31
- }
32
- exports.DebugFormatter = DebugFormatter;