Skip to content

Commit

Permalink
fix: logger
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Nov 22, 2021
1 parent 7f25006 commit df98e31
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/util/src/Logger/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type SafeLog = (log: LogMessage) => void;
const pid = process.pid;
const host = os.hostname();
const writers: SafeLog[] = [];
const writing = new Set<Promise<unknown>>();
const transports: ILoggerTransport[] = [];
const loggers = new WeakMap<LoggerOwner, Logger>();

Expand Down Expand Up @@ -75,7 +76,10 @@ export class Logger {
transports.push(transport);
const safeFn = makeSafe(async (log: LogMessage) => {
if (transport.writable) {
await transport.write(log);
const pending = Promise.resolve(transport.write(log));
writing.add(pending);

await pending.finally(() => writing.delete(pending));
}
});

Expand Down Expand Up @@ -192,12 +196,16 @@ export class Logger {
await this.reset();
}

private static reset(): Promise<PromiseSettledResult<Promisify<void>>[]> {
private static reset(): Promise<PromiseSettledResult<Promisify<unknown>>[]> {
writers.splice(0, writers.length);

return Promise.allSettled(
transports
.splice(0, transports.length)
.map((transport) => transport.close()),
[
...writing.values(),
...transports
.splice(0, transports.length)
.map((transport) => transport.close()),
],
);
}

Expand Down

0 comments on commit df98e31

Please sign in to comment.