xcally-nest-library 0.0.32 → 0.0.33

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.32",
3
+ "version": "0.0.33",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -0,0 +1 @@
1
+ export const LOGGER_OPTIONS = 'LOGGER_OPTIONS';
@@ -1,76 +1,98 @@
1
- import { Global, Module } from '@nestjs/common';
1
+ import { DynamicModule, Global, Module } from '@nestjs/common';
2
2
  import { ConfigModule, ConfigService } from '@nestjs/config';
3
3
  import { LoggerModule as LoggerModulePino } from 'nestjs-pino';
4
4
  import { LoggerService } from './logger.service';
5
+ import { LOGGER_OPTIONS } from './constants';
6
+
7
+ export interface LoggerModuleOptions {
8
+ 'resource.service.name': string;
9
+ 'resource.service.namespace': string;
10
+ 'resource.service.version'?: string;
11
+ 'resource.service.environment'?: string;
12
+ 'resource.service.deployment.environment'?: string;
13
+ 'resource.service.deployment.id'?: string;
14
+ 'resource.service.deployment.revision'?: string;
15
+ }
5
16
 
6
17
  @Global()
7
- @Module({
8
- providers: [LoggerService],
9
- exports: [LoggerService],
10
- imports: [
11
- LoggerModulePino.forRootAsync({
12
- imports: [ConfigModule],
13
- inject: [ConfigService],
14
- useFactory: async (config: ConfigService) => {
15
- const projectName = process.env.npm_package_name || 'default_project';
16
- return {
17
- pinoHttp: {
18
- customLogLevel: (req, res) => {
19
- if (res.statusCode >= 500) return 'error';
20
- if (res.statusCode >= 400) return 'warn';
21
- return 'info';
22
- },
23
- level: config.get('LOG_LEVEL') || 'info',
24
- // customErrorMessage?: ((req: IM, res: SR, error: Error) => string) | undefined;
25
- // customErrorMessage: (req, res, error) => {
26
- // return 'Auto generated error' + error?.message;
27
- // },
28
- // customSuccessMessage(req, res, responseTime) {
29
- // return 'Auto generated success';
30
- // },
31
- // autoLogging: true,
32
- // level: config.get('LOG_LEVEL') || 'info',
33
- // stream: pino.destination(`./logs/info.log`),
34
- redact: {
35
- paths: ['req.headers.cookie', 'req.headers.authorization'],
36
- },
37
- transport: {
38
- targets: [
39
- {
40
- level: 'debug',
41
- target: 'pino-pretty',
42
- options: {
43
- colorize: true,
44
- },
18
+ export class LoggerModule {
19
+ static forRoot(options: LoggerModuleOptions): DynamicModule {
20
+ const loggerOptions = {
21
+ provide: LOGGER_OPTIONS,
22
+ useValue: options,
23
+ };
24
+
25
+ const providers = [LoggerService, loggerOptions];
26
+ const exports = [LoggerService, loggerOptions];
27
+ return {
28
+ module: LoggerModule,
29
+ providers,
30
+ exports,
31
+ imports: [
32
+ LoggerModulePino.forRootAsync({
33
+ imports: [ConfigModule],
34
+ inject: [ConfigService],
35
+ useFactory: async (config: ConfigService) => {
36
+ const projectName = process.env.npm_package_name || 'default_project';
37
+ return {
38
+ pinoHttp: {
39
+ customLogLevel: (req, res) => {
40
+ if (res.statusCode >= 500) return 'error';
41
+ if (res.statusCode >= 400) return 'warn';
42
+ return 'info';
45
43
  },
46
- // {
47
- // level: 'error',
48
- // target: 'pino/file',
49
- // options: {
50
- // destination: './logs/error.log',
51
- // },
44
+ level: config.get('LOG_LEVEL') || 'info',
45
+ // customErrorMessage?: ((req: IM, res: SR, error: Error) => string) | undefined;
46
+ // customErrorMessage: (req, res, error) => {
47
+ // return 'Auto generated error' + error?.message;
48
+ // },
49
+ // customSuccessMessage(req, res, responseTime) {
50
+ // return 'Auto generated success';
52
51
  // },
53
- {
54
- level: config.get('LOG_LEVEL') || 'info',
55
- target: 'pino/file',
56
- options: {
57
- destination: process.env.LOG_OUT_DIR
58
- ? process.env.LOG_OUT_DIR + `${projectName}.log`
59
- : `./logs/${projectName}.log`,
60
- mkdir: true,
61
- append: process.env.NODE_ENV !== 'production' ? false : true,
62
- },
52
+ // autoLogging: true,
53
+ // level: config.get('LOG_LEVEL') || 'info',
54
+ // stream: pino.destination(`./logs/info.log`),
55
+ redact: {
56
+ paths: ['req.headers.cookie', 'req.headers.authorization'],
63
57
  },
64
- ],
65
- },
66
- // genReqId: () => { // Non't needed with pino instrumentation
67
- // const activeSpan = api.trace.getSpan(api.context.active());
68
- // return activeSpan.spanContext().traceId;
69
- // },
58
+ transport: {
59
+ targets: [
60
+ {
61
+ level: 'debug',
62
+ target: 'pino-pretty',
63
+ options: {
64
+ colorize: true,
65
+ },
66
+ },
67
+ // {
68
+ // level: 'error',
69
+ // target: 'pino/file',
70
+ // options: {
71
+ // destination: './logs/error.log',
72
+ // },
73
+ // },
74
+ {
75
+ level: config.get('LOG_LEVEL') || 'info',
76
+ target: 'pino/file',
77
+ options: {
78
+ destination: process.env.LOG_OUT_DIR
79
+ ? process.env.LOG_OUT_DIR + `${projectName}.log`
80
+ : `./logs/${projectName}.log`,
81
+ mkdir: true,
82
+ append: process.env.NODE_ENV !== 'production' ? false : true,
83
+ },
84
+ },
85
+ ],
86
+ },
87
+ // genReqId: () => { // Non't needed with pino instrumentation
88
+ // const activeSpan = api.trace.getSpan(api.context.active());
89
+ // return activeSpan.spanContext().traceId;
90
+ // },
91
+ },
92
+ };
70
93
  },
71
- };
72
- },
73
- }),
74
- ],
75
- })
76
- export class LoggerModule {}
94
+ }),
95
+ ],
96
+ };
97
+ }
98
+ }
@@ -1,5 +1,7 @@
1
- import { Injectable } from '@nestjs/common';
1
+ import { Inject, Injectable } from '@nestjs/common';
2
2
  import { PinoLogger } from 'nestjs-pino';
3
+ import { LOGGER_OPTIONS } from './constants';
4
+ import { LoggerModuleOptions } from './logger.module';
3
5
  /**
4
6
  * @interface LoggerMeta
5
7
  * @description Interface representing metadata for logging purposes.
@@ -12,19 +14,22 @@ interface LoggerMeta {
12
14
  }
13
15
  @Injectable()
14
16
  export class LoggerService {
15
- constructor(private readonly logger: PinoLogger) {}
17
+ constructor(
18
+ @Inject(LOGGER_OPTIONS) private readonly loggerOptions: LoggerModuleOptions,
19
+ private readonly logger: PinoLogger,
20
+ ) {}
16
21
 
17
22
  debug(msg: string, meta?: LoggerMeta): void {
18
- this.logger.debug({ meta }, msg);
23
+ this.logger.debug({ meta, ...this.loggerOptions }, msg);
19
24
  }
20
25
  warn(msg: string, meta?: LoggerMeta): void {
21
- this.logger.warn({ meta }, msg);
26
+ this.logger.warn({ meta, ...this.loggerOptions }, msg);
22
27
  }
23
28
  error(msg: string, meta?: LoggerMeta): void {
24
- this.logger.error({ meta }, msg);
29
+ this.logger.error({ meta, ...this.loggerOptions }, msg);
25
30
  }
26
31
  fatal(msg: string, meta?: LoggerMeta): void {
27
- this.logger.fatal({ meta }, msg);
32
+ this.logger.fatal({ meta, ...this.loggerOptions }, msg);
28
33
  }
29
34
 
30
35
  info(msg: string, meta?: LoggerMeta): void {
@@ -37,10 +42,10 @@ export class LoggerService {
37
42
  // span.end();
38
43
  // });
39
44
 
40
- this.logger.info({ meta }, msg);
45
+ this.logger.info({ meta, ...this.loggerOptions }, msg);
41
46
  }
42
47
 
43
48
  trace(msg: string, meta?: LoggerMeta): void {
44
- this.logger.trace({ meta }, msg);
49
+ this.logger.trace({ meta, ...this.loggerOptions }, msg);
45
50
  }
46
51
  }