Skip to content

Commit e7b8cbb

Browse files
committed
logger writer flush
1 parent 6daa19f commit e7b8cbb

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

packages/web/docs/src/content/gateway/logging-and-error-handling.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ interface LogWriter {
275275
attrs: Attributes | null | undefined,
276276
msg: string | null | undefined
277277
): void | Promise<void>
278+
flush?(): void | Promise<void>
278279
}
279280
```
280281

packages/web/docs/src/content/logger.mdx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,8 @@ log.info({ some: 'attributes' }, 'hello world')
591591
592592
You can implement custom log writers for the Hive Logger by creating a class that implements the
593593
`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.
595596
596597
Your writer can perform any action, such as sending logs to a file, external service, or custom
597598
destination.
@@ -600,6 +601,24 @@ Writers can be synchronous (returning `void`) or asynchronous (returning a `Prom
600601
writer performs asynchronous operations (like network requests or file writes), simply return a
601602
promise from the `write` method.
602603
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+
603622
#### Example of HTTP Writer
604623
605624
```ts
@@ -658,6 +677,9 @@ writers are async.
658677
This design allows you to use async writers without impacting the performance of your application or
659678
blocking the main thread.
660679
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+
661683
##### Explicit Resource Management
662684
663685
The Hive Logger also supports

packages/web/docs/src/content/migration-guides/gateway-v1-v2.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ interface LogWriter {
342342
attrs: Attributes | null | undefined,
343343
msg: string | null | undefined
344344
): void | Promise<void>
345+
flush?(): void | Promise<void>
345346
}
346347
```
347348

0 commit comments

Comments
 (0)