@athenna/logger 1.1.8 → 1.2.1
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/LICENSE.md +3 -15
- package/README.md +6 -6
- package/package.json +87 -106
- package/src/Drivers/ConsoleDriver.js +43 -17
- package/src/Drivers/DebugDriver.js +45 -18
- package/src/Drivers/DiscordDriver.js +48 -25
- package/src/Drivers/FileDriver.js +51 -29
- package/src/Drivers/NullDriver.js +19 -7
- package/src/Drivers/PinoDriver.js +83 -61
- package/src/Drivers/SlackDriver.js +45 -22
- package/src/Drivers/TelegramDriver.js +44 -21
- package/src/Exceptions/DriverExistException.js +31 -0
- package/src/Exceptions/FormatterExistException.js +32 -0
- package/src/Exceptions/NotFoundChannelException.js +32 -0
- package/src/Exceptions/NotFoundDriverException.js +22 -10
- package/src/Exceptions/NotFoundFormatterException.js +22 -10
- package/src/Exceptions/OnlyPinoPrettyException.js +19 -10
- package/src/Facades/Log.js +9 -5
- package/src/Factories/DriverFactory.js +98 -56
- package/src/Factories/FormatterFactory.js +67 -37
- package/src/Formatters/CliFormatter.js +21 -22
- package/src/Formatters/JsonFormatter.js +15 -10
- package/src/Formatters/MessageFormatter.js +30 -33
- package/src/Formatters/NestFormatter.js +31 -22
- package/src/Formatters/NoneFormatter.js +21 -0
- package/src/Formatters/RequestFormatter.js +49 -37
- package/src/Formatters/SimpleFormatter.js +30 -33
- package/src/Helpers/ColorHelper.js +259 -0
- package/src/Helpers/FactoryHelper.js +121 -0
- package/src/Providers/LoggerProvider.js +13 -15
- package/src/index.d.ts +383 -0
- package/src/index.js +269 -0
- package/index.d.ts +0 -13
- package/index.js +0 -25
- package/src/Contracts/DefaultDriverConfigs.d.ts +0 -4
- package/src/Contracts/DefaultDriverConfigs.js +0 -2
- package/src/Contracts/DriverContract.d.ts +0 -13
- package/src/Contracts/DriverContract.js +0 -10
- package/src/Contracts/FormatterContract.d.ts +0 -11
- package/src/Contracts/FormatterContract.js +0 -10
- package/src/Contracts/LevelTypes.d.ts +0 -1
- package/src/Contracts/LevelTypes.js +0 -2
- package/src/Drivers/ConsoleDriver.d.ts +0 -19
- package/src/Drivers/DebugDriver.d.ts +0 -19
- package/src/Drivers/DiscordDriver.d.ts +0 -20
- package/src/Drivers/FileDriver.d.ts +0 -19
- package/src/Drivers/NullDriver.d.ts +0 -13
- package/src/Drivers/PinoDriver.d.ts +0 -20
- package/src/Drivers/SlackDriver.d.ts +0 -19
- package/src/Drivers/TelegramDriver.d.ts +0 -21
- package/src/Exceptions/ChannelNotConfiguredException.d.ts +0 -12
- package/src/Exceptions/ChannelNotConfiguredException.js +0 -19
- package/src/Exceptions/DriverAlreadyExistException.d.ts +0 -12
- package/src/Exceptions/DriverAlreadyExistException.js +0 -19
- package/src/Exceptions/FormatterAlreadyExistException.d.ts +0 -12
- package/src/Exceptions/FormatterAlreadyExistException.js +0 -19
- package/src/Exceptions/NotFoundDriverException.d.ts +0 -12
- package/src/Exceptions/NotFoundFormatterException.d.ts +0 -12
- package/src/Exceptions/OnlyPinoPrettyException.d.ts +0 -12
- package/src/Facades/Log.d.ts +0 -10
- package/src/Factories/DriverFactory.d.ts +0 -19
- package/src/Factories/FormatterFactory.d.ts +0 -18
- package/src/Formatters/CliFormatter.d.ts +0 -19
- package/src/Formatters/JsonFormatter.d.ts +0 -16
- package/src/Formatters/MessageFormatter.d.ts +0 -20
- package/src/Formatters/NestFormatter.d.ts +0 -19
- package/src/Formatters/RequestFormatter.d.ts +0 -17
- package/src/Formatters/SimpleFormatter.d.ts +0 -21
- package/src/Logger.d.ts +0 -110
- package/src/Logger.js +0 -216
- package/src/Providers/LoggerProvider.d.ts +0 -17
- package/src/Utils/Color.d.ts +0 -48
- package/src/Utils/Color.js +0 -93
- package/src/Utils/getTimestamp.d.ts +0 -9
- package/src/Utils/getTimestamp.js +0 -23
- package/src/Utils/groupConfigs.d.ts +0 -9
- package/src/Utils/groupConfigs.js +0 -18
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import { Facade } from '@athenna/ioc'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Log facade.
|
|
5
|
+
*
|
|
6
|
+
* @type {Facade & import('../index').Logger}
|
|
7
|
+
*/
|
|
8
|
+
export const Log: Facade & Logger
|
|
9
|
+
|
|
10
|
+
export class ColorHelper {
|
|
11
|
+
/**
|
|
12
|
+
* Chalk instance.
|
|
13
|
+
*
|
|
14
|
+
* @return {import('chalk').ChalkInstance}
|
|
15
|
+
*/
|
|
16
|
+
static chalk: import('chalk').ChalkInstance
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Paint as bold.
|
|
20
|
+
*
|
|
21
|
+
* @return {import('chalk').ChalkInstance}
|
|
22
|
+
*/
|
|
23
|
+
static get bold(): import('chalk').ChalkInstance
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Paint as purple.
|
|
27
|
+
*
|
|
28
|
+
* @return {import('chalk').ChalkInstance}
|
|
29
|
+
*/
|
|
30
|
+
static get purple(): import('chalk').ChalkInstance
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Paint as darkPurple.
|
|
34
|
+
*
|
|
35
|
+
* @return {import('chalk').ChalkInstance}
|
|
36
|
+
*/
|
|
37
|
+
static get darkPurple(): import('chalk').ChalkInstance
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Paint as yellow.
|
|
41
|
+
*
|
|
42
|
+
* @return {import('chalk').ChalkInstance}
|
|
43
|
+
*/
|
|
44
|
+
static get yellow(): import('chalk').ChalkInstance
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Paint as cyan.
|
|
48
|
+
*
|
|
49
|
+
* @return {import('chalk').ChalkInstance}
|
|
50
|
+
*/
|
|
51
|
+
static get cyan(): import('chalk').ChalkInstance
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Paint as white.
|
|
55
|
+
*
|
|
56
|
+
* @return {import('chalk').ChalkInstance}
|
|
57
|
+
*/
|
|
58
|
+
static get white(): import('chalk').ChalkInstance
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Paint as orange.
|
|
62
|
+
*
|
|
63
|
+
* @return {import('chalk').ChalkInstance}
|
|
64
|
+
*/
|
|
65
|
+
static get orange(): import('chalk').ChalkInstance
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Paint as green.
|
|
69
|
+
*
|
|
70
|
+
* @return {import('chalk').ChalkInstance}
|
|
71
|
+
*/
|
|
72
|
+
static get green(): import('chalk').ChalkInstance
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Paint as darkGreen.
|
|
76
|
+
*
|
|
77
|
+
* @return {import('chalk').ChalkInstance}
|
|
78
|
+
*/
|
|
79
|
+
static get darkGreen(): import('chalk').ChalkInstance
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Paint as red.
|
|
83
|
+
*
|
|
84
|
+
* @return {import('chalk').ChalkInstance}
|
|
85
|
+
*/
|
|
86
|
+
static get red(): import('chalk').ChalkInstance
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Paint infos.
|
|
90
|
+
*
|
|
91
|
+
* @return {import('chalk').ChalkInstance}
|
|
92
|
+
*/
|
|
93
|
+
static get info(): import('chalk').ChalkInstance
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Paint logs.
|
|
97
|
+
*
|
|
98
|
+
* @return {import('chalk').ChalkInstance}
|
|
99
|
+
*/
|
|
100
|
+
static get log(): import('chalk').ChalkInstance
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Paint debugs.
|
|
104
|
+
*
|
|
105
|
+
* @return {import('chalk').ChalkInstance}
|
|
106
|
+
*/
|
|
107
|
+
static get debug(): import('chalk').ChalkInstance
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Paint errors.
|
|
111
|
+
*
|
|
112
|
+
* @return {import('chalk').ChalkInstance}
|
|
113
|
+
*/
|
|
114
|
+
static get error(): import('chalk').ChalkInstance
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Paint warnings.
|
|
118
|
+
*
|
|
119
|
+
* @return {import('chalk').ChalkInstance}
|
|
120
|
+
*/
|
|
121
|
+
static get warning(): import('chalk').ChalkInstance
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Paint http method.
|
|
125
|
+
*
|
|
126
|
+
* @return {import('chalk').ChalkInstance}
|
|
127
|
+
*/
|
|
128
|
+
static get GET(): import('chalk').ChalkInstance
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Paint http method.
|
|
132
|
+
*
|
|
133
|
+
* @return {import('chalk').ChalkInstance}
|
|
134
|
+
*/
|
|
135
|
+
static get HEAD(): import('chalk').ChalkInstance
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Paint http method.
|
|
139
|
+
*
|
|
140
|
+
* @return {import('chalk').ChalkInstance}
|
|
141
|
+
*/
|
|
142
|
+
static get PUT(): import('chalk').ChalkInstance
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Paint http method.
|
|
146
|
+
*
|
|
147
|
+
* @return {import('chalk').ChalkInstance}
|
|
148
|
+
*/
|
|
149
|
+
static get PATCH(): import('chalk').ChalkInstance
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Paint http method.
|
|
153
|
+
*
|
|
154
|
+
* @return {import('chalk').ChalkInstance}
|
|
155
|
+
*/
|
|
156
|
+
static get POST(): import('chalk').ChalkInstance
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Paint http method.
|
|
160
|
+
*
|
|
161
|
+
* @return {import('chalk').ChalkInstance}
|
|
162
|
+
*/
|
|
163
|
+
static get DELETE(): import('chalk').ChalkInstance
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Paint http method.
|
|
167
|
+
*
|
|
168
|
+
* @return {import('chalk').ChalkInstance}
|
|
169
|
+
*/
|
|
170
|
+
static get OPTIONS(): import('chalk').ChalkInstance
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Remove all colors and special chars of string.
|
|
174
|
+
*
|
|
175
|
+
* @param {string} string
|
|
176
|
+
* @return {import('chalk').ChalkInstance}
|
|
177
|
+
*/
|
|
178
|
+
static removeColors(string: string): import('chalk').ChalkInstance
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Paint by the http method.
|
|
182
|
+
*
|
|
183
|
+
* @param {'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH', 'OPTIONS', 'HEAD' } method
|
|
184
|
+
* @return {import('chalk').ChalkInstance}
|
|
185
|
+
*/
|
|
186
|
+
static httpMethod(method: any): import('chalk').ChalkInstance
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export class FactoryHelper {
|
|
190
|
+
/**
|
|
191
|
+
* Get the timestamp value formatted.
|
|
192
|
+
*
|
|
193
|
+
* @return {string}
|
|
194
|
+
*/
|
|
195
|
+
static getTimestamp(): string
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Get the timestamp diff between logs.
|
|
199
|
+
*
|
|
200
|
+
* @param {number} lastTimestamp
|
|
201
|
+
* @return {string}
|
|
202
|
+
*/
|
|
203
|
+
static getTimestampDiff(lastTimestamp: number): string
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Get the emoji by level.
|
|
207
|
+
*
|
|
208
|
+
* @param {string} level
|
|
209
|
+
* @param {string} [customEmoji]
|
|
210
|
+
* @return {string}
|
|
211
|
+
*/
|
|
212
|
+
static getEmojiByLevel(level: string, customEmoji?: string): string
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Paint the message by level.
|
|
216
|
+
*
|
|
217
|
+
* @param {string} level
|
|
218
|
+
* @param {string|any} message
|
|
219
|
+
* @return {string}
|
|
220
|
+
*/
|
|
221
|
+
static paintByLevel(level: string, message: string): string
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Group the configuration values.
|
|
225
|
+
*
|
|
226
|
+
* @param {any} object
|
|
227
|
+
* @param {any} defaultValue
|
|
228
|
+
* @return {any}
|
|
229
|
+
*/
|
|
230
|
+
static groupConfigs(object: any, defaultValue: any): any
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export class DriverFactory {
|
|
234
|
+
/**
|
|
235
|
+
* Drivers of DriverFactory.
|
|
236
|
+
*
|
|
237
|
+
* @type {Map<string, { Driver: any }>}
|
|
238
|
+
*/
|
|
239
|
+
static drivers: Map<
|
|
240
|
+
string,
|
|
241
|
+
{
|
|
242
|
+
Driver: any
|
|
243
|
+
}
|
|
244
|
+
>
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Return an array with all available drivers.
|
|
248
|
+
*
|
|
249
|
+
* @return {any[]}
|
|
250
|
+
*/
|
|
251
|
+
static availableDrivers(): any[]
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Fabricate a new instance of a driver based on
|
|
255
|
+
* channel configurations.
|
|
256
|
+
*
|
|
257
|
+
* @param {string} channelName
|
|
258
|
+
* @param {any} runtimeConfig
|
|
259
|
+
* @return {any}
|
|
260
|
+
*/
|
|
261
|
+
static fabricate(channelName: string, runtimeConfig?: any): any
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Creates a new driver implementation.
|
|
265
|
+
*
|
|
266
|
+
* @param {string} name
|
|
267
|
+
* @param {(channel: string, configs?: any) => any} driver
|
|
268
|
+
*/
|
|
269
|
+
static createDriver(
|
|
270
|
+
name: string,
|
|
271
|
+
driver: (channel: string, configs?: any) => any,
|
|
272
|
+
): void
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export class FormatterFactory {
|
|
276
|
+
/**
|
|
277
|
+
* Formatters of FormatterFactory.
|
|
278
|
+
*
|
|
279
|
+
* @type {Map<string, { Formatter: any }>}
|
|
280
|
+
*/
|
|
281
|
+
static formatters: Map<
|
|
282
|
+
string,
|
|
283
|
+
{
|
|
284
|
+
Formatter: any
|
|
285
|
+
}
|
|
286
|
+
>
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Return an array with all available formatters.
|
|
290
|
+
*
|
|
291
|
+
* @return {any[]}
|
|
292
|
+
*/
|
|
293
|
+
static availableFormatters(): any[]
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Fabricate a new instance of a formatter.
|
|
297
|
+
*
|
|
298
|
+
* @param {string} formatterName
|
|
299
|
+
* @return {any}
|
|
300
|
+
*/
|
|
301
|
+
static fabricate(formatterName: string): any
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Creates a new formatter implementation.
|
|
305
|
+
*
|
|
306
|
+
* @param {string} name
|
|
307
|
+
* @param {() => any} formatter
|
|
308
|
+
*/
|
|
309
|
+
static createFormatter(name: string, formatter: () => any): void
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export class Logger {
|
|
313
|
+
/**
|
|
314
|
+
* Set runtime configurations for drivers and
|
|
315
|
+
* formatters.
|
|
316
|
+
*
|
|
317
|
+
* @param {any} runtimeConfig
|
|
318
|
+
* @return {Logger}
|
|
319
|
+
*/
|
|
320
|
+
config(runtimeConfig: any): Logger
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Change the log channel.
|
|
324
|
+
*
|
|
325
|
+
* @param {string[]} channels
|
|
326
|
+
* @return {Logger}
|
|
327
|
+
*/
|
|
328
|
+
channel(...channels: string[]): Logger
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Creates a log of type info in channel.
|
|
332
|
+
*
|
|
333
|
+
* @param {string|any} message
|
|
334
|
+
* @param {any} [options]
|
|
335
|
+
* @return {void | Promise<void>}
|
|
336
|
+
*/
|
|
337
|
+
info(message: string | any, options?: any): void | Promise<void>
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Creates a log of type warn in channel.
|
|
341
|
+
*
|
|
342
|
+
* @param {string|any} message
|
|
343
|
+
* @param {any} [options]
|
|
344
|
+
* @return {void | Promise<void>}
|
|
345
|
+
*/
|
|
346
|
+
warn(message: string | any, options?: any): void | Promise<void>
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Creates a log of type error in channel.
|
|
350
|
+
*
|
|
351
|
+
* @param {string|any} message
|
|
352
|
+
* @param {any} [options]
|
|
353
|
+
* @return {void | Promise<void>}
|
|
354
|
+
*/
|
|
355
|
+
error(message: string | any, options?: any): void | Promise<void>
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Creates a log of type critical in channel.
|
|
359
|
+
*
|
|
360
|
+
* @param {string|any} message
|
|
361
|
+
* @param {any} [options]
|
|
362
|
+
* @return {void | Promise<void>}
|
|
363
|
+
*/
|
|
364
|
+
critical(message: string | any, options?: any): void | Promise<void>
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Creates a log of type debug in channel.
|
|
368
|
+
*
|
|
369
|
+
* @param {string|any} message
|
|
370
|
+
* @param {any} [options]
|
|
371
|
+
* @return {void | Promise<void>}
|
|
372
|
+
*/
|
|
373
|
+
debug(message: string | any, options?: any): void | Promise<void>
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Creates a log of type success in channel.
|
|
377
|
+
*
|
|
378
|
+
* @param {string|any} message
|
|
379
|
+
* @param {any} [options]
|
|
380
|
+
* @return {void | Promise<void>}
|
|
381
|
+
*/
|
|
382
|
+
success(message: string | any, options?: any): void | Promise<void>
|
|
383
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import Chalk from 'chalk'
|
|
2
|
+
|
|
3
|
+
import { Is, Options } from '@secjs/utils'
|
|
4
|
+
|
|
5
|
+
import { ColorHelper } from '#src/Helpers/ColorHelper'
|
|
6
|
+
import { DriverFactory } from '#src/Factories/DriverFactory'
|
|
7
|
+
import { FormatterFactory } from '#src/Factories/FormatterFactory'
|
|
8
|
+
|
|
9
|
+
export * from './Facades/Log.js'
|
|
10
|
+
export * from './Helpers/ColorHelper.js'
|
|
11
|
+
export * from './Helpers/FactoryHelper.js'
|
|
12
|
+
export * from './Factories/DriverFactory.js'
|
|
13
|
+
export * from './Factories/FormatterFactory.js'
|
|
14
|
+
|
|
15
|
+
export class Logger {
|
|
16
|
+
/**
|
|
17
|
+
* Runtime configurations to be used inside the Drivers and Formatters.
|
|
18
|
+
*
|
|
19
|
+
* @type {any}
|
|
20
|
+
*/
|
|
21
|
+
#runtimeConfig = {}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The driver responsible for transporting the logs.
|
|
25
|
+
*
|
|
26
|
+
* @type {any[]}
|
|
27
|
+
*/
|
|
28
|
+
#drivers = []
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The log channel selected with driver and formatter configurations.
|
|
32
|
+
*
|
|
33
|
+
* @type {string[]}
|
|
34
|
+
*/
|
|
35
|
+
#channelNames = []
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Creates a new instance of Logger.
|
|
39
|
+
*
|
|
40
|
+
* @return {Logger}
|
|
41
|
+
*/
|
|
42
|
+
constructor() {
|
|
43
|
+
this.#drivers.push(DriverFactory.fabricate('default', this.#runtimeConfig))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Return all drivers available.
|
|
48
|
+
*
|
|
49
|
+
* @return {string[]}
|
|
50
|
+
*/
|
|
51
|
+
static get drivers() {
|
|
52
|
+
return DriverFactory.availableDrivers()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Return all formatters available.
|
|
57
|
+
*
|
|
58
|
+
* @return {string[]}
|
|
59
|
+
*/
|
|
60
|
+
static get formatters() {
|
|
61
|
+
return FormatterFactory.availableFormatters()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Applies the log engine to execute chalk methods of string.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} content
|
|
68
|
+
*/
|
|
69
|
+
static #applyLogEngine(content) {
|
|
70
|
+
if (Is.String(content)) {
|
|
71
|
+
const matches = content.match(/\({(.*?)} (.*?)\)/)
|
|
72
|
+
|
|
73
|
+
if (matches) {
|
|
74
|
+
const chalkMethodsString = matches[1].replace(/\s/g, '')
|
|
75
|
+
const chalkMethodsArray = chalkMethodsString.split(',')
|
|
76
|
+
const message = matches[2]
|
|
77
|
+
|
|
78
|
+
let chalk = Chalk
|
|
79
|
+
|
|
80
|
+
chalkMethodsArray.forEach(chalkMethod => {
|
|
81
|
+
if (!chalk[chalkMethod]) return
|
|
82
|
+
|
|
83
|
+
chalk = chalk[chalkMethod]
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
content = content
|
|
87
|
+
.replace(`({${matches[1]}} `, '')
|
|
88
|
+
.replace(`({${matches[1]}}`, '')
|
|
89
|
+
.replace(`${matches[2]})`, chalk(message))
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return content
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return content
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Transport the log.
|
|
100
|
+
*
|
|
101
|
+
* @param {string|any} message
|
|
102
|
+
* @param {any} [options]
|
|
103
|
+
* @param {any} [defaultValues]
|
|
104
|
+
* @return {void | Promise<void>}
|
|
105
|
+
*/
|
|
106
|
+
#log(message, options = {}, defaultValues = {}) {
|
|
107
|
+
options = this.#createOptions(options, defaultValues)
|
|
108
|
+
|
|
109
|
+
message = Logger.#applyLogEngine(message)
|
|
110
|
+
|
|
111
|
+
const promises = this.#drivers.map(d => d.transport(message, options))
|
|
112
|
+
|
|
113
|
+
return Promise.all(promises)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Create options concatenating client options with default options.
|
|
118
|
+
*
|
|
119
|
+
* @param {any} options
|
|
120
|
+
* @param {any} defaultValues
|
|
121
|
+
* @return {any}
|
|
122
|
+
*/
|
|
123
|
+
#createOptions(options, defaultValues) {
|
|
124
|
+
let formatterConfig = Options.create(
|
|
125
|
+
options.formatterConfig,
|
|
126
|
+
defaultValues.formatterConfig,
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
if (this.#runtimeConfig.formatterConfig) {
|
|
130
|
+
formatterConfig = {
|
|
131
|
+
...this.#runtimeConfig.formatterConfig,
|
|
132
|
+
...formatterConfig,
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
options = {
|
|
137
|
+
...options,
|
|
138
|
+
formatterConfig,
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return options
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Set runtime configurations for drivers and
|
|
146
|
+
* formatters.
|
|
147
|
+
*
|
|
148
|
+
* @param {any} runtimeConfig
|
|
149
|
+
* @return {Logger}
|
|
150
|
+
*/
|
|
151
|
+
config(runtimeConfig) {
|
|
152
|
+
this.#runtimeConfig = runtimeConfig
|
|
153
|
+
|
|
154
|
+
return this
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Change the log channel.
|
|
159
|
+
*
|
|
160
|
+
* @param {string[]} channels
|
|
161
|
+
* @return {Logger}
|
|
162
|
+
*/
|
|
163
|
+
channel(...channels) {
|
|
164
|
+
this.#drivers = []
|
|
165
|
+
this.#channelNames = channels
|
|
166
|
+
|
|
167
|
+
channels.forEach(channel => {
|
|
168
|
+
this.#drivers.push(DriverFactory.fabricate(channel, this.#runtimeConfig))
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
return this
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Creates a log of type info in channel.
|
|
176
|
+
*
|
|
177
|
+
* @param {string|any} message
|
|
178
|
+
* @param {any} [options]
|
|
179
|
+
* @return {void | Promise<void>}
|
|
180
|
+
*/
|
|
181
|
+
info(message, options = {}) {
|
|
182
|
+
return this.#log(message, options, {
|
|
183
|
+
formatterConfig: {
|
|
184
|
+
level: 'INFO',
|
|
185
|
+
chalk: ColorHelper.cyan,
|
|
186
|
+
},
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Creates a log of type warn in channel.
|
|
192
|
+
*
|
|
193
|
+
* @param {string|any} message
|
|
194
|
+
* @param {any} [options]
|
|
195
|
+
* @return {void | Promise<void>}
|
|
196
|
+
*/
|
|
197
|
+
warn(message, options = {}) {
|
|
198
|
+
return this.#log(message, options, {
|
|
199
|
+
formatterConfig: {
|
|
200
|
+
level: 'WARN',
|
|
201
|
+
chalk: ColorHelper.orange,
|
|
202
|
+
},
|
|
203
|
+
})
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Creates a log of type error in channel.
|
|
208
|
+
*
|
|
209
|
+
* @param {string|any} message
|
|
210
|
+
* @param {any} [options]
|
|
211
|
+
* @return {void | Promise<void>}
|
|
212
|
+
*/
|
|
213
|
+
error(message, options = {}) {
|
|
214
|
+
return this.#log(message, options, {
|
|
215
|
+
formatterConfig: {
|
|
216
|
+
level: 'ERROR',
|
|
217
|
+
chalk: ColorHelper.red,
|
|
218
|
+
},
|
|
219
|
+
})
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Creates a log of type critical in channel.
|
|
224
|
+
*
|
|
225
|
+
* @param {string|any} message
|
|
226
|
+
* @param {any} [options]
|
|
227
|
+
* @return {void | Promise<void>}
|
|
228
|
+
*/
|
|
229
|
+
critical(message, options = {}) {
|
|
230
|
+
return this.#log(message, options, {
|
|
231
|
+
formatterConfig: {
|
|
232
|
+
level: 'CRITICAL',
|
|
233
|
+
chalk: ColorHelper.darkRed,
|
|
234
|
+
},
|
|
235
|
+
})
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Creates a log of type debug in channel.
|
|
240
|
+
*
|
|
241
|
+
* @param {string|any} message
|
|
242
|
+
* @param {any} [options]
|
|
243
|
+
* @return {void | Promise<void>}
|
|
244
|
+
*/
|
|
245
|
+
debug(message, options = {}) {
|
|
246
|
+
return this.#log(message, options, {
|
|
247
|
+
formatterConfig: {
|
|
248
|
+
level: 'DEBUG',
|
|
249
|
+
chalk: ColorHelper.purple,
|
|
250
|
+
},
|
|
251
|
+
})
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Creates a log of type success in channel.
|
|
256
|
+
*
|
|
257
|
+
* @param {string|any} message
|
|
258
|
+
* @param {any} [options]
|
|
259
|
+
* @return {void | Promise<void>}
|
|
260
|
+
*/
|
|
261
|
+
success(message, options = {}) {
|
|
262
|
+
return this.#log(message, options, {
|
|
263
|
+
formatterConfig: {
|
|
264
|
+
level: 'SUCCESS',
|
|
265
|
+
chalk: ColorHelper.green,
|
|
266
|
+
},
|
|
267
|
+
})
|
|
268
|
+
}
|
|
269
|
+
}
|
package/index.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
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 * from './src/Logger';
|
|
10
|
-
export * from './src/Facades/Log';
|
|
11
|
-
export * from './src/Utils/Color';
|
|
12
|
-
export * from './src/Contracts/DriverContract';
|
|
13
|
-
export * from './src/Contracts/FormatterContract';
|
package/index.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
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
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
11
|
-
if (k2 === undefined) k2 = k;
|
|
12
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
13
|
-
}) : (function(o, m, k, k2) {
|
|
14
|
-
if (k2 === undefined) k2 = k;
|
|
15
|
-
o[k2] = m[k];
|
|
16
|
-
}));
|
|
17
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
18
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
19
|
-
};
|
|
20
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
-
__exportStar(require("./src/Logger"), exports);
|
|
22
|
-
__exportStar(require("./src/Facades/Log"), exports);
|
|
23
|
-
__exportStar(require("./src/Utils/Color"), exports);
|
|
24
|
-
__exportStar(require("./src/Contracts/DriverContract"), exports);
|
|
25
|
-
__exportStar(require("./src/Contracts/FormatterContract"), exports);
|
|
@@ -1,13 +0,0 @@
|
|
|
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 { DefaultDriverConfigs } from './DefaultDriverConfigs';
|
|
10
|
-
export interface DriverContract {
|
|
11
|
-
configs?: DefaultDriverConfigs | any;
|
|
12
|
-
transport(message: string, options?: any): void | Promise<void>;
|
|
13
|
-
}
|