@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
@@ -0,0 +1,259 @@
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
+ import chalk from 'chalk'
11
+
12
+ export class ColorHelper {
13
+ /**
14
+ * Chalk instance.
15
+ *
16
+ * @return {import('chalk').ChalkInstance}
17
+ */
18
+ static chalk = chalk
19
+
20
+ /**
21
+ * Paint as bold.
22
+ *
23
+ * @return {import('chalk').ChalkInstance}
24
+ */
25
+ static get bold() {
26
+ return ColorHelper.chalk.bold
27
+ }
28
+
29
+ /**
30
+ * Paint as purple.
31
+ *
32
+ * @return {import('chalk').ChalkInstance}
33
+ */
34
+ static get purple() {
35
+ return ColorHelper.chalk.hex('#7628c8')
36
+ }
37
+
38
+ /**
39
+ * Paint as darkPurple.
40
+ *
41
+ * @return {import('chalk').ChalkInstance}
42
+ */
43
+ static get darkPurple() {
44
+ return ColorHelper.chalk.hex('#501b86')
45
+ }
46
+
47
+ /**
48
+ * Paint as yellow.
49
+ *
50
+ * @return {import('chalk').ChalkInstance}
51
+ */
52
+ static get yellow() {
53
+ return ColorHelper.chalk.hex('#ffe600')
54
+ }
55
+
56
+ /**
57
+ * Paint as cyan.
58
+ *
59
+ * @return {import('chalk').ChalkInstance}
60
+ */
61
+ static get cyan() {
62
+ return ColorHelper.chalk.hex('#00ffff')
63
+ }
64
+
65
+ /**
66
+ * Paint as white.
67
+ *
68
+ * @return {import('chalk').ChalkInstance}
69
+ */
70
+ static get white() {
71
+ return ColorHelper.chalk.hex('#ffffff')
72
+ }
73
+
74
+ /**
75
+ * Paint as orange.
76
+ *
77
+ * @return {import('chalk').ChalkInstance}
78
+ */
79
+ static get orange() {
80
+ return ColorHelper.chalk.hex('#f18b0e')
81
+ }
82
+
83
+ /**
84
+ * Paint as green.
85
+ *
86
+ * @return {import('chalk').ChalkInstance}
87
+ */
88
+ static get green() {
89
+ return ColorHelper.chalk.hex('#0ef12c')
90
+ }
91
+
92
+ /**
93
+ * Paint as darkGreen.
94
+ *
95
+ * @return {import('chalk').ChalkInstance}
96
+ */
97
+ static get darkGreen() {
98
+ return ColorHelper.chalk.hex('#1cb70b')
99
+ }
100
+
101
+ /**
102
+ * Paint as red.
103
+ *
104
+ * @return {import('chalk').ChalkInstance}
105
+ */
106
+ static get red() {
107
+ return ColorHelper.chalk.hex('#f10e0e')
108
+ }
109
+
110
+ /**
111
+ * Paint as darkRed.
112
+ *
113
+ * @return {import('chalk').ChalkInstance}
114
+ */
115
+ static get darkRed() {
116
+ return ColorHelper.chalk.hex('#710909')
117
+ }
118
+
119
+ /**
120
+ * Paint infos.
121
+ *
122
+ * @return {import('chalk').ChalkInstance}
123
+ */
124
+ static get info() {
125
+ return this.cyan.bold
126
+ }
127
+
128
+ /**
129
+ * Paint debugs.
130
+ *
131
+ * @return {import('chalk').ChalkInstance}
132
+ */
133
+ static get debug() {
134
+ return this.purple.bold
135
+ }
136
+
137
+ /**
138
+ * Paint error.
139
+ *
140
+ * @return {import('chalk').ChalkInstance}
141
+ */
142
+ static get error() {
143
+ return this.red.bold
144
+ }
145
+
146
+ /**
147
+ * Paint success.
148
+ *
149
+ * @return {import('chalk').ChalkInstance}
150
+ */
151
+ static get success() {
152
+ return this.green.bold
153
+ }
154
+
155
+ /**
156
+ * Paint critical.
157
+ *
158
+ * @return {import('chalk').ChalkInstance}
159
+ */
160
+ static get critical() {
161
+ return this.darkRed.bold
162
+ }
163
+
164
+ /**
165
+ * Paint warning.
166
+ *
167
+ * @return {import('chalk').ChalkInstance}
168
+ */
169
+ static get warning() {
170
+ return this.orange.bold
171
+ }
172
+
173
+ /**
174
+ * Paint http method.
175
+ *
176
+ * @return {import('chalk').ChalkInstance}
177
+ */
178
+ static get GET() {
179
+ return this.purple.bold
180
+ }
181
+
182
+ /**
183
+ * Paint http method.
184
+ *
185
+ * @return {import('chalk').ChalkInstance}
186
+ */
187
+ static get HEAD() {
188
+ return this.cyan.bold
189
+ }
190
+
191
+ /**
192
+ * Paint http method.
193
+ *
194
+ * @return {import('chalk').ChalkInstance}
195
+ */
196
+ static get PUT() {
197
+ return this.orange.bold
198
+ }
199
+
200
+ /**
201
+ * Paint http method.
202
+ *
203
+ * @return {import('chalk').ChalkInstance}
204
+ */
205
+ static get PATCH() {
206
+ return this.yellow.bold
207
+ }
208
+
209
+ /**
210
+ * Paint http method.
211
+ *
212
+ * @return {import('chalk').ChalkInstance}
213
+ */
214
+ static get POST() {
215
+ return this.green.bold
216
+ }
217
+
218
+ /**
219
+ * Paint http method.
220
+ *
221
+ * @return {import('chalk').ChalkInstance}
222
+ */
223
+ static get DELETE() {
224
+ return this.red.bold
225
+ }
226
+
227
+ /**
228
+ * Paint http method.
229
+ *
230
+ * @return {import('chalk').ChalkInstance}
231
+ */
232
+ static get OPTIONS() {
233
+ return this.cyan.bold
234
+ }
235
+
236
+ /**
237
+ * Remove all colors and special chars of string.
238
+ *
239
+ * @param {string} string
240
+ * @return {import('chalk').ChalkInstance}
241
+ */
242
+ static removeColors(string) {
243
+ return ColorHelper.chalk.reset(string).replace(
244
+ // eslint-disable-next-line no-control-regex
245
+ /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
246
+ '',
247
+ )
248
+ }
249
+
250
+ /**
251
+ * Paint by the http method.
252
+ *
253
+ * @param {'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH', 'OPTIONS', 'HEAD' } method
254
+ * @return {import('chalk').ChalkInstance}
255
+ */
256
+ static httpMethod(method) {
257
+ return this[method]
258
+ }
259
+ }
@@ -0,0 +1,121 @@
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
+ import { Options } from '@secjs/utils'
11
+
12
+ import { ColorHelper } from '#src/Helpers/ColorHelper'
13
+
14
+ export class FactoryHelper {
15
+ /**
16
+ * Get the timestamp value formatted.
17
+ *
18
+ * @return {string}
19
+ */
20
+ static getTimestamp() {
21
+ const localeStringOptions = {
22
+ year: 'numeric',
23
+ hour: 'numeric',
24
+ minute: 'numeric',
25
+ second: 'numeric',
26
+ day: '2-digit',
27
+ month: '2-digit',
28
+ }
29
+
30
+ return new Date(Date.now()).toLocaleString(undefined, localeStringOptions)
31
+ }
32
+
33
+ /**
34
+ * Get the timestamp diff between logs.
35
+ *
36
+ * @param {number} lastTimestamp
37
+ * @return {string}
38
+ */
39
+ static getTimestampDiff(lastTimestamp) {
40
+ let timestampDiff = ''
41
+
42
+ if (lastTimestamp) {
43
+ timestampDiff = ColorHelper.yellow(` +${Date.now() - lastTimestamp}ms`)
44
+ }
45
+
46
+ return timestampDiff
47
+ }
48
+
49
+ /**
50
+ * Get the emoji by level.
51
+ *
52
+ * @param {string} level
53
+ * @param {string} [customEmoji]
54
+ * @return {string}
55
+ */
56
+ static getEmojiByLevel(level, customEmoji) {
57
+ if (customEmoji) {
58
+ return customEmoji
59
+ }
60
+
61
+ const levelEmojis = {
62
+ info: '\u{2139}',
63
+ debug: '\u{1F50E}',
64
+ warn: '\u{26A0}',
65
+ error: '\u{274C}',
66
+ success: '\u{2705}',
67
+ critical: '\u{1F6D1}',
68
+ }
69
+
70
+ if (!levelEmojis[level.toLowerCase()]) {
71
+ return ''
72
+ }
73
+
74
+ return levelEmojis[level.toLowerCase()]
75
+ }
76
+
77
+ /**
78
+ * Paint the message by level.
79
+ *
80
+ * @param {string} level
81
+ * @param {string} message
82
+ * @return {string}
83
+ */
84
+ static paintByLevel(level, message) {
85
+ level = level.toLowerCase()
86
+
87
+ const levelColors = {
88
+ info: ColorHelper.info,
89
+ debug: ColorHelper.debug,
90
+ warn: ColorHelper.warning,
91
+ error: ColorHelper.error,
92
+ success: ColorHelper.success,
93
+ critical: ColorHelper.critical,
94
+ }
95
+
96
+ if (!levelColors[level]) {
97
+ return message
98
+ }
99
+
100
+ return levelColors[level](message)
101
+ }
102
+
103
+ /**
104
+ * Group the configuration values.
105
+ *
106
+ * @param {any} object
107
+ * @param {any} defaultValue
108
+ * @return {any}
109
+ */
110
+ static groupConfigs(object, defaultValue) {
111
+ const formatter = object.formatter || defaultValue.formatter
112
+ const formatterConfig = Options.create(
113
+ object.formatterConfig,
114
+ defaultValue.formatterConfig,
115
+ )
116
+
117
+ const driverConfig = Options.create(object, defaultValue)
118
+
119
+ return { ...driverConfig, formatter, formatterConfig }
120
+ }
121
+ }
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * @athenna/logger
4
3
  *
@@ -7,18 +6,17 @@
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.LoggerProvider = void 0;
12
- const Logger_1 = require("../Logger");
13
- const ioc_1 = require("@athenna/ioc");
14
- class LoggerProvider extends ioc_1.ServiceProvider {
15
- /**
16
- * Register any application services.
17
- *
18
- * @return void
19
- */
20
- register() {
21
- this.container.bind('Athenna/Core/Logger', Logger_1.Logger);
22
- }
9
+
10
+ import { Logger } from '#src/index'
11
+ import { ServiceProvider } from '@athenna/ioc'
12
+
13
+ export class LoggerProvider extends ServiceProvider {
14
+ /**
15
+ * Register any application services.
16
+ *
17
+ * @return void
18
+ */
19
+ register() {
20
+ this.container.bind('Athenna/Core/Logger', Logger)
21
+ }
23
22
  }
24
- exports.LoggerProvider = LoggerProvider;