Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call logtail.flush() when LogtailTransport (winston) is closed #118

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
});
});
11 changes: 10 additions & 1 deletion packages/winston/src/winston.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ 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
Loading