Skip to content

Commit 4fef03f

Browse files
authored
fix: prototypes added to exported logger are not accepted (#7)
1 parent 6d5fd71 commit 4fef03f

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/instrumentation.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,16 @@ export class BunyanInstrumentation extends InstrumentationBase {
8585
inherits(LoggerTraced, Logger);
8686

8787
const patchedExports = Object.assign(LoggerTraced, Logger);
88+
// Ensure patchedExports.prototype points to LoggerTraced.prototype
89+
// so that prototype modifications (like bunyan.prototype.monitor = ...)
90+
// work correctly on logger instances
91+
patchedExports.prototype = LoggerTraced.prototype;
8892

8993
this._wrap(
9094
patchedExports,
9195
'createLogger',
9296
// eslint-disable-next-line @typescript-eslint/no-explicit-any
93-
this._getPatchedCreateLogger() as any
97+
this._getPatchedCreateLogger(LoggerTraced.prototype) as any
9498
);
9599

96100
return patchedExports;
@@ -138,11 +142,14 @@ export class BunyanInstrumentation extends InstrumentationBase {
138142
};
139143
}
140144

141-
private _getPatchedCreateLogger() {
145+
private _getPatchedCreateLogger(loggerTracedPrototype: any) {
142146
return (original: (...args: unknown[]) => void) => {
143147
const instrumentation = this;
144148
return function patchedCreateLogger(...args: unknown[]) {
145149
const logger = original(...args);
150+
// Ensure logger instances inherit from LoggerTraced.prototype so that
151+
// prototype modifications (like bunyan.prototype.monitor = ...) work correctly
152+
Object.setPrototypeOf(logger, loggerTracedPrototype);
146153
instrumentation._addStream(logger);
147154
return logger;
148155
};

0 commit comments

Comments
 (0)