@@ -591,7 +591,8 @@ log.info({ some: 'attributes' }, 'hello world')
591
591
592
592
You can implement custom log writers for the Hive Logger by creating a class that implements the
593
593
` LogWriter` interface. This interface requires a single ` write` method, which receives the log
594
- level, attributes, and message.
594
+ level, attributes, and message and an optional ` flush` method allowing you to ensure all writer jobs
595
+ are completed when the logger is flushed.
595
596
596
597
Your writer can perform any action, such as sending logs to a file, external service, or custom
597
598
destination.
@@ -600,6 +601,24 @@ Writers can be synchronous (returning `void`) or asynchronous (returning a `Prom
600
601
writer performs asynchronous operations (like network requests or file writes), simply return a
601
602
promise from the ` write` method.
602
603
604
+ Furthermore, you can optionally implement the ` flush` method to ensure that all pending writes are
605
+ completed before the logger is disposed or flushed. This is particularly useful for asynchronous
606
+ writers that need to ensure all logs are written before the application exits or the logger is no
607
+ longer needed.
608
+
609
+ ` ` ` ts
610
+ import { Attributes, LogLevel } from ' @graphql-hive/logger'
611
+
612
+ interface LogWriter {
613
+ write(
614
+ level: LogLevel,
615
+ attrs: Attributes | null | undefined,
616
+ msg: string | null | undefined
617
+ ): void | Promise< void>
618
+ flush? (): void | Promise< void>
619
+ }
620
+ ` ` `
621
+
603
622
# ### Example of HTTP Writer
604
623
605
624
` ` ` ts
@@ -658,6 +677,9 @@ writers are async.
658
677
This design allows you to use async writers without impacting the performance of your application or
659
678
blocking the main thread.
660
679
680
+ After all writes have been completed, the logger will call the optional ` flush` method on the
681
+ writers, executing any custom finalization logic you may have implemented.
682
+
661
683
# #### Explicit Resource Management
662
684
663
685
The Hive Logger also supports
0 commit comments