Skip to content

Commit

Permalink
Call logtail.flush() when winston transport is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
mutaflux committed Jul 31, 2024
1 parent e3d1caf commit c29758f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
32 changes: 32 additions & 0 deletions packages/winston/src/winston.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,36 @@ describe("Winston logging tests", () => {
const context = logs[0].context;
expect(context.runtime.file).toMatch("winston.test.ts");
});

it("should flush logtail when the logger is closed", async () => {
let logs: ILogtailLog[] = [];

const logtail = new Logtail("test", { throwExceptions: true });

logtail.setSync(async (_logs: ILogtailLog[]) => {
logs.push(..._logs);
return logs;
});

const logger = winston.createLogger({
level: LogLevel.Info,
transports: [new LogtailTransport(logtail)],
});

const finished = new Promise<void>(resolve => {
logger.on('finish', resolve);
});

// Act
logger.info("a test message");
logger.end();

await finished;

// Should be exactly one log
expect(logs.length).toBe(1);

// Message should match
expect(logs[0].message).toBe("a test message");
});
});
12 changes: 11 additions & 1 deletion packages/winston/src/winston.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ const stackContextHint = {

export class LogtailTransport extends Transport {
public constructor(private _logtail: Logtail, opts?: Transport.TransportStreamOptions) {
super(opts);
super({
...opts,
close: () => {
this._logtail.flush()
.then(() => {
if (opts?.close) {
opts.close();
}
});
},
});
}

public log(info: LogEntry, cb: Function) {
Expand Down

0 comments on commit c29758f

Please sign in to comment.