Skip to content

Commit 8596447

Browse files
committed
chore: add caching for getLogger
1 parent 75ffe66 commit 8596447

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/infrastructure/logging/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@ const loggerConfig = process.env['NODE_ENV'] === 'production'
1717

1818
const rootLogger = pino(loggerConfig);
1919

20+
let loggerCache = new Map<keyof LoggingConfig, pino.Logger>();
21+
2022
/**
2123
* Creates child logger and returns it.
2224
* @param moduleName - name of the module that is logging
2325
*/
2426
export function getLogger(moduleName: keyof LoggingConfig): pino.Logger {
27+
const cachedLogger = loggerCache.get(moduleName);
28+
29+
if (cachedLogger) {
30+
return cachedLogger;
31+
}
32+
2533
const childLogger = rootLogger.child({
2634
module: moduleName,
2735
});
@@ -34,6 +42,8 @@ export function getLogger(moduleName: keyof LoggingConfig): pino.Logger {
3442

3543
childLogger.level = logLevel;
3644

45+
loggerCache.set(moduleName, childLogger);
46+
3747
return childLogger;
3848
}
3949

0 commit comments

Comments
 (0)