xcally-nest-library 0.0.33 → 0.0.35

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": "xcally-nest-library",
3
- "version": "0.0.33",
3
+ "version": "0.0.35",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -3,6 +3,7 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
3
3
  import { LoggerModule as LoggerModulePino } from 'nestjs-pino';
4
4
  import { LoggerService } from './logger.service';
5
5
  import { LOGGER_OPTIONS } from './constants';
6
+ import { ClsModule } from 'nestjs-cls';
6
7
 
7
8
  export interface LoggerModuleOptions {
8
9
  'resource.service.name': string;
@@ -12,6 +13,7 @@ export interface LoggerModuleOptions {
12
13
  'resource.service.deployment.environment'?: string;
13
14
  'resource.service.deployment.id'?: string;
14
15
  'resource.service.deployment.revision'?: string;
16
+ autoLogging?: boolean;
15
17
  }
16
18
 
17
19
  @Global()
@@ -29,6 +31,7 @@ export class LoggerModule {
29
31
  providers,
30
32
  exports,
31
33
  imports: [
34
+ ClsModule,
32
35
  LoggerModulePino.forRootAsync({
33
36
  imports: [ConfigModule],
34
37
  inject: [ConfigService],
@@ -42,6 +45,7 @@ export class LoggerModule {
42
45
  return 'info';
43
46
  },
44
47
  level: config.get('LOG_LEVEL') || 'info',
48
+ autoLogging: options.autoLogging || true,
45
49
  // customErrorMessage?: ((req: IM, res: SR, error: Error) => string) | undefined;
46
50
  // customErrorMessage: (req, res, error) => {
47
51
  // return 'Auto generated error' + error?.message;
@@ -2,6 +2,7 @@ import { Inject, Injectable } from '@nestjs/common';
2
2
  import { PinoLogger } from 'nestjs-pino';
3
3
  import { LOGGER_OPTIONS } from './constants';
4
4
  import { LoggerModuleOptions } from './logger.module';
5
+ import { ClsService } from 'nestjs-cls';
5
6
  /**
6
7
  * @interface LoggerMeta
7
8
  * @description Interface representing metadata for logging purposes.
@@ -17,19 +18,26 @@ export class LoggerService {
17
18
  constructor(
18
19
  @Inject(LOGGER_OPTIONS) private readonly loggerOptions: LoggerModuleOptions,
19
20
  private readonly logger: PinoLogger,
21
+ private readonly cls: ClsService,
20
22
  ) {}
21
23
 
24
+ private withAdditionalTraceId(meta?: LoggerMeta) {
25
+ const xcTraceId: string | undefined = this.cls.get('xcTraceId');
26
+ const xcActionId: string | undefined = this.cls.get('xcActionId');
27
+ return { ...meta, xcTraceId ,xcActionId};
28
+ }
29
+
22
30
  debug(msg: string, meta?: LoggerMeta): void {
23
- this.logger.debug({ meta, ...this.loggerOptions }, msg);
31
+ this.logger.debug({ meta: this.withAdditionalTraceId(meta), ...this.loggerOptions }, msg);
24
32
  }
25
33
  warn(msg: string, meta?: LoggerMeta): void {
26
- this.logger.warn({ meta, ...this.loggerOptions }, msg);
34
+ this.logger.warn({ meta: this.withAdditionalTraceId(meta), ...this.loggerOptions }, msg);
27
35
  }
28
36
  error(msg: string, meta?: LoggerMeta): void {
29
- this.logger.error({ meta, ...this.loggerOptions }, msg);
37
+ this.logger.error({ meta: this.withAdditionalTraceId(meta), ...this.loggerOptions }, msg);
30
38
  }
31
39
  fatal(msg: string, meta?: LoggerMeta): void {
32
- this.logger.fatal({ meta, ...this.loggerOptions }, msg);
40
+ this.logger.fatal({ meta: this.withAdditionalTraceId(meta), ...this.loggerOptions }, msg);
33
41
  }
34
42
 
35
43
  info(msg: string, meta?: LoggerMeta): void {
@@ -42,10 +50,23 @@ export class LoggerService {
42
50
  // span.end();
43
51
  // });
44
52
 
45
- this.logger.info({ meta, ...this.loggerOptions }, msg);
53
+ this.logger.info({ meta: this.withAdditionalTraceId(meta), ...this.loggerOptions }, msg);
46
54
  }
47
55
 
48
56
  trace(msg: string, meta?: LoggerMeta): void {
49
- this.logger.trace({ meta, ...this.loggerOptions }, msg);
57
+ this.logger.trace({ meta: this.withAdditionalTraceId(meta), ...this.loggerOptions }, msg);
58
+ }
59
+
60
+ // Esempio di uso corretto:
61
+ someMethod() {
62
+ this.cls.run(() => {
63
+ this.cls.set('xcTraceId', 'my-trace-id');
64
+ // Tutto il codice asincrono che parte da qui vedrà il valore
65
+ this.logger.info('Log con trace id'); // Qui funziona
66
+ setTimeout(() => {
67
+ this.logger.info('Anche qui funziona');
68
+ }, 100);
69
+ });
70
+ // Qui fuori il valore NON è disponibile
50
71
  }
51
72
  }