@athenna/logger 3.0.4 → 3.0.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@athenna/logger",
3
- "version": "3.0.4",
3
+ "version": "3.0.6",
4
4
  "description": "The Athenna logging solution. Log in stdout, files and buckets.",
5
5
  "license": "MIT",
6
6
  "author": "João Lenon <lenon@athenna.io>",
@@ -51,9 +51,9 @@
51
51
  "telegraf": "4.7.0"
52
52
  },
53
53
  "devDependencies": {
54
- "@athenna/common": "3.0.0",
55
- "@athenna/config": "3.0.2",
56
- "@athenna/ioc": "3.0.0",
54
+ "@athenna/common": "3.0.1",
55
+ "@athenna/config": "3.0.3",
56
+ "@athenna/ioc": "3.0.1",
57
57
  "@japa/assert": "1.3.4",
58
58
  "@japa/run-failed-tests": "1.0.7",
59
59
  "@japa/runner": "2.0.7",
@@ -60,7 +60,7 @@ export class Driver {
60
60
  * @param {any} configs
61
61
  * @return {ConsoleDriver}
62
62
  */
63
- constructor(configs) {
63
+ constructor(configs = {}) {
64
64
  this.configs = configs
65
65
 
66
66
  const json = Json.copy(configs)
@@ -70,8 +70,8 @@ export class Driver {
70
70
 
71
71
  this.driverConfig = json
72
72
  this.level = json.level || 'info'
73
- this.formatter = configs.formatter
74
- this.formatterConfig = configs.formatterConfig
73
+ this.formatter = configs.formatter || 'none'
74
+ this.formatterConfig = configs.formatterConfig || {}
75
75
  }
76
76
 
77
77
  /**
@@ -69,6 +69,20 @@ export class DriverFactory {
69
69
  return new Driver(configs)
70
70
  }
71
71
 
72
+ /**
73
+ * Fabricate a new instance of a driver without
74
+ * configurations.
75
+ *
76
+ * @param {string} driverName
77
+ * @param {any} runtimeConfig
78
+ * @return {any}
79
+ */
80
+ static fabricateOnly(driverName, runtimeConfig = {}) {
81
+ const { Driver } = this.#drivers.get(driverName)
82
+
83
+ return new Driver(runtimeConfig)
84
+ }
85
+
72
86
  /**
73
87
  * Creates a new driver implementation.
74
88
  *
@@ -7,13 +7,12 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
 
10
+ import rTracer from 'cls-rtracer'
11
+
10
12
  import { hostname } from 'node:os'
11
13
  import { Is } from '@athenna/common'
12
- import { createRequire } from 'node:module'
13
14
  import { ColorHelper } from '#src/Helpers/ColorHelper'
14
15
 
15
- const require = createRequire(import.meta.url)
16
-
17
16
  export class Formatter {
18
17
  /**
19
18
  * Holds the configuration object of formatter.
@@ -73,13 +72,7 @@ export class Formatter {
73
72
  * @return {string | null}
74
73
  */
75
74
  traceId() {
76
- try {
77
- const rTracer = require('cls-rtracer')
78
-
79
- return rTracer.id()
80
- } catch (err) {
81
- return null
82
- }
75
+ return rTracer.id() || null
83
76
  }
84
77
 
85
78
  /**
@@ -7,7 +7,9 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
 
10
- import chalk from 'chalk'
10
+ import { Is } from '@athenna/common'
11
+ import { format } from 'node:util'
12
+ import chalk, { Chalk } from 'chalk'
11
13
 
12
14
  export class ColorHelper {
13
15
  /**
@@ -274,4 +276,47 @@ export class ColorHelper {
274
276
  static httpMethod(method) {
275
277
  return this[method]
276
278
  }
279
+
280
+ /**
281
+ * Applies the log engine to execute chalk methods.
282
+ *
283
+ * @param {string} args
284
+ * @return {any}
285
+ */
286
+ static applyLogEngine(...args) {
287
+ if (!Is.String(args[0])) {
288
+ return args[0]
289
+ }
290
+
291
+ let content = format(...args.filter(arg => arg !== undefined))
292
+
293
+ const matches = content.match(/\({(.*?)} ([\s\S]*?)\)/g)
294
+
295
+ if (!matches) {
296
+ return content
297
+ }
298
+
299
+ matches.forEach(match => {
300
+ const [chalkMethodsInBrackets, chalkMethodsString] =
301
+ match.match(/\{(.*?)\}/)
302
+
303
+ const message = match
304
+ .replace(chalkMethodsInBrackets, '')
305
+ .replace(/\s*\(\s*|\s*\)\s*/g, '')
306
+
307
+ const chalkMethodsArray = chalkMethodsString.replace(/\s/g, '').split(',')
308
+
309
+ let chalk = new Chalk()
310
+
311
+ chalkMethodsArray.forEach(chalkMethod => {
312
+ if (!chalk[chalkMethod]) return
313
+
314
+ chalk = chalk[chalkMethod]
315
+ })
316
+
317
+ content = content.replace(match, chalk(message))
318
+ })
319
+
320
+ return content
321
+ }
277
322
  }
@@ -0,0 +1,134 @@
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 { ColorHelper } from '#src/index'
11
+ import { DriverFactory } from '#src/Factories/DriverFactory'
12
+
13
+ export class ConsoleLogger {
14
+ /**
15
+ * The driver responsible for transporting the logs.
16
+ *
17
+ * @type {any[]}
18
+ */
19
+ #drivers = []
20
+
21
+ /**
22
+ * Creates a new instance of ConsoleLogger.
23
+ *
24
+ * @param {any} [runtimeConfigs]
25
+ * @return {ConsoleLogger}
26
+ */
27
+ constructor(runtimeConfigs) {
28
+ this.#drivers.push(DriverFactory.fabricateOnly('console', runtimeConfigs))
29
+ }
30
+
31
+ /**
32
+ * Set runtime configurations for drivers and
33
+ * formatters.
34
+ *
35
+ * @return {ConsoleLogger}
36
+ */
37
+ config() {
38
+ return this
39
+ }
40
+
41
+ /**
42
+ * Change the log channel.
43
+ *
44
+ * @return {ConsoleLogger}
45
+ */
46
+ channel() {
47
+ return this
48
+ }
49
+
50
+ /**
51
+ * Call drivers to transport the log.
52
+ *
53
+ * @param {string} level
54
+ * @param {string} args
55
+ * @return {any | Promise<any>}
56
+ */
57
+ #log(level, ...args) {
58
+ const message = ColorHelper.applyLogEngine(...args)
59
+
60
+ const promises = this.#drivers.map(d => d.transport(level, message))
61
+
62
+ return Promise.all(promises)
63
+ }
64
+
65
+ /**
66
+ * Creates a log of type trace in channel.
67
+ *
68
+ * @param {string|any} args
69
+ * @return {any | Promise<any>}
70
+ */
71
+ trace(...args) {
72
+ return this.#log('trace', ...args)
73
+ }
74
+
75
+ /**
76
+ * Creates a log of type debug in channel.
77
+ *
78
+ * @param {string|any} args
79
+ * @return {any | Promise<any>}
80
+ */
81
+ debug(...args) {
82
+ return this.#log('debug', ...args)
83
+ }
84
+
85
+ /**
86
+ * Creates a log of type info in channel.
87
+ *
88
+ * @param {string|any} args
89
+ * @return {any | Promise<any>}
90
+ */
91
+ info(...args) {
92
+ return this.#log('info', ...args)
93
+ }
94
+
95
+ /**
96
+ * Creates a log of type success in channel.
97
+ *
98
+ * @param {string|any} args
99
+ * @return {any | Promise<any>}
100
+ */
101
+ success(...args) {
102
+ return this.#log('success', ...args)
103
+ }
104
+
105
+ /**
106
+ * Creates a log of type warn in channel.
107
+ *
108
+ * @param {string|any} args
109
+ * @return {any | Promise<any>}
110
+ */
111
+ warn(...args) {
112
+ return this.#log('warn', ...args)
113
+ }
114
+
115
+ /**
116
+ * Creates a log of type error in channel.
117
+ *
118
+ * @param {string|any} args
119
+ * @return {any | Promise<any>}
120
+ */
121
+ error(...args) {
122
+ return this.#log('error', ...args)
123
+ }
124
+
125
+ /**
126
+ * Creates a log of type fatal in channel.
127
+ *
128
+ * @param {string|any} args
129
+ * @return {any | Promise<any>}
130
+ */
131
+ fatal(...args) {
132
+ return this.#log('fatal', ...args)
133
+ }
134
+ }
package/src/index.d.ts CHANGED
@@ -1,3 +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
+
1
10
  import { Facade } from '@athenna/ioc'
2
11
 
3
12
  export const Log: typeof Facade & Logger
@@ -404,6 +413,14 @@ export class ColorHelper {
404
413
  * @return {import('chalk').ChalkInstance}
405
414
  */
406
415
  static httpMethod(method: any): import('chalk').ChalkInstance
416
+
417
+ /**
418
+ * Applies the log engine to execute chalk methods.
419
+ *
420
+ * @param {string} args
421
+ * @return {any}
422
+ */
423
+ static applyLogEngine(...args): any
407
424
  }
408
425
 
409
426
  export class FactoryHelper {
@@ -447,6 +464,16 @@ export class DriverFactory {
447
464
  */
448
465
  static fabricate(channelName: string, runtimeConfig?: any): any
449
466
 
467
+ /**
468
+ * Fabricate a new instance of a driver without
469
+ * configurations.
470
+ *
471
+ * @param {string} driverName
472
+ * @param {any} runtimeConfig
473
+ * @return {any}
474
+ */
475
+ static fabricateOnly(driverName: string, runtimeConfig?: any): any
476
+
450
477
  /**
451
478
  * Creates a new driver implementation.
452
479
  *
@@ -496,7 +523,113 @@ export class FormatterFactory {
496
523
  static createFormatter(name: string, formatter: () => any): void
497
524
  }
498
525
 
526
+ export class ConsoleLogger {
527
+ /**
528
+ * Creates a log of type trace in channel.
529
+ *
530
+ * @param {string|any} message
531
+ * @return {any | Promise<any>}
532
+ */
533
+ trace(message: string | any): any | Promise<any>
534
+
535
+ /**
536
+ * Creates a log of type trace in channel.
537
+ *
538
+ * @param {string[]|any[]} args
539
+ * @return {any | Promise<any>}
540
+ */
541
+ trace(...args: string[] | any[]): any | Promise<any>
542
+
543
+ /**
544
+ * Creates a log of type debug in channel.
545
+ *
546
+ * @param {string|any} message
547
+ * @return {any | Promise<any>}
548
+ */
549
+ debug(message: string | any): any | Promise<any>
550
+
551
+ /**
552
+ * Creates a log of type debug in channel.
553
+ *
554
+ * @param {string[]|any[]} args
555
+ * @return {any | Promise<any>}
556
+ */
557
+ debug(...args: string[] | any[]): any | Promise<any>
558
+
559
+ /**
560
+ * Creates a log of type info in channel.
561
+ *
562
+ * @param {string|any} message
563
+ * @return {any | Promise<any>}
564
+ */
565
+ info(message: string | any): any | Promise<any>
566
+
567
+ /**
568
+ * Creates a log of type info in channel.
569
+ *
570
+ * @param {string[]|any[]} args
571
+ * @return {any | Promise<any>}
572
+ */
573
+ info(...args: string[] | any[]): any | Promise<any>
574
+
575
+ /**
576
+ * Creates a log of type success in channel.
577
+ *
578
+ * @param {string|any} message
579
+ * @return {any | Promise<any>}
580
+ */
581
+ success(message: string | any): any | Promise<any>
582
+
583
+ /**
584
+ * Creates a log of type success in channel.
585
+ *
586
+ * @param {string[]|any[]} args
587
+ * @return {any | Promise<any>}
588
+ */
589
+ success(...args: string[] | any[]): any | Promise<any>
590
+
591
+ /**
592
+ * Creates a log of type warn in channel.
593
+ *
594
+ * @param {string|any} message
595
+ * @return {any | Promise<any>}
596
+ */
597
+ warn(message: string | any): any | Promise<any>
598
+
599
+ /**
600
+ * Creates a log of type warn in channel.
601
+ *
602
+ * @param {string[]|any[]} args
603
+ * @return {any | Promise<any>}
604
+ */
605
+ warn(...args: string[] | any[]): any | Promise<any>
606
+
607
+ /**
608
+ * Creates a log of type error in channel.
609
+ *
610
+ * @param {string|any} message
611
+ * @return {any | Promise<any>}
612
+ */
613
+ error(message: string | any): any | Promise<any>
614
+
615
+ /**
616
+ * Creates a log of type error in channel.
617
+ *
618
+ * @param {string[]|any[]} args
619
+ * @return {any | Promise<any>}
620
+ */
621
+ error(...args: string[] | any[]): any | Promise<any>
622
+ }
623
+
499
624
  export class Logger {
625
+ /**
626
+ * Get a new instance of the ConsoleLogger.
627
+ *
628
+ * @param {any} [runtimeConfigs]
629
+ * @return {ConsoleLogger}
630
+ */
631
+ static getConsoleLogger(runtimeConfigs?: any): ConsoleLogger
632
+
500
633
  /**
501
634
  * Set runtime configurations for drivers and
502
635
  * formatters.
package/src/index.js CHANGED
@@ -1,12 +1,20 @@
1
- import { Chalk } from 'chalk'
2
- import { format } from 'node:util'
3
- import { Is } from '@athenna/common'
4
-
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 { ColorHelper } from '#src/index'
11
+ import { ConsoleLogger } from '#src/Helpers/ConsoleLogger'
5
12
  import { DriverFactory } from '#src/Factories/DriverFactory'
6
13
 
7
14
  export * from './Facades/Log.js'
8
15
 
9
16
  export * from './Helpers/ColorHelper.js'
17
+ export * from './Helpers/ConsoleLogger.js'
10
18
  export * from './Helpers/FactoryHelper.js'
11
19
 
12
20
  export * from './Drivers/Driver.js'
@@ -16,6 +24,16 @@ export * from './Factories/DriverFactory.js'
16
24
  export * from './Factories/FormatterFactory.js'
17
25
 
18
26
  export class Logger {
27
+ /**
28
+ * Get a new instance of the ConsoleLogger.
29
+ *
30
+ * @param {any} [runtimeConfigs]
31
+ * @return {ConsoleLogger}
32
+ */
33
+ static getConsoleLogger(runtimeConfigs) {
34
+ return new ConsoleLogger(runtimeConfigs)
35
+ }
36
+
19
37
  /**
20
38
  * The driver responsible for transporting the logs.
21
39
  *
@@ -76,7 +94,7 @@ export class Logger {
76
94
  * @return {any | Promise<any>}
77
95
  */
78
96
  #log(level, ...args) {
79
- const message = this.#applyEngine(...args)
97
+ const message = ColorHelper.applyLogEngine(...args)
80
98
 
81
99
  const promises = this.#drivers.map(d => d.transport(level, message))
82
100
 
@@ -152,47 +170,4 @@ export class Logger {
152
170
  fatal(...args) {
153
171
  return this.#log('fatal', ...args)
154
172
  }
155
-
156
- /**
157
- * Applies the log engine to execute chalk methods.
158
- *
159
- * @param {string} args
160
- * @return {any}
161
- */
162
- #applyEngine(...args) {
163
- if (!Is.String(args[0])) {
164
- return args[0]
165
- }
166
-
167
- let content = format(...args.filter(arg => arg !== undefined))
168
-
169
- const matches = content.match(/\({(.*?)} ([\s\S]*?)\)/g)
170
-
171
- if (!matches) {
172
- return content
173
- }
174
-
175
- matches.forEach(match => {
176
- const [chalkMethodsInBrackets, chalkMethodsString] =
177
- match.match(/\{(.*?)\}/)
178
-
179
- const message = match
180
- .replace(chalkMethodsInBrackets, '')
181
- .replace(/\s*\(\s*|\s*\)\s*/g, '')
182
-
183
- const chalkMethodsArray = chalkMethodsString.replace(/\s/g, '').split(',')
184
-
185
- let chalk = new Chalk()
186
-
187
- chalkMethodsArray.forEach(chalkMethod => {
188
- if (!chalk[chalkMethod]) return
189
-
190
- chalk = chalk[chalkMethod]
191
- })
192
-
193
- content = content.replace(match, chalk(message))
194
- })
195
-
196
- return content
197
- }
198
173
  }