@athenna/logger 1.1.7 → 1.2.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.
Files changed (66) hide show
  1. package/LICENSE.md +3 -15
  2. package/README.md +6 -6
  3. package/package.json +88 -101
  4. package/src/Drivers/ConsoleDriver.js +43 -22
  5. package/src/Drivers/DebugDriver.js +45 -23
  6. package/src/Drivers/DiscordDriver.js +57 -0
  7. package/src/Drivers/FileDriver.js +51 -34
  8. package/src/Drivers/NullDriver.js +28 -0
  9. package/src/Drivers/PinoDriver.js +94 -0
  10. package/src/Drivers/SlackDriver.js +54 -0
  11. package/src/Drivers/TelegramDriver.js +53 -0
  12. package/src/Exceptions/DriverExistException.js +31 -0
  13. package/src/Exceptions/FormatterExistException.js +32 -0
  14. package/src/Exceptions/NotFoundChannelException.js +32 -0
  15. package/src/Exceptions/NotFoundDriverException.js +22 -10
  16. package/src/Exceptions/NotFoundFormatterException.js +22 -10
  17. package/src/Exceptions/OnlyPinoPrettyException.js +28 -0
  18. package/src/Facades/Log.js +9 -5
  19. package/src/Factories/DriverFactory.js +98 -46
  20. package/src/Factories/FormatterFactory.js +67 -35
  21. package/src/Formatters/CliFormatter.js +21 -20
  22. package/src/Formatters/JsonFormatter.js +15 -10
  23. package/src/Formatters/MessageFormatter.js +40 -0
  24. package/src/Formatters/NestFormatter.js +31 -22
  25. package/src/Formatters/NoneFormatter.js +21 -0
  26. package/src/Formatters/RequestFormatter.js +49 -37
  27. package/src/Formatters/SimpleFormatter.js +30 -31
  28. package/src/Helpers/ColorHelper.js +259 -0
  29. package/src/Helpers/FactoryHelper.js +121 -0
  30. package/src/Providers/LoggerProvider.js +13 -15
  31. package/src/index.d.ts +383 -0
  32. package/src/index.js +269 -0
  33. package/index.d.ts +0 -13
  34. package/index.js +0 -25
  35. package/src/Contracts/DriverContract.d.ts +0 -11
  36. package/src/Contracts/DriverContract.js +0 -10
  37. package/src/Contracts/FormatterContract.d.ts +0 -11
  38. package/src/Contracts/FormatterContract.js +0 -10
  39. package/src/Contracts/LevelTypes.d.ts +0 -1
  40. package/src/Contracts/LevelTypes.js +0 -2
  41. package/src/Drivers/ConsoleDriver.d.ts +0 -25
  42. package/src/Drivers/DebugDriver.d.ts +0 -25
  43. package/src/Drivers/FileDriver.d.ts +0 -23
  44. package/src/Exceptions/ChannelNotConfiguredException.d.ts +0 -12
  45. package/src/Exceptions/ChannelNotConfiguredException.js +0 -19
  46. package/src/Exceptions/DriverAlreadyExistException.d.ts +0 -12
  47. package/src/Exceptions/DriverAlreadyExistException.js +0 -19
  48. package/src/Exceptions/FormatterAlreadyExistException.d.ts +0 -12
  49. package/src/Exceptions/FormatterAlreadyExistException.js +0 -19
  50. package/src/Exceptions/NotFoundDriverException.d.ts +0 -12
  51. package/src/Exceptions/NotFoundFormatterException.d.ts +0 -12
  52. package/src/Facades/Log.d.ts +0 -10
  53. package/src/Factories/DriverFactory.d.ts +0 -19
  54. package/src/Factories/FormatterFactory.d.ts +0 -18
  55. package/src/Formatters/CliFormatter.d.ts +0 -19
  56. package/src/Formatters/JsonFormatter.d.ts +0 -16
  57. package/src/Formatters/NestFormatter.d.ts +0 -19
  58. package/src/Formatters/RequestFormatter.d.ts +0 -17
  59. package/src/Formatters/SimpleFormatter.d.ts +0 -21
  60. package/src/Logger.d.ts +0 -110
  61. package/src/Logger.js +0 -223
  62. package/src/Providers/LoggerProvider.d.ts +0 -17
  63. package/src/Utils/Color.d.ts +0 -48
  64. package/src/Utils/Color.js +0 -93
  65. package/src/Utils/getTimestamp.d.ts +0 -9
  66. package/src/Utils/getTimestamp.js +0 -23
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * @athenna/logger
4
3
  *
@@ -7,41 +6,74 @@
7
6
  * For the full copyright and license information, please view the LICENSE
8
7
  * file that was distributed with this source code.
9
8
  */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.FormatterFactory = void 0;
12
- const CliFormatter_1 = require("../Formatters/CliFormatter");
13
- const JsonFormatter_1 = require("../Formatters/JsonFormatter");
14
- const NestFormatter_1 = require("../Formatters/NestFormatter");
15
- const SimpleFormatter_1 = require("../Formatters/SimpleFormatter");
16
- const RequestFormatter_1 = require("../Formatters/RequestFormatter");
17
- const NotFoundFormatterException_1 = require("../Exceptions/NotFoundFormatterException");
18
- const FormatterAlreadyExistException_1 = require("../Exceptions/FormatterAlreadyExistException");
19
- class FormatterFactory {
20
- static availableFormatters() {
21
- const availableFormatters = [];
22
- for (const [key] of this.formatters.entries()) {
23
- availableFormatters.push(key);
24
- }
25
- return availableFormatters;
9
+
10
+ import { CliFormatter } from '#src/Formatters/CliFormatter'
11
+ import { NestFormatter } from '#src/Formatters/NestFormatter'
12
+ import { JsonFormatter } from '#src/Formatters/JsonFormatter'
13
+ import { NoneFormatter } from '#src/Formatters/NoneFormatter'
14
+ import { SimpleFormatter } from '#src/Formatters/SimpleFormatter'
15
+ import { MessageFormatter } from '#src/Formatters/MessageFormatter'
16
+ import { RequestFormatter } from '#src/Formatters/RequestFormatter'
17
+ import { FormatterExistException } from '#src/Exceptions/FormatterExistException'
18
+ import { NotFoundFormatterException } from '#src/Exceptions/NotFoundFormatterException'
19
+
20
+ export class FormatterFactory {
21
+ /**
22
+ * Formatters of FormatterFactory.
23
+ *
24
+ * @type {Map<string, { Formatter: any }>}
25
+ */
26
+ static formatters = new Map()
27
+ .set('cli', { Formatter: CliFormatter })
28
+ .set('nest', { Formatter: NestFormatter })
29
+ .set('json', { Formatter: JsonFormatter })
30
+ .set('none', { Formatter: NoneFormatter })
31
+ .set('simple', { Formatter: SimpleFormatter })
32
+ .set('message', { Formatter: MessageFormatter })
33
+ .set('request', { Formatter: RequestFormatter })
34
+
35
+ /**
36
+ * Return an array with all available formatters.
37
+ *
38
+ * @return {any[]}
39
+ */
40
+ static availableFormatters() {
41
+ const availableFormatters = []
42
+
43
+ for (const [key] of this.formatters.entries()) {
44
+ availableFormatters.push(key)
26
45
  }
27
- static fabricate(formatterName) {
28
- const formatterObject = this.formatters.get(formatterName);
29
- if (!formatterObject) {
30
- throw new NotFoundFormatterException_1.NotFoundFormatterException(formatterName);
31
- }
32
- return new formatterObject.Formatter();
46
+
47
+ return availableFormatters
48
+ }
49
+
50
+ /**
51
+ * Fabricate a new instance of a formatter.
52
+ *
53
+ * @param {string} formatterName
54
+ * @return {any}
55
+ */
56
+ static fabricate(formatterName) {
57
+ const formatterObject = this.formatters.get(formatterName)
58
+
59
+ if (!formatterObject) {
60
+ throw new NotFoundFormatterException(formatterName)
33
61
  }
34
- static createFormatter(name, driver) {
35
- if (this.formatters.has(name)) {
36
- throw new FormatterAlreadyExistException_1.FormatterAlreadyExistException(name);
37
- }
38
- this.formatters.set(name, { Formatter: driver });
62
+
63
+ return new formatterObject.Formatter()
64
+ }
65
+
66
+ /**
67
+ * Creates a new formatter implementation.
68
+ *
69
+ * @param {string} name
70
+ * @param {() => any} formatter
71
+ */
72
+ static createFormatter(name, formatter) {
73
+ if (this.formatters.has(name)) {
74
+ throw new FormatterExistException(name)
39
75
  }
76
+
77
+ this.formatters.set(name, { Formatter: formatter })
78
+ }
40
79
  }
41
- exports.FormatterFactory = FormatterFactory;
42
- FormatterFactory.formatters = new Map()
43
- .set('cli', { Formatter: CliFormatter_1.CliFormatter })
44
- .set('nest', { Formatter: NestFormatter_1.NestFormatter })
45
- .set('json', { Formatter: JsonFormatter_1.JsonFormatter })
46
- .set('simple', { Formatter: SimpleFormatter_1.SimpleFormatter })
47
- .set('request', { Formatter: RequestFormatter_1.RequestFormatter });
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * @athenna/logger
4
3
  *
@@ -7,23 +6,25 @@
7
6
  * For the full copyright and license information, please view the LICENSE
8
7
  * file that was distributed with this source code.
9
8
  */
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
- }
9
+
10
+ import { FactoryHelper } from '#src/index'
11
+
12
+ export class CliFormatter {
13
+ /**
14
+ * Format the message.
15
+ *
16
+ * @param {string} message
17
+ * @param {{ level: 'info'|'INFO'|'debug'|'DEBUG'|'warn'|'WARN'|'error'|'ERROR'|'success'|'SUCCESS', chalk: import('chalk').ChalkInstance }} options
18
+ * @return {string}
19
+ */
20
+ format(message, options) {
21
+ options.level = options.level.toLowerCase()
22
+
23
+ const level = FactoryHelper.paintByLevel(
24
+ options.level,
25
+ `[ ${options.level} ]`,
26
+ )
27
+
28
+ return `${level} ${message}`
29
+ }
28
30
  }
29
- exports.CliFormatter = CliFormatter;
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * @athenna/logger
4
3
  *
@@ -7,13 +6,19 @@
7
6
  * For the full copyright and license information, please view the LICENSE
8
7
  * file that was distributed with this source code.
9
8
  */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.JsonFormatter = void 0;
12
- const Color_1 = require("../Utils/Color");
13
- class JsonFormatter {
14
- format(message, options) {
15
- const pid = Color_1.Color.yellow(`[Athenna] - PID: ${process.pid}`);
16
- return `${pid} - ${Color_1.Color.bold('JSON:')} ${options.chalk(JSON.stringify(message, null, 2))}`;
17
- }
9
+
10
+ import { ColorHelper } from '#src/index'
11
+
12
+ export class JsonFormatter {
13
+ /**
14
+ *
15
+ * @param {Record<any, any>} message
16
+ * @param {{ chalk: import('chalk').ChalkInstance }} options
17
+ * @return {string}
18
+ */
19
+ format(message, options) {
20
+ const jsonSpaced = JSON.stringify(message, null, 2)
21
+
22
+ return `${ColorHelper.bold('JSON:')} ${options.chalk(jsonSpaced)}`
23
+ }
18
24
  }
19
- exports.JsonFormatter = JsonFormatter;
@@ -0,0 +1,40 @@
1
+ /* eslint-disable prettier/prettier */
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
+
11
+ import { FactoryHelper } from '#src/index'
12
+
13
+ export class MessageFormatter {
14
+ /**
15
+ * The last timestamp.
16
+ *
17
+ * @type {number}
18
+ */
19
+ #lastTimestamp
20
+
21
+ /**
22
+ * Format the message.
23
+ *
24
+ * @param {string} message
25
+ * @param {{ level: 'info'|'INFO'|'debug'|'DEBUG'|'warn'|'WARN'|'error'|'ERROR'|'success'|'SUCCESS', customEmoji: string }} options
26
+ * @return {string}
27
+ */
28
+ format(message, options) {
29
+ const timestampDiff = FactoryHelper.getTimestampDiff(this.#lastTimestamp)
30
+
31
+ this.#lastTimestamp = Date.now()
32
+
33
+ const level = FactoryHelper.getEmojiByLevel(
34
+ options.level,
35
+ options.customEmoji,
36
+ )
37
+
38
+ return `${level} ${message}${timestampDiff}`
39
+ }
40
+ }
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * @athenna/logger
4
3
  *
@@ -7,25 +6,35 @@
7
6
  * For the full copyright and license information, please view the LICENSE
8
7
  * file that was distributed with this source code.
9
8
  */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.NestFormatter = void 0;
12
- const Color_1 = require("../Utils/Color");
13
- const getTimestamp_1 = require("../Utils/getTimestamp");
14
- class NestFormatter {
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;
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
- }
9
+
10
+ import { ColorHelper, FactoryHelper } from '#src/index'
11
+
12
+ export class NestFormatter {
13
+ /**
14
+ * The last timestamp.
15
+ *
16
+ * @type {number}
17
+ */
18
+ #lastTimestamp
19
+
20
+ /**
21
+ * Format the message.
22
+ *
23
+ * @param {string} message
24
+ * @param {{ context: string, chalk: import('chalk').ChalkInstance }} options
25
+ * @return {string}
26
+ */
27
+ format(message, options) {
28
+ const timestampDiff = FactoryHelper.getTimestampDiff(this.#lastTimestamp)
29
+
30
+ this.#lastTimestamp = Date.now()
31
+
32
+ const pid = ColorHelper.yellow(`[Athenna] - PID: ${process.pid}`)
33
+ const timestamp = FactoryHelper.getTimestamp()
34
+ const messageCtx = ColorHelper.yellow(`[${options.context}] `)
35
+
36
+ return `${pid} - ${timestamp} ${messageCtx}${options.chalk(
37
+ message,
38
+ )}${timestampDiff}`
39
+ }
30
40
  }
31
- exports.NestFormatter = NestFormatter;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @athenna/logger
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+
10
+ export class NoneFormatter {
11
+ /**
12
+ * Format the message.
13
+ *
14
+ * @param {string} message
15
+ * @param {any} [_options]
16
+ * @return {string}
17
+ */
18
+ format(message, _options) {
19
+ return message
20
+ }
21
+ }
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * @athenna/logger
4
3
  *
@@ -7,41 +6,54 @@
7
6
  * For the full copyright and license information, please view the LICENSE
8
7
  * file that was distributed with this source code.
9
8
  */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.RequestFormatter = void 0;
12
- const Color_1 = require("../Utils/Color");
13
- class RequestFormatter {
14
- format(ctx, options) {
15
- const ip = ctx.request.ip;
16
- const status = ctx.status;
17
- const responseTimeMs = `${Math.round(ctx.responseTime)}ms`;
18
- const methodAndUrl = Color_1.Color[ctx.request.method](`${ctx.request.method}::${ctx.request.baseUrl}`);
19
- if (!options.asJson) {
20
- return `(${ip}) - [${status}] ${methodAndUrl} ${responseTimeMs}`;
21
- }
22
- const metadata = {
23
- method: ctx.request.method,
24
- duration: responseTimeMs,
25
- status: status <= 399 ? 'SUCCESS' : 'ERROR',
26
- statusCode: status,
27
- url: ctx.request.hostUrl,
28
- path: ctx.request.baseUrl,
29
- createdAt: Date.now(),
30
- data: ctx.data,
31
- };
32
- const request = {
33
- url: ctx.request.hostUrl,
34
- method: ctx.request.method,
35
- body: ctx.request.body,
36
- params: ctx.request.params,
37
- queries: ctx.request.queries,
38
- headers: ctx.request.headers,
39
- };
40
- const response = {
41
- body: ctx.body,
42
- headers: ctx.headers,
43
- };
44
- return JSON.stringify({ request, response, metadata });
9
+
10
+ import { ColorHelper } from '#src/index'
11
+
12
+ export class RequestFormatter {
13
+ /**
14
+ * Format the message.
15
+ *
16
+ * @param {any} ctx
17
+ * @param {{ asJson: boolean, chalk: import('chalk').ChalkInstance }} options
18
+ * @return {string}
19
+ */
20
+ format(ctx, options) {
21
+ const ip = ctx.request.ip
22
+ const status = ctx.status
23
+ const responseTimeMs = `${Math.round(ctx.responseTime)}ms`
24
+ const methodAndUrl = ColorHelper[ctx.request.method](
25
+ `${ctx.request.method}::${ctx.request.baseUrl}`,
26
+ )
27
+
28
+ if (!options.asJson) {
29
+ return `(${ip}) - [${status}] ${methodAndUrl} ${responseTimeMs}`
45
30
  }
31
+
32
+ const metadata = {
33
+ method: ctx.request.method,
34
+ duration: responseTimeMs,
35
+ status: status <= 399 ? 'SUCCESS' : 'ERROR',
36
+ statusCode: status,
37
+ url: ctx.request.hostUrl,
38
+ path: ctx.request.baseUrl,
39
+ createdAt: Date.now(),
40
+ data: ctx.data,
41
+ }
42
+
43
+ const request = {
44
+ url: ctx.request.hostUrl,
45
+ method: ctx.request.method,
46
+ body: ctx.request.body,
47
+ params: ctx.request.params,
48
+ queries: ctx.request.queries,
49
+ headers: ctx.request.headers,
50
+ }
51
+
52
+ const response = {
53
+ body: ctx.body,
54
+ headers: ctx.headers,
55
+ }
56
+
57
+ return JSON.stringify({ request, response, metadata })
58
+ }
46
59
  }
47
- exports.RequestFormatter = RequestFormatter;
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * @athenna/logger
4
3
  *
@@ -7,34 +6,34 @@
7
6
  * For the full copyright and license information, please view the LICENSE
8
7
  * file that was distributed with this source code.
9
8
  */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.SimpleFormatter = void 0;
12
- const Color_1 = require("../Utils/Color");
13
- const getTimestamp_1 = require("../Utils/getTimestamp");
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;
22
- }
23
- static paintByLevel(level) {
24
- const levelColors = {
25
- info: Color_1.Color.info,
26
- debug: Color_1.Color.debug,
27
- warn: Color_1.Color.warning,
28
- error: Color_1.Color.error,
29
- success: Color_1.Color.log,
30
- };
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}`;
38
- }
9
+
10
+ import { FactoryHelper } from '#src/index'
11
+
12
+ export class SimpleFormatter {
13
+ /**
14
+ * The last timestamp.
15
+ *
16
+ * @type {number}
17
+ */
18
+ #lastTimestamp
19
+
20
+ /**
21
+ * Format the message.
22
+ *
23
+ * @param {string} message
24
+ * @param {{ level: 'info'|'INFO'|'debug'|'DEBUG'|'warn'|'WARN'|'error'|'ERROR'|'success'|'SUCCESS', chalk: import('chalk').ChalkInstance }} options
25
+ * @return {string}
26
+ */
27
+ format(message, options) {
28
+ const timestampDiff = FactoryHelper.getTimestampDiff(this.#lastTimestamp)
29
+ this.#lastTimestamp = Date.now()
30
+
31
+ const timestamp = FactoryHelper.getTimestamp()
32
+ const level = FactoryHelper.paintByLevel(
33
+ options.level.toLowerCase(),
34
+ `[${options.level.toUpperCase()}]`,
35
+ )
36
+
37
+ return `${level} - ${timestamp} ${options.chalk(message)}${timestampDiff}`
38
+ }
39
39
  }
40
- exports.SimpleFormatter = SimpleFormatter;