@athenna/logger 5.14.0 → 5.16.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@athenna/logger",
3
- "version": "5.14.0",
3
+ "version": "5.16.0",
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>",
@@ -63,6 +63,8 @@
63
63
  },
64
64
  "dependencies": {
65
65
  "@aws-lambda-powertools/logger": "^1.18.1",
66
+ "@opentelemetry/api": "^1.9.0",
67
+ "@opentelemetry/api-logs": "^0.213.0",
66
68
  "cls-rtracer": "^2.6.3",
67
69
  "telegraf": "^4.16.3"
68
70
  },
@@ -72,6 +74,7 @@
72
74
  "@athenna/ioc": "^5.2.0",
73
75
  "@athenna/test": "^5.5.0",
74
76
  "@athenna/tsconfig": "^5.0.0",
77
+ "@opentelemetry/context-async-hooks": "^2.6.0",
75
78
  "@typescript-eslint/eslint-plugin": "^8.38.0",
76
79
  "@typescript-eslint/parser": "^8.38.0",
77
80
  "commitizen": "^4.3.1",
@@ -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 { Driver } from '#src/drivers/Driver';
10
+ export declare class OtelDriver extends Driver {
11
+ transport(level: string, message: any): void;
12
+ private getBody;
13
+ private getSeverityNumber;
14
+ }
@@ -0,0 +1,51 @@
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 { Driver } from '#src/drivers/Driver';
10
+ import { context } from '@opentelemetry/api';
11
+ import { logs, SeverityNumber } from '@opentelemetry/api-logs';
12
+ export class OtelDriver extends Driver {
13
+ transport(level, message) {
14
+ if (!this.couldBeTransported(level)) {
15
+ return;
16
+ }
17
+ const logger = logs.getLogger('@athenna/logger');
18
+ logger.emit({
19
+ eventName: 'athenna.log',
20
+ severityNumber: this.getSeverityNumber(level),
21
+ severityText: level.toUpperCase(),
22
+ body: this.getBody(level, message),
23
+ attributes: {
24
+ 'athenna.log.level': level,
25
+ 'athenna.log.stream': this.getStreamTypeFor(level)
26
+ },
27
+ context: context.active()
28
+ });
29
+ }
30
+ getBody(level, message) {
31
+ const formatted = this.format(level, message, true);
32
+ try {
33
+ return JSON.parse(formatted);
34
+ }
35
+ catch {
36
+ return formatted;
37
+ }
38
+ }
39
+ getSeverityNumber(level) {
40
+ const levels = {
41
+ trace: SeverityNumber.TRACE,
42
+ debug: SeverityNumber.DEBUG,
43
+ info: SeverityNumber.INFO,
44
+ success: SeverityNumber.INFO2,
45
+ warn: SeverityNumber.WARN,
46
+ error: SeverityNumber.ERROR,
47
+ fatal: SeverityNumber.FATAL
48
+ };
49
+ return levels[level] || SeverityNumber.INFO;
50
+ }
51
+ }
@@ -12,6 +12,7 @@ import { Driver } from '#src/drivers/Driver';
12
12
  import { FileDriver } from '#src/drivers/FileDriver';
13
13
  import { NullDriver } from '#src/drivers/NullDriver';
14
14
  import { LokiDriver } from '#src/drivers/LokiDriver';
15
+ import { OtelDriver } from '#src/drivers/OtelDriver';
15
16
  import { SlackDriver } from '#src/drivers/SlackDriver';
16
17
  import { StackDriver } from '#src/drivers/StackDriver';
17
18
  import { LambdaDriver } from '#src/drivers/LambdaDriver';
@@ -30,6 +31,7 @@ export class DriverFactory {
30
31
  .set('file', { Driver: FileDriver })
31
32
  .set('null', { Driver: NullDriver })
32
33
  .set('loki', { Driver: LokiDriver })
34
+ .set('otel', { Driver: OtelDriver })
33
35
  .set('slack', { Driver: SlackDriver })
34
36
  .set('stack', { Driver: StackDriver })
35
37
  .set('lambda', { Driver: LambdaDriver })
@@ -35,6 +35,10 @@ export declare abstract class Formatter {
35
35
  * Get the trace id for formatter.
36
36
  */
37
37
  traceId(): string | null;
38
+ /**
39
+ * Get the span id for formatter.
40
+ */
41
+ spanId(): string | null;
38
42
  /**
39
43
  * Create the timestamp for formatter.
40
44
  */
@@ -8,6 +8,7 @@
8
8
  */
9
9
  import rTracer from 'cls-rtracer';
10
10
  import { hostname } from 'node:os';
11
+ import { trace } from '@opentelemetry/api';
11
12
  import { Is, Color } from '@athenna/common';
12
13
  export class Formatter {
13
14
  constructor() {
@@ -45,7 +46,14 @@ export class Formatter {
45
46
  * Get the trace id for formatter.
46
47
  */
47
48
  traceId() {
48
- return (rTracer.id() || null);
49
+ return (trace.getActiveSpan()?.spanContext().traceId ||
50
+ (rTracer.id() || null));
51
+ }
52
+ /**
53
+ * Get the span id for formatter.
54
+ */
55
+ spanId() {
56
+ return trace.getActiveSpan()?.spanContext().spanId || null;
49
57
  }
50
58
  /**
51
59
  * Create the timestamp for formatter.
@@ -16,7 +16,8 @@ export class JsonFormatter extends Formatter {
16
16
  time: Date.now(),
17
17
  pid: this.pid(),
18
18
  hostname: this.hostname(),
19
- traceId: this.traceId()
19
+ traceId: this.traceId(),
20
+ spanId: this.spanId()
20
21
  };
21
22
  if (Is.String(message)) {
22
23
  base.msg = message;
@@ -30,6 +30,7 @@ export class RequestFormatter extends Formatter {
30
30
  path: ctx.request.baseUrl,
31
31
  createdAt: Date.now(),
32
32
  traceId: this.traceId(),
33
+ spanId: this.spanId(),
33
34
  data: ctx.data
34
35
  };
35
36
  const request = {
@@ -17,12 +17,7 @@ export declare class Logger extends Macroable {
17
17
  */
18
18
  private runtimeConfigs;
19
19
  /**
20
- * Default message content that should be included
21
- * in every structured log emitted by this instance.
22
- */
23
- private messageDefaults;
24
- /**
25
- * Store the current logger strategy so create()
20
+ * Store the current logger strategy so config() and create()
26
21
  * can clone the logger preserving its behavior.
27
22
  */
28
23
  private selection;
@@ -97,8 +92,7 @@ export declare class Logger extends Macroable {
97
92
  */
98
93
  private log;
99
94
  /**
100
- * Attach message defaults to formatter configs so
101
- * structured formatters can enrich the output.
95
+ * Rebuild drivers using the current logger selection.
102
96
  */
103
- private withDefaultFormatterConfig;
97
+ private applySelection;
104
98
  }
@@ -23,16 +23,11 @@ export class Logger extends Macroable {
23
23
  */
24
24
  this.runtimeConfigs = {};
25
25
  /**
26
- * Default message content that should be included
27
- * in every structured log emitted by this instance.
28
- */
29
- this.messageDefaults = {};
30
- /**
31
- * Store the current logger strategy so create()
26
+ * Store the current logger strategy so config() and create()
32
27
  * can clone the logger preserving its behavior.
33
28
  */
34
29
  this.selection = {
35
- type: 'vanilla',
30
+ method: 'vanilla',
36
31
  values: []
37
32
  };
38
33
  this.channelOrVanilla(Config.get('logging.default'));
@@ -52,8 +47,8 @@ export class Logger extends Macroable {
52
47
  * formatters.
53
48
  */
54
49
  config(runtimeConfigs) {
55
- this.runtimeConfigs = runtimeConfigs;
56
- return this;
50
+ this.runtimeConfigs = Json.copy(runtimeConfigs || {});
51
+ return this.applySelection();
57
52
  }
58
53
  /**
59
54
  * Create a new logger instance inheriting the current
@@ -61,12 +56,16 @@ export class Logger extends Macroable {
61
56
  */
62
57
  create(defaults = {}) {
63
58
  const logger = new Logger();
64
- logger.runtimeConfigs = Json.copy(this.runtimeConfigs);
65
- logger.messageDefaults = {
66
- ...Json.copy(this.messageDefaults),
67
- ...Json.copy(defaults || {})
59
+ const runtimeConfigs = Json.copy(this.runtimeConfigs);
60
+ if (!runtimeConfigs.formatterConfig) {
61
+ runtimeConfigs.formatterConfig = {};
62
+ }
63
+ runtimeConfigs.formatterConfig.defaults = Json.copy(defaults);
64
+ logger.selection = {
65
+ method: this.selection.method,
66
+ values: Json.copy(this.selection.values)
68
67
  };
69
- return logger[this.selection.type](...Json.copy(this.selection.values));
68
+ return logger.config(runtimeConfigs);
70
69
  }
71
70
  /**
72
71
  * Change the log channel.
@@ -74,12 +73,12 @@ export class Logger extends Macroable {
74
73
  channel(...channels) {
75
74
  this.drivers = [];
76
75
  this.selection = {
77
- type: 'channel',
76
+ method: 'channel',
78
77
  values: [...channels]
79
78
  };
80
- const runtimeConfigs = this.withDefaultFormatterConfig(this.runtimeConfigs);
81
79
  channels.forEach(channel => {
82
- this.drivers.push(DriverFactory.fabricate(channel, runtimeConfigs));
80
+ const driver = DriverFactory.fabricate(channel, this.runtimeConfigs);
81
+ this.drivers.push(driver);
83
82
  });
84
83
  return this;
85
84
  }
@@ -91,15 +90,19 @@ export class Logger extends Macroable {
91
90
  vanilla(...configs) {
92
91
  this.drivers = [];
93
92
  this.selection = {
94
- type: 'vanilla',
93
+ method: 'vanilla',
95
94
  values: Json.copy(configs)
96
95
  };
97
96
  if (!configs.length) {
98
- this.drivers.push(DriverFactory.fabricateVanilla(this.withDefaultFormatterConfig()));
97
+ this.drivers.push(DriverFactory.fabricateVanilla(this.runtimeConfigs));
99
98
  return this;
100
99
  }
101
100
  configs.forEach(config => {
102
- this.drivers.push(DriverFactory.fabricateVanilla(this.withDefaultFormatterConfig(config)));
101
+ const driver = DriverFactory.fabricateVanilla({
102
+ ...config,
103
+ ...this.runtimeConfigs
104
+ });
105
+ this.drivers.push(driver);
103
106
  });
104
107
  return this;
105
108
  }
@@ -175,22 +178,9 @@ export class Logger extends Macroable {
175
178
  return Promise.all(promises);
176
179
  }
177
180
  /**
178
- * Attach message defaults to formatter configs so
179
- * structured formatters can enrich the output.
181
+ * Rebuild drivers using the current logger selection.
180
182
  */
181
- withDefaultFormatterConfig(configs = {}) {
182
- if (!Object.keys(this.messageDefaults).length) {
183
- return configs;
184
- }
185
- return {
186
- ...configs,
187
- formatterConfig: {
188
- ...(configs.formatterConfig || {}),
189
- defaults: {
190
- ...((configs.formatterConfig || {}).defaults || {}),
191
- ...this.messageDefaults
192
- }
193
- }
194
- };
183
+ applySelection() {
184
+ return this[this.selection.method](...Json.copy(this.selection.values));
195
185
  }
196
186
  }