xcally-nest-library 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
package/index.ts CHANGED
@@ -9,3 +9,4 @@ export * from './src/decorators/utils.decorators';
9
9
  export * from './src/config/env.validation';
10
10
  export * from './src/modules/logger/pino/logger.service';
11
11
  export * from './src/modules/logger/pino/logger.module';
12
+ export * from './src/modules/logger/pino/logger.default';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xcally-nest-library",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -44,6 +44,7 @@
44
44
  "nestjs-paginate": "^8.6.2",
45
45
  "nestjs-pino": "^4.1.0",
46
46
  "pino": "^9.4.0",
47
+ "pino-pretty": "^11.2.2",
47
48
  "reflect-metadata": "^0.2.2",
48
49
  "rxjs": "^7.8.1",
49
50
  "typeorm": "^0.3.20",
@@ -0,0 +1 @@
1
+ export { Logger } from 'nestjs-pino';
@@ -1,23 +1,73 @@
1
- import { Module } from '@nestjs/common';
2
- import { LoggerService } from './logger.service';
1
+ import { DynamicModule, Global, Module } from '@nestjs/common';
2
+ import { ConfigModule, ConfigService } from '@nestjs/config';
3
3
  import { LoggerModule as LoggerModulePino } from 'nestjs-pino';
4
- import api from '@opentelemetry/api';
4
+ import { LoggerService } from './logger.service';
5
5
 
6
- @Module({
7
- providers: [LoggerService],
8
- exports: [LoggerService],
9
- imports: [
10
- LoggerModulePino.forRoot({
11
- pinoHttp: {
12
- genReqId: () => {
13
- const activeSpan = api.trace.getSpan(api.context.active());
14
- return activeSpan.spanContext().traceId;
15
- },
16
- redact: {
17
- paths: ['req.headers.cookie', 'req.headers.authorization'],
18
- },
19
- },
20
- }),
21
- ],
22
- })
23
- export class LoggerModule {}
6
+ @Global()
7
+ @Module({})
8
+ export class LoggerModule {
9
+ static forRoot(): DynamicModule {
10
+ return {
11
+ module: LoggerModule,
12
+ providers: [LoggerService],
13
+ exports: [LoggerService],
14
+ imports: [
15
+ LoggerModulePino.forRootAsync({
16
+ imports: [ConfigModule],
17
+ inject: [ConfigService],
18
+ useFactory: async (config: ConfigService) => {
19
+ return {
20
+ pinoHttp: {
21
+ level: config.get('LOG_LEVEL') || 'info',
22
+ // customErrorMessage?: ((req: IM, res: SR, error: Error) => string) | undefined;
23
+ // customErrorMessage: (req, res, error) => {
24
+ // return 'Auto generated error' + error?.message;
25
+ // },
26
+ // customSuccessMessage(req, res, responseTime) {
27
+ // return 'Auto generated success';
28
+ // },
29
+ // autoLogging: true,
30
+ // level: config.get('LOG_LEVEL') || 'info',
31
+ // stream: pino.destination(`./logs/info.log`),
32
+ redact: {
33
+ paths: ['req.headers.cookie', 'req.headers.authorization'],
34
+ },
35
+ transport: {
36
+ targets: [
37
+ {
38
+ level: 'debug',
39
+ target: 'pino-pretty',
40
+ options: {
41
+ colorize: true,
42
+ },
43
+ },
44
+ // {
45
+ // level: 'error',
46
+ // target: 'pino/file',
47
+ // options: {
48
+ // destination: './logs/error.log',
49
+ // },
50
+ // },
51
+ {
52
+ level: 'info',
53
+ target: 'pino/file',
54
+ options: {
55
+ destination: './logs/info.log',
56
+ mkdir: true,
57
+ append: process.env.NODE_ENV !== 'production' ? false : true,
58
+ },
59
+ },
60
+ ],
61
+ },
62
+ // genReqId: () => { // Non't needed with pino instrumentation
63
+ // const activeSpan = api.trace.getSpan(api.context.active());
64
+ // return activeSpan.spanContext().traceId;
65
+ // },
66
+ },
67
+ };
68
+ },
69
+ }),
70
+ ],
71
+ };
72
+ }
73
+ }
@@ -1,43 +1,36 @@
1
1
  import { Injectable } from '@nestjs/common';
2
2
  import { PinoLogger } from 'nestjs-pino';
3
- import pino from 'pino';
4
3
  import api from '@opentelemetry/api';
5
4
 
6
- type PinoMethods = Pick<pino.Logger, 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal'>;
7
-
8
5
  @Injectable()
9
- export class LoggerService implements PinoMethods {
6
+ export class LoggerService {
10
7
  constructor(private readonly logger: PinoLogger) {}
11
- trace(msg: string, ...args: any[]): void {
12
- const activeSpan = api.trace.getSpan(api.context.active());
13
- activeSpan.addEvent(msg, ...args);
14
- this.logger.trace(msg, ...args);
15
- }
16
- debug(msg: string, ...args: any[]): void {
8
+
9
+ debug(msg: string, meta?: any): void {
17
10
  const activeSpan = api.trace.getSpan(api.context.active());
18
- activeSpan.addEvent(msg, ...args);
19
- this.logger.debug(msg, ...args);
11
+ activeSpan.addEvent(msg);
12
+ this.logger.debug({ meta }, msg);
20
13
  }
21
- warn(msg: string, ...args: any[]): void {
14
+ warn(msg: string, meta?: any): void {
22
15
  const activeSpan = api.trace.getSpan(api.context.active());
23
- activeSpan.addEvent(msg, ...args);
24
- this.logger.warn(msg, ...args);
16
+ activeSpan.addEvent(msg);
17
+ this.logger.warn({ meta }, msg);
25
18
  }
26
- error(msg: string, ...args: any[]): void {
19
+ error(msg: string, meta?: any): void {
27
20
  const activeSpan = api.trace.getSpan(api.context.active());
28
- activeSpan.addEvent(msg, ...args);
29
- this.logger.error(msg, ...args);
21
+ activeSpan.addEvent(msg);
22
+ this.logger.error({ meta }, msg);
30
23
  }
31
- fatal(msg: string, ...args: any[]): void {
24
+ fatal(msg: string, meta?: any): void {
32
25
  const activeSpan = api.trace.getSpan(api.context.active());
33
- activeSpan.addEvent(msg, ...args);
34
- this.logger.fatal(msg, ...args);
26
+ activeSpan.addEvent(msg);
27
+ this.logger.fatal({ meta }, msg);
35
28
  }
36
29
 
37
- info(msg: string, ...args: any[]): void {
30
+ info(msg: string, meta?: any): void {
38
31
  // LOG EVENTS
39
32
  const activeSpan = api.trace.getSpan(api.context.active());
40
- activeSpan.addEvent(msg, ...args);
33
+ activeSpan.addEvent(msg);
41
34
 
42
35
  // // LOG CORRELATION -- You can see correlation between spans and logs
43
36
  // const tracer = api.trace.getTracer(process.env.npm_package_name, process.env.npm_package_version);
@@ -46,14 +39,6 @@ export class LoggerService implements PinoMethods {
46
39
  // span.end();
47
40
  // });
48
41
 
49
- this.logger.info(msg, ...args);
50
- }
51
-
52
- log(msg: string, ...args: any[]): void {
53
- const activeSpan = api.trace.getSpan(api.context.active());
54
- if (activeSpan) {
55
- activeSpan.addEvent(msg, ...args);
56
- }
57
- this.logger.info(msg, ...args);
42
+ this.logger.info({ meta }, msg);
58
43
  }
59
44
  }