@athenna/logger 3.0.3 → 3.0.5

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.3",
3
+ "version": "3.0.5",
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>",
@@ -59,6 +59,7 @@
59
59
  "@japa/runner": "2.0.7",
60
60
  "@japa/spec-reporter": "1.1.12",
61
61
  "c8": "7.11.2",
62
+ "cls-rtracer": "2.6.2",
62
63
  "commitizen": "4.2.5",
63
64
  "cross-env": "7.0.3",
64
65
  "cz-conventional-changelog": "3.3.0",
@@ -7,10 +7,10 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
 
10
- import { hostname } from 'node:os'
10
+ import rTracer from 'cls-rtracer'
11
11
 
12
+ import { hostname } from 'node:os'
12
13
  import { Is } from '@athenna/common'
13
-
14
14
  import { ColorHelper } from '#src/Helpers/ColorHelper'
15
15
 
16
16
  export class Formatter {
@@ -57,6 +57,24 @@ export class Formatter {
57
57
  return hostname()
58
58
  }
59
59
 
60
+ /**
61
+ * Get the level without any color or format.
62
+ *
63
+ * @return {string}
64
+ */
65
+ level() {
66
+ return this.configs.level
67
+ }
68
+
69
+ /**
70
+ * Get the trace id for formatter.
71
+ *
72
+ * @return {string | null}
73
+ */
74
+ traceId() {
75
+ return rTracer.id() || null
76
+ }
77
+
60
78
  /**
61
79
  * Create the timestamp for formatter.
62
80
  *
@@ -95,13 +113,16 @@ export class Formatter {
95
113
 
96
114
  /**
97
115
  * Clean the message removing colors if clean
98
- * option is true.
116
+ * option is true. If force is true, then colors
117
+ * will be removed even if configs clean option
118
+ * is false.
99
119
  *
100
120
  * @param message {string}
121
+ * @param [force] {boolean}
101
122
  * @return {string}
102
123
  */
103
- clean(message) {
104
- if (this.configs.clean) {
124
+ clean(message, force = false) {
125
+ if (this.configs.clean || force) {
105
126
  return ColorHelper.removeColors(message)
106
127
  }
107
128
 
@@ -19,10 +19,11 @@ export class JsonFormatter extends Formatter {
19
19
  */
20
20
  format(message) {
21
21
  const base = {
22
- level: this.configs.level,
22
+ level: this.level(),
23
23
  time: Date.now(),
24
24
  pid: this.pid(),
25
25
  hostname: this.hostname(),
26
+ traceId: this.traceId(),
26
27
  }
27
28
 
28
29
  if (Is.String(message)) {
@@ -41,6 +41,7 @@ export class RequestFormatter extends Formatter {
41
41
  url: ctx.request.hostUrl,
42
42
  path: ctx.request.baseUrl,
43
43
  createdAt: Date.now(),
44
+ traceId: this.traceId(),
44
45
  data: ctx.data,
45
46
  }
46
47
 
@@ -17,12 +17,11 @@ export class SimpleFormatter extends Formatter {
17
17
  * @return {string}
18
18
  */
19
19
  format(message) {
20
+ const pid = this.pid()
21
+ const time = this.timestamp()
20
22
  const level = this.simpleLevel()
23
+ const colorizedMsg = this.applyColors(message)
21
24
 
22
- return this.clean(
23
- `${level} - ${this.timestamp()} - (${this.pid()}) ${this.applyColors(
24
- message,
25
- )}`,
26
- )
25
+ return this.clean(`${level} - ${time} - (${pid}) ${colorizedMsg}`)
27
26
  }
28
27
  }
package/src/index.d.ts CHANGED
@@ -126,6 +126,20 @@ export class Formatter {
126
126
  */
127
127
  hostname(): string
128
128
 
129
+ /**
130
+ * Get the level without any color or format.
131
+ *
132
+ * @return {string}
133
+ */
134
+ level(): string
135
+
136
+ /**
137
+ * Get the trace id for formatter.
138
+ *
139
+ * @return {string | null}
140
+ */
141
+ traceId(): string | null
142
+
129
143
  /**
130
144
  * Create the timestamp for formatter.
131
145
  *
@@ -143,12 +157,15 @@ export class Formatter {
143
157
 
144
158
  /**
145
159
  * Clean the message removing colors if clean
146
- * option is true.
160
+ * option is true. If force is true, then colors
161
+ * will be removed even if configs clean option
162
+ * is false.
147
163
  *
148
164
  * @param message {string}
165
+ * @param [force] {boolean}
149
166
  * @return {string}
150
167
  */
151
- clean(message: string): string
168
+ clean(message, force?: boolean): string
152
169
 
153
170
  /**
154
171
  * Apply all colors necessary to message.
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Chalk } from 'chalk'
2
- import { Is } from '@athenna/common'
3
2
  import { format } from 'node:util'
3
+ import { Is } from '@athenna/common'
4
4
 
5
5
  import { DriverFactory } from '#src/Factories/DriverFactory'
6
6