@athenna/logger 1.0.5 → 1.0.8

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.5",
3
+ "version": "1.0.8",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "author": "João Lenon <lenon@athenna.io>",
@@ -153,8 +153,9 @@
153
153
  }
154
154
  },
155
155
  "dependencies": {
156
- "@athenna/ioc": "1.0.7",
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
  }
@@ -14,12 +14,12 @@ export interface ConsoleDriverOpts {
14
14
  context: string;
15
15
  formatter: string;
16
16
  streamType: string;
17
+ formatterConfig: any;
17
18
  }
18
19
  export declare class ConsoleDriver implements DriverContract {
19
- private readonly _level;
20
- private readonly _context;
21
20
  private readonly _formatter;
22
21
  private readonly _streamType;
22
+ private readonly _formatterConfig;
23
23
  constructor(channel: string, configs?: any);
24
24
  transport(message: string, options?: ConsoleDriverOpts): void;
25
25
  }
@@ -14,19 +14,17 @@ const FormatterFactory_1 = require("../Factories/FormatterFactory");
14
14
  class ConsoleDriver {
15
15
  constructor(channel, configs = {}) {
16
16
  const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
17
- this._level = configs.level || channelConfig.level;
18
- this._context = configs.context || channelConfig.context;
19
17
  this._formatter = configs.formatter || channelConfig.formatter;
20
18
  this._streamType = configs.streamType || channelConfig.streamType;
19
+ this._formatterConfig =
20
+ configs.formatterConfig || channelConfig.formatterConfig;
21
21
  }
22
22
  transport(message, options) {
23
23
  options = Object.assign({}, {
24
- level: this._level,
25
- context: this._context,
26
24
  formatter: this._formatter,
27
25
  streamType: this._streamType,
28
26
  }, options);
29
- message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, options);
27
+ message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, options.formatterConfig || this._formatterConfig);
30
28
  process[options.streamType].write(`${message}\n`);
31
29
  }
32
30
  }
@@ -14,12 +14,12 @@ export interface DebugDriverOpts {
14
14
  context: string;
15
15
  formatter: string;
16
16
  namespace: string;
17
+ formatterConfig: any;
17
18
  }
18
19
  export declare class DebugDriver implements DriverContract {
19
- private readonly _level;
20
- private readonly _context;
21
20
  private readonly _formatter;
22
21
  private readonly _namespace;
22
+ private readonly _formatterConfig;
23
23
  constructor(channel: string, configs?: any);
24
24
  transport(message: string, options?: DebugDriverOpts): void;
25
25
  }
@@ -15,19 +15,17 @@ const FormatterFactory_1 = require("../Factories/FormatterFactory");
15
15
  class DebugDriver {
16
16
  constructor(channel, configs = {}) {
17
17
  const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
18
- this._level = configs.level || channelConfig.level;
19
- this._context = configs.context || channelConfig.context;
20
18
  this._formatter = configs.formatter || channelConfig.formatter;
21
19
  this._namespace = configs.namespace || channelConfig.namespace;
20
+ this._formatterConfig =
21
+ configs.formatterConfig || channelConfig.formatterConfig;
22
22
  }
23
23
  transport(message, options) {
24
24
  options = Object.assign({}, {
25
- level: this._level,
26
- context: this._context,
27
25
  formatter: this._formatter,
28
26
  namespace: this._namespace,
29
27
  }, options);
30
- message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, options);
28
+ message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, options.formatterConfig || this._formatterConfig);
31
29
  debug_1.debug(options.namespace)(message);
32
30
  }
33
31
  }
@@ -12,12 +12,12 @@ export interface FileDriverOpts {
12
12
  context: string;
13
13
  formatter: string;
14
14
  filePath: string;
15
+ formatterConfig: any;
15
16
  }
16
17
  export declare class FileDriver implements DriverContract {
17
- private readonly _level;
18
- private readonly _context;
19
18
  private readonly _filePath;
20
19
  private readonly _formatter;
20
+ private readonly _formatterConfig;
21
21
  constructor(channel: string, configs?: any);
22
22
  transport(message: string, options?: FileDriverOpts): Promise<void>;
23
23
  }
@@ -17,15 +17,13 @@ const FormatterFactory_1 = require("../Factories/FormatterFactory");
17
17
  class FileDriver {
18
18
  constructor(channel, configs = {}) {
19
19
  const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
20
- this._level = configs.level || channelConfig.level;
21
- this._context = configs.context || channelConfig.context;
22
20
  this._filePath = configs.filePath || channelConfig.filePath;
23
21
  this._formatter = configs.formatter || channelConfig.formatter;
22
+ this._formatterConfig =
23
+ configs.formatterConfig || channelConfig.formatterConfig;
24
24
  }
25
25
  async transport(message, options) {
26
26
  options = Object.assign({}, {
27
- level: this._level,
28
- context: this._context,
29
27
  filePath: this._filePath,
30
28
  formatter: this._formatter,
31
29
  }, options);
@@ -34,7 +32,7 @@ class FileDriver {
34
32
  if (!fs_1.existsSync(dir)) {
35
33
  fs_1.mkdirSync(dir, { recursive: true });
36
34
  }
37
- message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, options);
35
+ message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, options.formatterConfig || this._formatterConfig);
38
36
  return new Promise((resolve, reject) => {
39
37
  const stream = fs_1.createWriteStream(filePath, { flags: 'a' });
40
38
  stream.write(`${Color_1.Color.removeColors(message)}` + '\n');
package/src/Logger.d.ts CHANGED
@@ -4,16 +4,18 @@ export declare class Logger {
4
4
  private runtimeConfig;
5
5
  private channelName;
6
6
  private driver;
7
- static buildDriver(name: string, driver: new (channel: string, configs?: any) => DriverContract): void;
8
- static buildFormatter(name: string, formatter: new () => FormatterContract): void;
7
+ constructor(runtimeConfig?: any);
9
8
  static get drivers(): string[];
10
9
  static get formatters(): string[];
11
- constructor(runtimeConfig?: any);
10
+ static buildDriver(name: string, driver: new (channel: string, configs?: any) => DriverContract): void;
11
+ static buildFormatter(name: string, formatter: new () => FormatterContract): void;
12
+ private static applyLogEngine;
12
13
  channel(channel: string, runtimeConfig?: any): Logger;
13
- log(message: any, options?: any): Promise<void>;
14
- info(message: any, options?: any): Promise<void>;
15
- warn(message: any, options?: any): Promise<void>;
16
- error(message: any, options?: any): Promise<void>;
17
- debug(message: any, options?: any): Promise<void>;
18
- success(message: any, options?: any): Promise<void>;
14
+ log(message: any, options?: {}): void | Promise<void>;
15
+ info(message: any, options?: {}): void | Promise<void>;
16
+ warn(message: any, options?: {}): void | Promise<void>;
17
+ error(message: any, options?: {}): void | Promise<void>;
18
+ debug(message: any, options?: {}): void | Promise<void>;
19
+ success(message: any, options?: {}): void | Promise<void>;
20
+ private createOptions;
19
21
  }
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");
@@ -12,17 +16,39 @@ class Logger {
12
16
  this.channelName = 'default';
13
17
  this.driver = DriverFactory_1.DriverFactory.fabricate(this.channelName, this.runtimeConfig);
14
18
  }
19
+ static get drivers() {
20
+ return DriverFactory_1.DriverFactory.availableDrivers();
21
+ }
22
+ static get formatters() {
23
+ return FormatterFactory_1.FormatterFactory.availableFormatters();
24
+ }
15
25
  static buildDriver(name, driver) {
16
26
  DriverFactory_1.DriverFactory.createDriver(name, driver);
17
27
  }
18
28
  static buildFormatter(name, formatter) {
19
29
  FormatterFactory_1.FormatterFactory.createFormatter(name, formatter);
20
30
  }
21
- static get drivers() {
22
- return DriverFactory_1.DriverFactory.availableDrivers();
23
- }
24
- static get formatters() {
25
- return FormatterFactory_1.FormatterFactory.availableFormatters();
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;
26
52
  }
27
53
  channel(channel, runtimeConfig) {
28
54
  if (runtimeConfig)
@@ -30,80 +56,86 @@ class Logger {
30
56
  this.driver = DriverFactory_1.DriverFactory.fabricate(channel, this.runtimeConfig);
31
57
  return this;
32
58
  }
33
- async log(message, options) {
34
- options = Object.assign({}, { context: 'Logger' }, options);
35
- if (this.runtimeConfig && this.runtimeConfig.formatterConfig) {
36
- options = {
37
- ...options,
38
- ...this.runtimeConfig.formatterConfig,
39
- };
40
- }
41
- await this.driver.transport(message, options);
59
+ log(message, options = {}) {
60
+ options = this.createOptions(options, {
61
+ streamType: 'stdout',
62
+ });
63
+ message = Logger.applyLogEngine(message);
64
+ return this.driver.transport(message, options);
42
65
  }
43
- async info(message, options) {
44
- options = Object.assign({}, { context: 'Logger' }, options);
45
- options.level = 'INFO';
46
- options.color = Color_1.Color.cyan;
47
- options.streamType = 'stdout';
48
- if (this.runtimeConfig && this.runtimeConfig.formatterConfig) {
49
- options = {
50
- ...options,
51
- ...this.runtimeConfig.formatterConfig,
52
- };
53
- }
54
- await this.driver.transport(message, options);
66
+ info(message, options = {}) {
67
+ options = this.createOptions(options, {
68
+ streamType: 'stdout',
69
+ formatterConfig: {
70
+ level: 'INFO',
71
+ color: Color_1.Color.cyan,
72
+ },
73
+ });
74
+ message = Logger.applyLogEngine(message);
75
+ return this.driver.transport(message, options);
55
76
  }
56
- async warn(message, options) {
57
- options = Object.assign({}, { context: 'Logger' }, options);
58
- options.level = 'WARN';
59
- options.color = Color_1.Color.orange;
60
- options.streamType = 'stdout';
61
- if (this.runtimeConfig && this.runtimeConfig.formatterConfig) {
62
- options = {
63
- ...options,
64
- ...this.runtimeConfig.formatterConfig,
65
- };
66
- }
67
- await this.driver.transport(message, options);
77
+ warn(message, options = {}) {
78
+ options = this.createOptions(options, {
79
+ streamType: 'stdout',
80
+ formatterConfig: {
81
+ level: 'WARN',
82
+ color: Color_1.Color.orange,
83
+ },
84
+ });
85
+ message = Logger.applyLogEngine(message);
86
+ return this.driver.transport(message, options);
68
87
  }
69
- async error(message, options) {
70
- options = Object.assign({}, { context: 'Logger' }, options);
71
- options.level = 'ERROR';
72
- options.color = Color_1.Color.red;
73
- options.streamType = 'stderr';
74
- if (this.runtimeConfig && this.runtimeConfig.formatterConfig) {
75
- options = {
76
- ...options,
77
- ...this.runtimeConfig.formatterConfig,
78
- };
79
- }
80
- await this.driver.transport(message, options);
88
+ error(message, options = {}) {
89
+ options = this.createOptions(options, {
90
+ streamType: 'stdout',
91
+ formatterConfig: {
92
+ level: 'ERROR',
93
+ color: Color_1.Color.red,
94
+ },
95
+ });
96
+ message = Logger.applyLogEngine(message);
97
+ return this.driver.transport(message, options);
81
98
  }
82
- async debug(message, options) {
83
- options = Object.assign({}, { context: 'Logger' }, options);
84
- options.level = 'DEBUG';
85
- options.color = Color_1.Color.purple;
86
- options.streamType = 'stdout';
87
- if (this.runtimeConfig && this.runtimeConfig.formatterConfig) {
88
- options = {
89
- ...options,
90
- ...this.runtimeConfig.formatterConfig,
91
- };
92
- }
93
- await this.driver.transport(message, options);
99
+ debug(message, options = {}) {
100
+ options = this.createOptions(options, {
101
+ streamType: 'stdout',
102
+ formatterConfig: {
103
+ level: 'DEBUG',
104
+ color: Color_1.Color.purple,
105
+ },
106
+ });
107
+ message = Logger.applyLogEngine(message);
108
+ return this.driver.transport(message, options);
109
+ }
110
+ success(message, options = {}) {
111
+ options = this.createOptions(options, {
112
+ streamType: 'stdout',
113
+ formatterConfig: {
114
+ level: 'SUCCESS',
115
+ color: Color_1.Color.green,
116
+ },
117
+ });
118
+ message = Logger.applyLogEngine(message);
119
+ return this.driver.transport(message, options);
94
120
  }
95
- async success(message, options) {
96
- options = Object.assign({}, { context: 'Logger' }, options);
97
- options.level = 'SUCCESS';
98
- options.color = Color_1.Color.green;
99
- options.streamType = 'stdout';
100
- if (this.runtimeConfig && this.runtimeConfig.formatterConfig) {
101
- options = {
102
- ...options,
121
+ createOptions(options, defaultValues) {
122
+ let formatterConfig = Object.assign({}, {
123
+ ...defaultValues.formatterConfig,
124
+ }, options.formatterConfig);
125
+ if (this.runtimeConfig.formatterConfig) {
126
+ formatterConfig = {
103
127
  ...this.runtimeConfig.formatterConfig,
128
+ ...formatterConfig,
104
129
  };
105
130
  }
106
- await this.driver.transport(message, options);
131
+ options = Object.assign({}, {
132
+ streamType: 'stdout',
133
+ }, options);
134
+ options = {
135
+ ...options,
136
+ formatterConfig,
137
+ };
138
+ return options;
107
139
  }
108
140
  }
109
141
  exports.Logger = Logger;
@@ -8,6 +8,10 @@
8
8
  */
9
9
  import { ServiceProvider } from '@athenna/ioc';
10
10
  export declare class LoggerProvider extends ServiceProvider {
11
- boot(): void;
11
+ /**
12
+ * Register any application services.
13
+ *
14
+ * @return void
15
+ */
12
16
  register(): void;
13
17
  }
@@ -12,7 +12,11 @@ exports.LoggerProvider = void 0;
12
12
  const Logger_1 = require("../Logger");
13
13
  const ioc_1 = require("@athenna/ioc");
14
14
  class LoggerProvider extends ioc_1.ServiceProvider {
15
- boot() { }
15
+ /**
16
+ * Register any application services.
17
+ *
18
+ * @return void
19
+ */
16
20
  register() {
17
21
  this.container.bind('Athenna/Core/Logger', Logger_1.Logger);
18
22
  }
@@ -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;