@athenna/logger 1.1.8 → 1.1.9

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 (77) hide show
  1. package/LICENSE.md +3 -15
  2. package/README.md +6 -6
  3. package/package.json +84 -106
  4. package/src/Drivers/ConsoleDriver.js +43 -17
  5. package/src/Drivers/DebugDriver.js +45 -18
  6. package/src/Drivers/DiscordDriver.js +48 -25
  7. package/src/Drivers/FileDriver.js +51 -29
  8. package/src/Drivers/NullDriver.js +19 -7
  9. package/src/Drivers/PinoDriver.js +83 -61
  10. package/src/Drivers/SlackDriver.js +45 -22
  11. package/src/Drivers/TelegramDriver.js +44 -21
  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 +19 -10
  18. package/src/Facades/Log.js +9 -5
  19. package/src/Factories/DriverFactory.js +98 -56
  20. package/src/Factories/FormatterFactory.js +67 -37
  21. package/src/Formatters/CliFormatter.js +21 -22
  22. package/src/Formatters/JsonFormatter.js +15 -10
  23. package/src/Formatters/MessageFormatter.js +30 -33
  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 -33
  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/DefaultDriverConfigs.d.ts +0 -4
  36. package/src/Contracts/DefaultDriverConfigs.js +0 -2
  37. package/src/Contracts/DriverContract.d.ts +0 -13
  38. package/src/Contracts/DriverContract.js +0 -10
  39. package/src/Contracts/FormatterContract.d.ts +0 -11
  40. package/src/Contracts/FormatterContract.js +0 -10
  41. package/src/Contracts/LevelTypes.d.ts +0 -1
  42. package/src/Contracts/LevelTypes.js +0 -2
  43. package/src/Drivers/ConsoleDriver.d.ts +0 -19
  44. package/src/Drivers/DebugDriver.d.ts +0 -19
  45. package/src/Drivers/DiscordDriver.d.ts +0 -20
  46. package/src/Drivers/FileDriver.d.ts +0 -19
  47. package/src/Drivers/NullDriver.d.ts +0 -13
  48. package/src/Drivers/PinoDriver.d.ts +0 -20
  49. package/src/Drivers/SlackDriver.d.ts +0 -19
  50. package/src/Drivers/TelegramDriver.d.ts +0 -21
  51. package/src/Exceptions/ChannelNotConfiguredException.d.ts +0 -12
  52. package/src/Exceptions/ChannelNotConfiguredException.js +0 -19
  53. package/src/Exceptions/DriverAlreadyExistException.d.ts +0 -12
  54. package/src/Exceptions/DriverAlreadyExistException.js +0 -19
  55. package/src/Exceptions/FormatterAlreadyExistException.d.ts +0 -12
  56. package/src/Exceptions/FormatterAlreadyExistException.js +0 -19
  57. package/src/Exceptions/NotFoundDriverException.d.ts +0 -12
  58. package/src/Exceptions/NotFoundFormatterException.d.ts +0 -12
  59. package/src/Exceptions/OnlyPinoPrettyException.d.ts +0 -12
  60. package/src/Facades/Log.d.ts +0 -10
  61. package/src/Factories/DriverFactory.d.ts +0 -19
  62. package/src/Factories/FormatterFactory.d.ts +0 -18
  63. package/src/Formatters/CliFormatter.d.ts +0 -19
  64. package/src/Formatters/JsonFormatter.d.ts +0 -16
  65. package/src/Formatters/MessageFormatter.d.ts +0 -20
  66. package/src/Formatters/NestFormatter.d.ts +0 -19
  67. package/src/Formatters/RequestFormatter.d.ts +0 -17
  68. package/src/Formatters/SimpleFormatter.d.ts +0 -21
  69. package/src/Logger.d.ts +0 -110
  70. package/src/Logger.js +0 -216
  71. package/src/Providers/LoggerProvider.d.ts +0 -17
  72. package/src/Utils/Color.d.ts +0 -48
  73. package/src/Utils/Color.js +0 -93
  74. package/src/Utils/getTimestamp.d.ts +0 -9
  75. package/src/Utils/getTimestamp.js +0 -23
  76. package/src/Utils/groupConfigs.d.ts +0 -9
  77. package/src/Utils/groupConfigs.js +0 -18
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * @athenna/logger
4
3
  *
@@ -7,63 +6,106 @@
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.DriverFactory = void 0;
12
- const utils_1 = require("@secjs/utils");
13
- const PinoDriver_1 = require("../Drivers/PinoDriver");
14
- const FileDriver_1 = require("../Drivers/FileDriver");
15
- const NullDriver_1 = require("../Drivers/NullDriver");
16
- const DebugDriver_1 = require("../Drivers/DebugDriver");
17
- const SlackDriver_1 = require("../Drivers/SlackDriver");
18
- const ConsoleDriver_1 = require("../Drivers/ConsoleDriver");
19
- const DiscordDriver_1 = require("../Drivers/DiscordDriver");
20
- const TelegramDriver_1 = require("../Drivers/TelegramDriver");
21
- const NotFoundDriverException_1 = require("../Exceptions/NotFoundDriverException");
22
- const DriverAlreadyExistException_1 = require("../Exceptions/DriverAlreadyExistException");
23
- const ChannelNotConfiguredException_1 = require("../Exceptions/ChannelNotConfiguredException");
24
- class DriverFactory {
25
- static availableDrivers() {
26
- const availableDrivers = [];
27
- for (const [key] of this.drivers.entries()) {
28
- availableDrivers.push(key);
29
- }
30
- return availableDrivers;
9
+
10
+ import { Config } from '@secjs/utils'
11
+
12
+ import { FileDriver } from '#src/Drivers/FileDriver'
13
+ import { NullDriver } from '#src/Drivers/NullDriver'
14
+ import { PinoDriver } from '#src/Drivers/PinoDriver'
15
+ import { DebugDriver } from '#src/Drivers/DebugDriver'
16
+ import { SlackDriver } from '#src/Drivers/SlackDriver'
17
+ import { ConsoleDriver } from '#src/Drivers/ConsoleDriver'
18
+ import { DiscordDriver } from '#src/Drivers/DiscordDriver'
19
+ import { TelegramDriver } from '#src/Drivers/TelegramDriver'
20
+ import { DriverExistException } from '#src/Exceptions/DriverExistException'
21
+ import { NotFoundDriverException } from '#src/Exceptions/NotFoundDriverException'
22
+ import { NotFoundChannelException } from '#src/Exceptions/NotFoundChannelException'
23
+
24
+ export class DriverFactory {
25
+ /**
26
+ * Drivers of DriverFactory.
27
+ *
28
+ * @type {Map<string, { Driver: any }>}
29
+ */
30
+ static drivers = new Map()
31
+ .set('file', { Driver: FileDriver })
32
+ .set('null', { Driver: NullDriver })
33
+ .set('pino', { Driver: PinoDriver })
34
+ .set('slack', { Driver: SlackDriver })
35
+ .set('debug', { Driver: DebugDriver })
36
+ .set('console', { Driver: ConsoleDriver })
37
+ .set('discord', { Driver: DiscordDriver })
38
+ .set('telegram', { Driver: TelegramDriver })
39
+
40
+ /**
41
+ * Return an array with all available drivers.
42
+ *
43
+ * @return {any[]}
44
+ */
45
+ static availableDrivers() {
46
+ const availableDrivers = []
47
+
48
+ for (const [key] of this.drivers.entries()) {
49
+ availableDrivers.push(key)
31
50
  }
32
- static fabricate(channelName, runtimeConfig = {}) {
33
- if (channelName === 'default') {
34
- channelName = utils_1.Config.get('logging.default');
35
- }
36
- const channelConfig = this.getChannelConfig(channelName);
37
- const driverObject = this.drivers.get(channelConfig.driver);
38
- if (!driverObject) {
39
- throw new NotFoundDriverException_1.NotFoundDriverException(channelConfig.driver);
40
- }
41
- return new driverObject.Driver(channelName, runtimeConfig);
51
+
52
+ return availableDrivers
53
+ }
54
+
55
+ /**
56
+ * Fabricate a new instance of a driver based on
57
+ * channel configurations.
58
+ *
59
+ * @param {string} channelName
60
+ * @param {any} runtimeConfig
61
+ * @return {any}
62
+ */
63
+ static fabricate(channelName, runtimeConfig = {}) {
64
+ if (channelName === 'default') {
65
+ channelName = Config.get('logging.default')
42
66
  }
43
- static createDriver(name, driver) {
44
- if (this.drivers.has(name)) {
45
- throw new DriverAlreadyExistException_1.DriverAlreadyExistException(name);
46
- }
47
- this.drivers.set(name, { Driver: driver });
67
+
68
+ const channelConfig = this.#getChannelConfig(channelName)
69
+ const driverObject = this.drivers.get(channelConfig.driver)
70
+
71
+ if (!driverObject) {
72
+ throw new NotFoundDriverException(channelConfig.driver)
48
73
  }
49
- static getChannelConfig(channelName) {
50
- const channelConfig = utils_1.Config.get(`logging.channels.${channelName}`);
51
- if (!channelConfig) {
52
- throw new ChannelNotConfiguredException_1.ChannelNotConfiguredException(channelName);
53
- }
54
- if (!this.drivers.has(channelConfig.driver)) {
55
- throw new NotFoundDriverException_1.NotFoundDriverException(channelConfig.driver);
56
- }
57
- return channelConfig;
74
+
75
+ return new driverObject.Driver(channelName, runtimeConfig)
76
+ }
77
+
78
+ /**
79
+ * Creates a new driver implementation.
80
+ *
81
+ * @param {string} name
82
+ * @param {(channel: string, configs?: any) => any} driver
83
+ */
84
+ static createDriver(name, driver) {
85
+ if (this.drivers.has(name)) {
86
+ throw new DriverExistException(name)
58
87
  }
88
+
89
+ this.drivers.set(name, { Driver: driver })
90
+ }
91
+
92
+ /**
93
+ * Get all the configuration of a channel.
94
+ *
95
+ * @param {string} channelName
96
+ * @return {any}
97
+ */
98
+ static #getChannelConfig(channelName) {
99
+ const channelConfig = Config.get(`logging.channels.${channelName}`)
100
+
101
+ if (!channelConfig) {
102
+ throw new NotFoundChannelException(channelName)
103
+ }
104
+
105
+ if (!this.drivers.has(channelConfig.driver)) {
106
+ throw new NotFoundDriverException(channelConfig.driver)
107
+ }
108
+
109
+ return channelConfig
110
+ }
59
111
  }
60
- exports.DriverFactory = DriverFactory;
61
- DriverFactory.drivers = new Map()
62
- .set('file', { Driver: FileDriver_1.FileDriver })
63
- .set('null', { Driver: NullDriver_1.NullDriver })
64
- .set('pino', { Driver: PinoDriver_1.PinoDriver })
65
- .set('slack', { Driver: SlackDriver_1.SlackDriver })
66
- .set('debug', { Driver: DebugDriver_1.DebugDriver })
67
- .set('console', { Driver: ConsoleDriver_1.ConsoleDriver })
68
- .set('discord', { Driver: DiscordDriver_1.DiscordDriver })
69
- .set('telegram', { Driver: TelegramDriver_1.TelegramDriver });
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * @athenna/logger
4
3
  *
@@ -7,43 +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 MessageFormatter_1 = require("../Formatters/MessageFormatter");
17
- const RequestFormatter_1 = require("../Formatters/RequestFormatter");
18
- const NotFoundFormatterException_1 = require("../Exceptions/NotFoundFormatterException");
19
- const FormatterAlreadyExistException_1 = require("../Exceptions/FormatterAlreadyExistException");
20
- class FormatterFactory {
21
- static availableFormatters() {
22
- const availableFormatters = [];
23
- for (const [key] of this.formatters.entries()) {
24
- availableFormatters.push(key);
25
- }
26
- 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)
27
45
  }
28
- static fabricate(formatterName) {
29
- const formatterObject = this.formatters.get(formatterName);
30
- if (!formatterObject) {
31
- throw new NotFoundFormatterException_1.NotFoundFormatterException(formatterName);
32
- }
33
- 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)
34
61
  }
35
- static createFormatter(name, driver) {
36
- if (this.formatters.has(name)) {
37
- throw new FormatterAlreadyExistException_1.FormatterAlreadyExistException(name);
38
- }
39
- 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)
40
75
  }
76
+
77
+ this.formatters.set(name, { Formatter: formatter })
78
+ }
41
79
  }
42
- exports.FormatterFactory = FormatterFactory;
43
- FormatterFactory.formatters = new Map()
44
- .set('cli', { Formatter: CliFormatter_1.CliFormatter })
45
- .set('nest', { Formatter: NestFormatter_1.NestFormatter })
46
- .set('json', { Formatter: JsonFormatter_1.JsonFormatter })
47
- .set('simple', { Formatter: SimpleFormatter_1.SimpleFormatter })
48
- .set('message', { Formatter: MessageFormatter_1.MessageFormatter })
49
- .set('request', { Formatter: RequestFormatter_1.RequestFormatter });
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * @athenna/logger
4
3
  *
@@ -7,25 +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
- if (!levelColors[level.toLowerCase()])
23
- return `[ ${level.toLowerCase()} ]`;
24
- return levelColors[level.toLowerCase()](`[ ${level.toLowerCase()} ]`);
25
- }
26
- format(message, options) {
27
- const level = CliFormatter.paintByLevel(options.level);
28
- return `${level} ${message}`;
29
- }
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
+ }
30
30
  }
31
- 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;
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /* eslint-disable prettier/prettier */
3
2
  /**
4
3
  * @athenna/logger
@@ -8,36 +7,34 @@
8
7
  * For the full copyright and license information, please view the LICENSE
9
8
  * file that was distributed with this source code.
10
9
  */
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.MessageFormatter = void 0;
13
- const Color_1 = require("../Utils/Color");
14
- class MessageFormatter {
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 getEmojiByLevel(level, customEmoji) {
24
- if (customEmoji)
25
- return customEmoji;
26
- const levelEmojis = {
27
- info: '\u{2139}',
28
- debug: '\u{1F50E}',
29
- warn: '\u{26A0}',
30
- error: '\u{274C}',
31
- success: '\u{2705}',
32
- };
33
- if (!levelEmojis[level.toLowerCase()])
34
- return '';
35
- return levelEmojis[level.toLowerCase()];
36
- }
37
- format(message, options) {
38
- const timestampDiff = MessageFormatter.getTimestampDiff();
39
- const level = MessageFormatter.getEmojiByLevel(options.level, options.customEmoji);
40
- return `${level} ${message}${timestampDiff}`;
41
- }
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
+ }
42
40
  }
43
- exports.MessageFormatter = MessageFormatter;
@@ -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;