xcally-nest-library 0.0.32 → 0.0.34

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,11 +1,27 @@
1
1
  {
2
2
  "name": "xcally-nest-library",
3
- "version": "0.0.32",
3
+ "version": "0.0.34",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "author": "",
8
8
  "license": "UNLICENSED",
9
+ "scripts": {
10
+ "bump:patch": "npm version patch -m \"Bump version to %s\"",
11
+ "prepublishOnly": "npm run build",
12
+ "build": "tsc",
13
+ "build:watch": "rimraf dist && tsc-watch -b -v",
14
+ "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
15
+ "prettier:check": "prettier --check ./**/*.{ts,js,json,*rc}",
16
+ "prettier:write": "prettier --write ./**/*.{ts,js,json,*rc}",
17
+ "start": "nest start",
18
+ "start:dev": "nest start --watch",
19
+ "start:debug": "nest start --debug --watch",
20
+ "start:prod": "node dist/main",
21
+ "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
22
+ "lib:link": "pnpm link --global",
23
+ "lib:unlink": "pnpm unlink xcally-nest-libs --global"
24
+ },
9
25
  "dependencies": {
10
26
  "@nestjs/config": "^4.0.0",
11
27
  "class-transformer": "^0.5.1",
@@ -37,20 +53,5 @@
37
53
  "@nestjs/common": "^10.0.0 || ^11.0.0",
38
54
  "@nestjs/core": "^10.0.0 || ^11.0.0",
39
55
  "@nestjs/microservices": "^10.0.0 || ^11.0.0"
40
- },
41
- "scripts": {
42
- "bump:patch": "npm version patch -m \"Bump version to %s\"",
43
- "build": "tsc",
44
- "build:watch": "rimraf dist && tsc-watch -b -v",
45
- "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
46
- "prettier:check": "prettier --check ./**/*.{ts,js,json,*rc}",
47
- "prettier:write": "prettier --write ./**/*.{ts,js,json,*rc}",
48
- "start": "nest start",
49
- "start:dev": "nest start --watch",
50
- "start:debug": "nest start --debug --watch",
51
- "start:prod": "node dist/main",
52
- "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
53
- "lib:link": "pnpm link --global",
54
- "lib:unlink": "pnpm unlink xcally-nest-libs --global"
55
56
  }
56
- }
57
+ }
@@ -0,0 +1 @@
1
+ export const LOGGER_OPTIONS = 'LOGGER_OPTIONS';
@@ -1,76 +1,100 @@
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
+ autoLogging?: boolean;
16
+ }
5
17
 
6
18
  @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
- },
19
+ export class LoggerModule {
20
+ static forRoot(options: LoggerModuleOptions): DynamicModule {
21
+ const loggerOptions = {
22
+ provide: LOGGER_OPTIONS,
23
+ useValue: options,
24
+ };
25
+
26
+ const providers = [LoggerService, loggerOptions];
27
+ const exports = [LoggerService, loggerOptions];
28
+ return {
29
+ module: LoggerModule,
30
+ providers,
31
+ exports,
32
+ imports: [
33
+ LoggerModulePino.forRootAsync({
34
+ imports: [ConfigModule],
35
+ inject: [ConfigService],
36
+ useFactory: async (config: ConfigService) => {
37
+ const projectName = process.env.npm_package_name || 'default_project';
38
+ return {
39
+ pinoHttp: {
40
+ customLogLevel: (req, res) => {
41
+ if (res.statusCode >= 500) return 'error';
42
+ if (res.statusCode >= 400) return 'warn';
43
+ return 'info';
45
44
  },
46
- // {
47
- // level: 'error',
48
- // target: 'pino/file',
49
- // options: {
50
- // destination: './logs/error.log',
51
- // },
45
+ level: config.get('LOG_LEVEL') || 'info',
46
+ autoLogging: options.autoLogging || true,
47
+ // customErrorMessage?: ((req: IM, res: SR, error: Error) => string) | undefined;
48
+ // customErrorMessage: (req, res, error) => {
49
+ // return 'Auto generated error' + error?.message;
50
+ // },
51
+ // customSuccessMessage(req, res, responseTime) {
52
+ // return 'Auto generated success';
52
53
  // },
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
- },
54
+ // autoLogging: true,
55
+ // level: config.get('LOG_LEVEL') || 'info',
56
+ // stream: pino.destination(`./logs/info.log`),
57
+ redact: {
58
+ paths: ['req.headers.cookie', 'req.headers.authorization'],
63
59
  },
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
- // },
60
+ transport: {
61
+ targets: [
62
+ {
63
+ level: 'debug',
64
+ target: 'pino-pretty',
65
+ options: {
66
+ colorize: true,
67
+ },
68
+ },
69
+ // {
70
+ // level: 'error',
71
+ // target: 'pino/file',
72
+ // options: {
73
+ // destination: './logs/error.log',
74
+ // },
75
+ // },
76
+ {
77
+ level: config.get('LOG_LEVEL') || 'info',
78
+ target: 'pino/file',
79
+ options: {
80
+ destination: process.env.LOG_OUT_DIR
81
+ ? process.env.LOG_OUT_DIR + `${projectName}.log`
82
+ : `./logs/${projectName}.log`,
83
+ mkdir: true,
84
+ append: process.env.NODE_ENV !== 'production' ? false : true,
85
+ },
86
+ },
87
+ ],
88
+ },
89
+ // genReqId: () => { // Non't needed with pino instrumentation
90
+ // const activeSpan = api.trace.getSpan(api.context.active());
91
+ // return activeSpan.spanContext().traceId;
92
+ // },
93
+ },
94
+ };
70
95
  },
71
- };
72
- },
73
- }),
74
- ],
75
- })
76
- export class LoggerModule {}
96
+ }),
97
+ ],
98
+ };
99
+ }
100
+ }
@@ -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
  }