@athenna/logger 3.0.8 → 3.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.
Files changed (85) hide show
  1. package/build/Drivers/ConsoleDriver.d.ts +12 -0
  2. package/build/Drivers/ConsoleDriver.js +19 -0
  3. package/build/Drivers/DiscordDriver.d.ts +12 -0
  4. package/build/Drivers/DiscordDriver.js +22 -0
  5. package/build/Drivers/Driver.d.ts +52 -0
  6. package/build/Drivers/Driver.js +85 -0
  7. package/build/Drivers/FileDriver.d.ts +12 -0
  8. package/build/Drivers/FileDriver.js +24 -0
  9. package/build/Drivers/NullDriver.d.ts +12 -0
  10. package/build/Drivers/NullDriver.js +14 -0
  11. package/build/Drivers/SlackDriver.d.ts +12 -0
  12. package/build/Drivers/SlackDriver.js +19 -0
  13. package/build/Drivers/StackDriver.d.ts +12 -0
  14. package/build/Drivers/StackDriver.js +19 -0
  15. package/build/Drivers/TelegramDriver.d.ts +12 -0
  16. package/build/Drivers/TelegramDriver.js +21 -0
  17. package/build/Exceptions/DriverExistException.d.ts +12 -0
  18. package/build/Exceptions/DriverExistException.js +21 -0
  19. package/{src/Facades/Log.js → build/Exceptions/FormatterExistException.d.ts} +4 -9
  20. package/build/Exceptions/FormatterExistException.js +21 -0
  21. package/build/Exceptions/NotFoundDriverException.d.ts +12 -0
  22. package/build/Exceptions/NotFoundDriverException.js +21 -0
  23. package/build/Exceptions/NotFoundFormatterException.d.ts +12 -0
  24. package/build/Exceptions/NotFoundFormatterException.js +21 -0
  25. package/build/Exceptions/NotImplementedConfigException.d.ts +12 -0
  26. package/build/Exceptions/NotImplementedConfigException.js +28 -0
  27. package/build/Facades/Log.d.ts +10 -0
  28. package/build/Facades/Log.js +10 -0
  29. package/build/Factories/DriverFactory.d.ts +39 -0
  30. package/build/Factories/DriverFactory.js +93 -0
  31. package/build/Factories/FormatterFactory.d.ts +29 -0
  32. package/build/Factories/FormatterFactory.js +57 -0
  33. package/build/Formatters/CliFormatter.d.ts +12 -0
  34. package/build/Formatters/CliFormatter.js +15 -0
  35. package/build/Formatters/Formatter.d.ts +85 -0
  36. package/build/Formatters/Formatter.js +177 -0
  37. package/build/Formatters/JsonFormatter.d.ts +12 -0
  38. package/build/Formatters/JsonFormatter.js +26 -0
  39. package/build/Formatters/MessageFormatter.d.ts +12 -0
  40. package/build/Formatters/MessageFormatter.js +14 -0
  41. package/build/Formatters/NoneFormatter.d.ts +12 -0
  42. package/{src → build}/Formatters/NoneFormatter.js +4 -12
  43. package/build/Formatters/RequestFormatter.d.ts +12 -0
  44. package/build/Formatters/RequestFormatter.js +45 -0
  45. package/build/Formatters/SimpleFormatter.d.ts +12 -0
  46. package/build/Formatters/SimpleFormatter.js +18 -0
  47. package/build/Helpers/ColorHelper.d.ts +131 -0
  48. package/build/Helpers/ColorHelper.js +215 -0
  49. package/build/Helpers/FactoryHelper.d.ts +14 -0
  50. package/build/Helpers/FactoryHelper.js +20 -0
  51. package/build/Logger/Logger.d.ts +68 -0
  52. package/build/Logger/Logger.js +105 -0
  53. package/build/Logger/VanillaLogger.d.ts +60 -0
  54. package/build/Logger/VanillaLogger.js +86 -0
  55. package/build/Providers/LoggerProvider.d.ts +12 -0
  56. package/{src → build}/Providers/LoggerProvider.js +5 -12
  57. package/build/index.d.ts +18 -0
  58. package/build/index.js +18 -0
  59. package/package.json +81 -59
  60. package/src/Drivers/ConsoleDriver.js +0 -40
  61. package/src/Drivers/DiscordDriver.js +0 -43
  62. package/src/Drivers/Driver.js +0 -134
  63. package/src/Drivers/FileDriver.js +0 -47
  64. package/src/Drivers/NullDriver.js +0 -33
  65. package/src/Drivers/SlackDriver.js +0 -40
  66. package/src/Drivers/StackDriver.js +0 -45
  67. package/src/Drivers/TelegramDriver.js +0 -46
  68. package/src/Exceptions/DriverExistException.js +0 -31
  69. package/src/Exceptions/FormatterExistException.js +0 -32
  70. package/src/Exceptions/NotFoundDriverException.js +0 -31
  71. package/src/Exceptions/NotFoundFormatterException.js +0 -31
  72. package/src/Exceptions/NotImplementedConfigException.js +0 -37
  73. package/src/Factories/DriverFactory.js +0 -128
  74. package/src/Factories/FormatterFactory.js +0 -77
  75. package/src/Formatters/CliFormatter.js +0 -24
  76. package/src/Formatters/Formatter.js +0 -266
  77. package/src/Formatters/JsonFormatter.js +0 -37
  78. package/src/Formatters/MessageFormatter.js +0 -26
  79. package/src/Formatters/RequestFormatter.js +0 -64
  80. package/src/Formatters/SimpleFormatter.js +0 -27
  81. package/src/Helpers/ColorHelper.js +0 -322
  82. package/src/Helpers/FactoryHelper.js +0 -31
  83. package/src/Helpers/VanillaLogger.js +0 -134
  84. package/src/index.d.ts +0 -797
  85. package/src/index.js +0 -180
@@ -0,0 +1,93 @@
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
+ import { Config } from '@athenna/config';
10
+ import { Options } from '@athenna/common';
11
+ import { FileDriver } from '#src/Drivers/FileDriver';
12
+ import { NullDriver } from '#src/Drivers/NullDriver';
13
+ import { SlackDriver } from '#src/Drivers/SlackDriver';
14
+ import { StackDriver } from '#src/Drivers/StackDriver';
15
+ import { ConsoleDriver } from '#src/Drivers/ConsoleDriver';
16
+ import { DiscordDriver } from '#src/Drivers/DiscordDriver';
17
+ import { FactoryHelper } from '#src/Helpers/FactoryHelper';
18
+ import { TelegramDriver } from '#src/Drivers/TelegramDriver';
19
+ import { DriverExistException } from '#src/Exceptions/DriverExistException';
20
+ import { NotFoundDriverException } from '#src/Exceptions/NotFoundDriverException';
21
+ import { NotImplementedConfigException } from '#src/Exceptions/NotImplementedConfigException';
22
+ export class DriverFactory {
23
+ /**
24
+ * Drivers of DriverFactory.
25
+ */
26
+ static drivers = new Map()
27
+ .set('file', { Driver: FileDriver })
28
+ .set('null', { Driver: NullDriver })
29
+ .set('slack', { Driver: SlackDriver })
30
+ .set('stack', { Driver: StackDriver })
31
+ .set('console', { Driver: ConsoleDriver })
32
+ .set('discord', { Driver: DiscordDriver })
33
+ .set('telegram', { Driver: TelegramDriver });
34
+ /**
35
+ * Return an array with all available drivers.
36
+ */
37
+ static availableDrivers() {
38
+ const availableDrivers = [];
39
+ for (const key of this.drivers.keys()) {
40
+ availableDrivers.push(key);
41
+ }
42
+ return availableDrivers;
43
+ }
44
+ /**
45
+ * Fabricate a new instance of a driver based on
46
+ * channel configurations.
47
+ */
48
+ static fabricate(channelName, configs = {}) {
49
+ const channelConfig = this.getChannelConfig(channelName);
50
+ const { Driver } = this.drivers.get(channelConfig.driver);
51
+ return new Driver(FactoryHelper.groupConfigs(configs, channelConfig));
52
+ }
53
+ /**
54
+ * Fabricate a new instance of a driver with vanilla
55
+ * configurations.
56
+ */
57
+ static fabricateVanilla(configs = {}) {
58
+ configs = Options.create(configs, {
59
+ driver: 'console',
60
+ formatter: 'none',
61
+ });
62
+ if (!this.drivers.has(configs.driver)) {
63
+ throw new NotFoundDriverException(configs.driver);
64
+ }
65
+ const { Driver } = this.drivers.get(configs.driver);
66
+ return new Driver(configs);
67
+ }
68
+ /**
69
+ * Creates a new driver implementation.
70
+ */
71
+ static createDriver(name, driver) {
72
+ if (this.drivers.has(name)) {
73
+ throw new DriverExistException(name);
74
+ }
75
+ this.drivers.set(name, { Driver: driver });
76
+ }
77
+ /**
78
+ * Get all the configuration of a channel.
79
+ */
80
+ static getChannelConfig(channelName) {
81
+ if (channelName === 'default') {
82
+ channelName = Config.get('logging.default') || channelName;
83
+ }
84
+ const channelConfig = Config.get(`logging.channels.${channelName}`);
85
+ if (!channelConfig) {
86
+ throw new NotImplementedConfigException(channelName);
87
+ }
88
+ if (!this.drivers.has(channelConfig.driver)) {
89
+ throw new NotFoundDriverException(channelConfig.driver);
90
+ }
91
+ return channelConfig;
92
+ }
93
+ }
@@ -0,0 +1,29 @@
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
+ import { Formatter } from '#src/Formatters/Formatter';
10
+ export declare class FormatterFactory {
11
+ /**
12
+ * Formatters of FormatterFactory.
13
+ */
14
+ static formatters: Map<string, {
15
+ Formatter: any;
16
+ }>;
17
+ /**
18
+ * Return an array with all available formatters.
19
+ */
20
+ static availableFormatters(): string[];
21
+ /**
22
+ * Fabricate a new instance of a formatter.
23
+ */
24
+ static fabricate(formatterName: string): Formatter;
25
+ /**
26
+ * Creates a new formatter implementation.
27
+ */
28
+ static createFormatter(name: string, formatter: typeof Formatter): void;
29
+ }
@@ -0,0 +1,57 @@
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
+ import { CliFormatter } from '#src/Formatters/CliFormatter';
10
+ import { JsonFormatter } from '#src/Formatters/JsonFormatter';
11
+ import { NoneFormatter } from '#src/Formatters/NoneFormatter';
12
+ import { SimpleFormatter } from '#src/Formatters/SimpleFormatter';
13
+ import { MessageFormatter } from '#src/Formatters/MessageFormatter';
14
+ import { RequestFormatter } from '#src/Formatters/RequestFormatter';
15
+ import { FormatterExistException } from '#src/Exceptions/FormatterExistException';
16
+ import { NotFoundFormatterException } from '#src/Exceptions/NotFoundFormatterException';
17
+ export class FormatterFactory {
18
+ /**
19
+ * Formatters of FormatterFactory.
20
+ */
21
+ static formatters = new Map()
22
+ .set('cli', { Formatter: CliFormatter })
23
+ .set('json', { Formatter: JsonFormatter })
24
+ .set('none', { Formatter: NoneFormatter })
25
+ .set('simple', { Formatter: SimpleFormatter })
26
+ .set('message', { Formatter: MessageFormatter })
27
+ .set('request', { Formatter: RequestFormatter });
28
+ /**
29
+ * Return an array with all available formatters.
30
+ */
31
+ static availableFormatters() {
32
+ const availableFormatters = [];
33
+ for (const [key] of this.formatters.entries()) {
34
+ availableFormatters.push(key);
35
+ }
36
+ return availableFormatters;
37
+ }
38
+ /**
39
+ * Fabricate a new instance of a formatter.
40
+ */
41
+ static fabricate(formatterName) {
42
+ const formatterObject = this.formatters.get(formatterName);
43
+ if (!formatterObject) {
44
+ throw new NotFoundFormatterException(formatterName);
45
+ }
46
+ return new formatterObject.Formatter();
47
+ }
48
+ /**
49
+ * Creates a new formatter implementation.
50
+ */
51
+ static createFormatter(name, formatter) {
52
+ if (this.formatters.has(name)) {
53
+ throw new FormatterExistException(name);
54
+ }
55
+ this.formatters.set(name, { Formatter: formatter });
56
+ }
57
+ }
@@ -0,0 +1,12 @@
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
+ import { Formatter } from '#src/Formatters/Formatter';
10
+ export declare class CliFormatter extends Formatter {
11
+ format(message: string): string;
12
+ }
@@ -0,0 +1,15 @@
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
+ import { Formatter } from '#src/Formatters/Formatter';
10
+ export class CliFormatter extends Formatter {
11
+ format(message) {
12
+ const level = this.cliLevel();
13
+ return this.clean(`${level} ${this.toString(message)}`);
14
+ }
15
+ }
@@ -0,0 +1,85 @@
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
+ export declare abstract class Formatter {
10
+ /**
11
+ * Holds the configuration object of formatter.
12
+ */
13
+ configs: any;
14
+ /**
15
+ * Creates a new instance of Formatter.
16
+ */
17
+ config(configs: any): Formatter;
18
+ /**
19
+ * Format the message.
20
+ */
21
+ abstract format(message: any): string;
22
+ /**
23
+ * Create the PID for formatter.
24
+ */
25
+ pid(): string;
26
+ /**
27
+ * Create the hostname for formatter.
28
+ */
29
+ hostname(): string;
30
+ /**
31
+ * Get the level without any color or format.
32
+ */
33
+ level(): string;
34
+ /**
35
+ * Get the trace id for formatter.
36
+ */
37
+ traceId(): string | null;
38
+ /**
39
+ * Create the timestamp for formatter.
40
+ */
41
+ timestamp(): string;
42
+ /**
43
+ * Transform the message to string.
44
+ */
45
+ toString(message: string): string;
46
+ /**
47
+ * Clean the message removing colors if clean
48
+ * option is true. If force is true, then colors
49
+ * will be removed even if configs clean option
50
+ * is false.
51
+ */
52
+ clean(message: string, force?: boolean): string;
53
+ /**
54
+ * Apply all colors necessary to message.
55
+ */
56
+ applyColors(message: string): string;
57
+ /**
58
+ * Apply colors in message.
59
+ */
60
+ applyColorsByChalk(message: string): string;
61
+ /**
62
+ * Apply colors in message by level.
63
+ */
64
+ applyColorsByLevel(message: string): string;
65
+ /**
66
+ * Create the cli level string.
67
+ */
68
+ cliLevel(): string;
69
+ /**
70
+ * Create the simple level string.
71
+ */
72
+ simpleLevel(): string;
73
+ /**
74
+ * Create the message level emoji string.
75
+ */
76
+ messageLevel(): string;
77
+ /**
78
+ * Get the emoji by level.
79
+ */
80
+ getEmojiByLevel(level: string, customEmoji?: string): string;
81
+ /**
82
+ * Paint the message by level.
83
+ */
84
+ paintMessageByLevel(level: string, message: string): string;
85
+ }
@@ -0,0 +1,177 @@
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
+ import rTracer from 'cls-rtracer';
10
+ import { hostname } from 'node:os';
11
+ import { Is } from '@athenna/common';
12
+ import { ColorHelper } from '#src/Helpers/ColorHelper';
13
+ export class Formatter {
14
+ /**
15
+ * Holds the configuration object of formatter.
16
+ */
17
+ configs = {};
18
+ /**
19
+ * Creates a new instance of Formatter.
20
+ */
21
+ config(configs) {
22
+ this.configs = configs;
23
+ return this;
24
+ }
25
+ /**
26
+ * Create the PID for formatter.
27
+ */
28
+ pid() {
29
+ return process.pid.toString();
30
+ }
31
+ /**
32
+ * Create the hostname for formatter.
33
+ */
34
+ hostname() {
35
+ return hostname();
36
+ }
37
+ /**
38
+ * Get the level without any color or format.
39
+ */
40
+ level() {
41
+ return this.configs.level;
42
+ }
43
+ /**
44
+ * Get the trace id for formatter.
45
+ */
46
+ traceId() {
47
+ return (rTracer.id() || null);
48
+ }
49
+ /**
50
+ * Create the timestamp for formatter.
51
+ */
52
+ timestamp() {
53
+ const localeStringOptions = {
54
+ year: 'numeric',
55
+ hour: 'numeric',
56
+ minute: 'numeric',
57
+ second: 'numeric',
58
+ day: '2-digit',
59
+ month: '2-digit',
60
+ };
61
+ return new Date(Date.now()).toLocaleString(undefined, localeStringOptions);
62
+ }
63
+ /**
64
+ * Transform the message to string.
65
+ */
66
+ toString(message) {
67
+ if (Is.String(message)) {
68
+ return message;
69
+ }
70
+ if (Is.Object(message)) {
71
+ message = JSON.stringify(message);
72
+ }
73
+ return `${message}`;
74
+ }
75
+ /**
76
+ * Clean the message removing colors if clean
77
+ * option is true. If force is true, then colors
78
+ * will be removed even if configs clean option
79
+ * is false.
80
+ */
81
+ clean(message, force = false) {
82
+ if (this.configs.clean || force) {
83
+ return ColorHelper.removeColors(message);
84
+ }
85
+ return message;
86
+ }
87
+ /**
88
+ * Apply all colors necessary to message.
89
+ */
90
+ applyColors(message) {
91
+ message = this.toString(message);
92
+ return this.applyColorsByChalk(this.applyColorsByLevel(message));
93
+ }
94
+ /**
95
+ * Apply colors in message.
96
+ */
97
+ applyColorsByChalk(message) {
98
+ if (!this.configs.chalk) {
99
+ return message;
100
+ }
101
+ return this.configs.chalk(message);
102
+ }
103
+ /**
104
+ * Apply colors in message by level.
105
+ */
106
+ applyColorsByLevel(message) {
107
+ const level = this.configs.level;
108
+ return this.paintMessageByLevel(level, message);
109
+ }
110
+ /**
111
+ * Create the cli level string.
112
+ */
113
+ cliLevel() {
114
+ const level = this.configs.level;
115
+ if (!ColorHelper[level]) {
116
+ return level;
117
+ }
118
+ return ColorHelper[level].bold(`[ ${level} ]`);
119
+ }
120
+ /**
121
+ * Create the simple level string.
122
+ */
123
+ simpleLevel() {
124
+ const level = this.configs.level;
125
+ if (!ColorHelper[level]) {
126
+ return level;
127
+ }
128
+ return ColorHelper[level].bold(`[${level.toUpperCase()}]`);
129
+ }
130
+ /**
131
+ * Create the message level emoji string.
132
+ */
133
+ messageLevel() {
134
+ const level = this.configs.level;
135
+ return this.getEmojiByLevel(level, this.configs.customEmoji);
136
+ }
137
+ /**
138
+ * Get the emoji by level.
139
+ */
140
+ getEmojiByLevel(level, customEmoji) {
141
+ if (customEmoji) {
142
+ return customEmoji;
143
+ }
144
+ const levelEmojis = {
145
+ trace: '\u{1F43E}',
146
+ debug: '\u{1F50E}',
147
+ info: '\u{2139}',
148
+ success: '\u{2705}',
149
+ warn: '\u{26A0}',
150
+ error: '\u{274C}',
151
+ fatal: '\u{1F6D1}',
152
+ };
153
+ if (!levelEmojis[level.toLowerCase()]) {
154
+ return '';
155
+ }
156
+ return levelEmojis[level.toLowerCase()];
157
+ }
158
+ /**
159
+ * Paint the message by level.
160
+ */
161
+ paintMessageByLevel(level, message) {
162
+ const levelLower = level.toLowerCase();
163
+ const levelColors = {
164
+ trace: ColorHelper.trace,
165
+ debug: ColorHelper.debug,
166
+ info: ColorHelper.info,
167
+ success: ColorHelper.success,
168
+ warn: ColorHelper.warn,
169
+ error: ColorHelper.error,
170
+ fatal: ColorHelper.fatal,
171
+ };
172
+ if (!levelColors[levelLower]) {
173
+ return message;
174
+ }
175
+ return levelColors[levelLower](message);
176
+ }
177
+ }
@@ -0,0 +1,12 @@
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
+ import { Formatter } from '#src/Formatters/Formatter';
10
+ export declare class JsonFormatter extends Formatter {
11
+ format(message: any): string;
12
+ }
@@ -0,0 +1,26 @@
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
+ import { Is } from '@athenna/common';
10
+ import { Formatter } from '#src/Formatters/Formatter';
11
+ export class JsonFormatter extends Formatter {
12
+ format(message) {
13
+ const base = {
14
+ level: this.level(),
15
+ time: Date.now(),
16
+ pid: this.pid(),
17
+ hostname: this.hostname(),
18
+ traceId: this.traceId(),
19
+ };
20
+ if (Is.String(message)) {
21
+ base.msg = message;
22
+ return JSON.stringify(base);
23
+ }
24
+ return JSON.stringify({ ...base, ...message });
25
+ }
26
+ }
@@ -0,0 +1,12 @@
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
+ import { Formatter } from '#src/Formatters/Formatter';
10
+ export declare class MessageFormatter extends Formatter {
11
+ format(message: string): string;
12
+ }
@@ -0,0 +1,14 @@
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
+ import { Formatter } from '#src/Formatters/Formatter';
10
+ export class MessageFormatter extends Formatter {
11
+ format(message) {
12
+ return this.clean(`${this.messageLevel()} - (${this.pid()}) - (${this.hostname()}): ${this.toString(message)}`);
13
+ }
14
+ }
@@ -0,0 +1,12 @@
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
+ import { Formatter } from '#src/Formatters/Formatter';
10
+ export declare class NoneFormatter extends Formatter {
11
+ format(message: string): string;
12
+ }
@@ -6,17 +6,9 @@
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
-
10
- import { Formatter } from '#src/Formatters/Formatter'
11
-
9
+ import { Formatter } from '#src/Formatters/Formatter';
12
10
  export class NoneFormatter extends Formatter {
13
- /**
14
- * Format the message.
15
- *
16
- * @param {string} message
17
- * @return {string}
18
- */
19
- format(message) {
20
- return this.clean(this.toString(message))
21
- }
11
+ format(message) {
12
+ return this.clean(this.toString(message));
13
+ }
22
14
  }
@@ -0,0 +1,12 @@
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
+ import { Formatter } from '#src/Formatters/Formatter';
10
+ export declare class RequestFormatter extends Formatter {
11
+ format(ctx: any): string;
12
+ }
@@ -0,0 +1,45 @@
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
+ import { ColorHelper } from '#src/index';
10
+ import { Formatter } from '#src/Formatters/Formatter';
11
+ export class RequestFormatter extends Formatter {
12
+ format(ctx) {
13
+ const ip = ctx.request.ip;
14
+ const status = ctx.status;
15
+ const responseTimeMs = `${Math.round(ctx.responseTime)}ms`;
16
+ const methodAndStatus = ColorHelper[ctx.request.method](`[${ctx.request.method}::${ctx.status}]`);
17
+ if (!this.configs.asJson) {
18
+ return this.clean(`${methodAndStatus} - [${ip}] - ${new Date().toISOString()} - ${ctx.request.baseUrl} ${responseTimeMs}`);
19
+ }
20
+ const metadata = {
21
+ method: ctx.request.method,
22
+ duration: responseTimeMs,
23
+ status: status <= 399 ? 'SUCCESS' : 'ERROR',
24
+ statusCode: status,
25
+ url: ctx.request.hostUrl,
26
+ path: ctx.request.baseUrl,
27
+ createdAt: Date.now(),
28
+ traceId: this.traceId(),
29
+ data: ctx.data,
30
+ };
31
+ const request = {
32
+ url: ctx.request.hostUrl,
33
+ method: ctx.request.method,
34
+ body: ctx.request.body,
35
+ params: ctx.request.params,
36
+ queries: ctx.request.queries,
37
+ headers: ctx.request.headers,
38
+ };
39
+ const response = {
40
+ body: ctx.body,
41
+ headers: ctx.headers,
42
+ };
43
+ return JSON.stringify({ request, response, metadata });
44
+ }
45
+ }
@@ -0,0 +1,12 @@
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
+ import { Formatter } from '#src/Formatters/Formatter';
10
+ export declare class SimpleFormatter extends Formatter {
11
+ format(message: string): string;
12
+ }
@@ -0,0 +1,18 @@
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
+ import { Formatter } from '#src/Formatters/Formatter';
10
+ export class SimpleFormatter extends Formatter {
11
+ format(message) {
12
+ const pid = this.pid();
13
+ const time = this.timestamp();
14
+ const level = this.simpleLevel();
15
+ const colorizedMsg = this.applyColors(message);
16
+ return this.clean(`${level} - ${time} - (${pid}) ${colorizedMsg}`);
17
+ }
18
+ }