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/dist/src/modules/logger/pino/constants.d.ts +1 -0
- package/dist/src/modules/logger/pino/constants.js +5 -0
- package/dist/src/modules/logger/pino/constants.js.map +1 -0
- package/dist/src/modules/logger/pino/logger.module.d.ts +12 -0
- package/dist/src/modules/logger/pino/logger.module.js +64 -52
- package/dist/src/modules/logger/pino/logger.module.js.map +1 -1
- package/dist/src/modules/logger/pino/logger.service.d.ts +3 -1
- package/dist/src/modules/logger/pino/logger.service.js +14 -8
- package/dist/src/modules/logger/pino/logger.service.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +18 -17
- package/src/modules/logger/pino/constants.ts +1 -0
- package/src/modules/logger/pino/logger.module.ts +91 -67
- package/src/modules/logger/pino/logger.service.ts +13 -8
package/package.json
CHANGED
@@ -1,11 +1,27 @@
|
|
1
1
|
{
|
2
2
|
"name": "xcally-nest-library",
|
3
|
-
"version": "0.0.
|
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
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
48
|
-
//
|
49
|
-
//
|
50
|
-
//
|
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
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
67
|
-
|
68
|
-
|
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(
|
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
|
}
|