Skip to content

Commit b42c5ad

Browse files
committed
refactor: optimize pinoLoggerAdapter
1 parent 2b70994 commit b42c5ad

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
11
import type pino from 'pino';
22
import type { DomainLogger } from '@domain/service/shared/logger.js';
3-
import { getRequestLogger } from '@infrastructure/logging/index.js';
3+
import { getLogger } from '@infrastructure/logging/index.js';
4+
import { getCurrentReqId } from '@infrastructure/logging/reqId.context.js';
45

56
/**
67
* Adapter that wraps a pino.Logger to implement the DomainLogger interface
78
* This allows the infrastructure's pino logger to be injected into domain services
89
* without the domain layer depending directly on pino
910
*/
1011
export class PinoDomainLoggerAdapter implements DomainLogger {
12+
private readonly baseLogger: pino.Logger;
13+
14+
constructor() {
15+
this.baseLogger = getLogger('domain');
16+
}
17+
1118
public debug(msg: string, meta?: Record<string, unknown>): void {
12-
this.getReqLogger().debug({
13-
msg,
14-
...meta,
15-
});
19+
const reqId = getCurrentReqId();
20+
21+
this.baseLogger.debug({ ...meta,
22+
reqId }, msg);
1623
}
1724

1825
public info(msg: string, meta?: Record<string, unknown>): void {
19-
this.getReqLogger().info({
20-
msg,
21-
...meta,
22-
});
26+
const reqId = getCurrentReqId();
27+
28+
this.baseLogger.info({ ...meta,
29+
reqId }, msg);
2330
}
2431

2532
public warn(msg: string, meta?: Record<string, unknown>): void {
26-
this.getReqLogger().warn({
27-
msg,
28-
...meta,
29-
});
30-
}
33+
const reqId = getCurrentReqId();
3134

32-
private getReqLogger(): pino.Logger {
33-
return getRequestLogger('domain');
35+
this.baseLogger.warn({ ...meta,
36+
reqId }, msg);
3437
}
3538
}

0 commit comments

Comments
 (0)