Skip to content

Commit 84ff1e8

Browse files
committed
fix: refactoring
1 parent 54f199b commit 84ff1e8

9 files changed

+102
-44
lines changed

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.node-version
44
.editorconfig
55
*.tsbuildinfo
6+
package-lock.json

README.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ import pinoHttpConfig from "./config/pino-http.config";
4141
load: [pinoConfig, pinoHttpConfig],
4242
// ... any other configuration options you want to use
4343
}),
44-
LoggerModule
44+
LoggerModule.forRootAsync({
45+
imports: [ConfigModule],
46+
useFactory: (configService: ConfigService) => configService.get<LoggerOptions>('pino'),
47+
inject: [ConfigService],
48+
}),
4549
]
4650
})
4751
class AppModule {}
@@ -51,17 +55,17 @@ then you should inject `Logger` into your main app:
5155

5256
```typescript
5357
// main.ts
54-
import { Logger } from '@zemd/nestjs-pino-logger';
58+
import {Logger, PINO_LOGGER_INSTANCE} from '@zemd/nestjs-pino-logger';
5559
import pinoHttp from "pino-http";
56-
import type { Options } from 'pino-http';
60+
import type {Options} from 'pino-http';
5761

58-
const app = await NestFactory.create(AppModule, { bufferLogs: true });
62+
const app = await NestFactory.create(AppModule, {bufferLogs: true});
5963
app.useLogger(app.get(Logger));
6064

6165
// if you want to use pino-http ↓
6266
app.use(pinoHttp({
63-
...configService.get<Options>('pino-http'),
64-
logger: app.get(Logger).getPinoInstance(),
67+
...configService.get<Options>('pino-http'),
68+
logger: app.get(PINO_LOGGER_INSTANCE),
6569
}));
6670
```
6771

@@ -150,7 +154,11 @@ In case if you want extend pino log object by adding more fields, you can use `b
150154
It looks like:
151155

152156
```typescript
153-
const message = buildPinoMessage('Hello World %o', { foo: 'bar' }, { data: 'the data object will be used to format the message' });
157+
const message = buildPinoMessage({
158+
message: 'Hello World %o',
159+
mergingObject: { foo: 'bar' },
160+
interpolationValues: [{ data: 'the data object will be used to format the message' }]
161+
});
154162
this.logger.log(message, 'here you can also pass something that will be added to the msg string');
155163
```
156164

src/Logger.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import {Injectable, LoggerService} from "@nestjs/common";
22
import * as util from "node:util";
3-
import pino from "pino";
4-
import {ConfigService} from "@nestjs/config";
3+
import type {Logger as PinoLogger} from "pino";
54
import crypto from "node:crypto";
6-
import {PinoMessageSymbol} from "./PinoMessageSymbol";
5+
import {PinoMessageSymbol} from "./logger.constants";
76

87
@Injectable()
98
export class Logger implements LoggerService {
10-
private readonly pinoInstance: any;
119
private static cache = new Map<string, any>();
1210

13-
constructor(private readonly configService: ConfigService) {
14-
this.pinoInstance = pino(this.configService.get('pino'));
11+
constructor(private readonly pinoInstance: PinoLogger) {
1512
}
1613

1714
debug(message: any, ...optionalParams: any[]): any {
@@ -83,8 +80,4 @@ export class Logger implements LoggerService {
8380
Logger.cache.set(ctx, {inst, timeout});
8481
return inst;
8582
}
86-
87-
public getPinoInstance() {
88-
return this.pinoInstance;
89-
}
9083
}

src/PinoMessageSymbol.ts

-1
This file was deleted.

src/buildPinoMessage.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import {PinoMessageSymbol} from "./PinoMessageSymbol";
1+
import {PinoMessageSymbol} from "./logger.constants";
22

3-
export const buildPinoMessage = (
4-
message: string,
5-
mergingObject?: Record<string, any>,
6-
...interpolationValues: any[]
7-
) => {
3+
export type PinoMessageType = {
4+
message: string;
5+
mergingObject?: Record<string, any>;
6+
interpolationValues?: any[];
7+
};
8+
9+
export const buildPinoMessage = (message: PinoMessageType) => {
810
return {
911
[PinoMessageSymbol]: true,
10-
mergingObject,
11-
message,
12-
interpolationValues,
12+
mergingObject: message.mergingObject,
13+
message: message.message,
14+
interpolationValues: message.interpolationValues
1315
};
1416
}

src/config.ts

-7
This file was deleted.

src/index.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export {LoggerModule} from "./logger.module";
22
export {Logger} from "./Logger";
3-
export {PinoMessageSymbol} from "./PinoMessageSymbol";
43
export {buildPinoMessage} from "./buildPinoMessage";
5-
export {customLevels} from "./config";
6-
export {default as PinoPrettyTransport} from "./pino-pretty-transport";
4+
export {default as PinoPrettyTransport} from "./pino-pretty-transport";
5+
export * from "./logger.constants";

src/logger.constants.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const PinoMessageSymbol = Symbol('__PinoMessageSymbol');
2+
export const PINO_LOGGER_OPTIONS = 'PinoLoggerOptions';
3+
export const PINO_LOGGER_INSTANCE = 'PinoLoggerInstance';
4+
export const customLevels = {
5+
error: 50,
6+
warn: 40,
7+
log: 30,
8+
debug: 20,
9+
verbose: 10,
10+
} as const;

src/logger.module.ts

+60-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,65 @@
1-
import {Global, Module, NestModule} from "@nestjs/common";
1+
import {DynamicModule, Global, Module, ModuleMetadata, Provider} from "@nestjs/common";
22
import {Logger} from "./Logger";
3+
import {PINO_LOGGER_INSTANCE, PINO_LOGGER_OPTIONS} from "./logger.constants";
4+
import type {LoggerOptions} from "pino";
5+
import pino from "pino";
6+
7+
export interface LoggerModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
8+
inject?: any[];
9+
useFactory?: (...args: any[]) => Promise<LoggerOptions> | LoggerOptions;
10+
}
311

412
@Global()
5-
@Module({
6-
providers: [Logger],
7-
exports: [Logger],
8-
})
9-
export class LoggerModule implements NestModule {
10-
configure(): any {
13+
@Module({})
14+
export class LoggerModule {
15+
constructor() {
16+
}
17+
18+
static forRoot(options: LoggerOptions): DynamicModule {
19+
const loggerModuleOptions = {
20+
provide: PINO_LOGGER_OPTIONS,
21+
useValue: options,
22+
};
23+
24+
const pinoProvider: Provider = {
25+
provide: PINO_LOGGER_INSTANCE,
26+
useFactory: () => {
27+
return pino(options);
28+
},
29+
};
30+
31+
return {
32+
module: LoggerModule,
33+
providers: [loggerModuleOptions, pinoProvider, Logger],
34+
exports: [loggerModuleOptions, pinoProvider, Logger]
35+
};
36+
}
37+
38+
static forRootAsync(options: LoggerModuleAsyncOptions): DynamicModule {
39+
const providers: Provider[] = [
40+
Logger
41+
];
42+
43+
const pinoProvider: Provider = {
44+
provide: PINO_LOGGER_INSTANCE,
45+
useFactory: async (options: LoggerOptions) => pino(options),
46+
inject: [PINO_LOGGER_OPTIONS],
47+
}
48+
providers.push(pinoProvider);
49+
50+
if (options.useFactory) {
51+
providers.push({
52+
provide: PINO_LOGGER_OPTIONS,
53+
useFactory: options.useFactory,
54+
inject: options.inject || [],
55+
});
56+
}
57+
58+
return {
59+
module: LoggerModule,
60+
imports: options.imports ?? [],
61+
providers,
62+
exports: [pinoProvider, Logger]
63+
};
1164
}
1265
}

0 commit comments

Comments
 (0)