Skip to content

Commit 70b6fd2

Browse files
committed
daily log writer
1 parent 748b920 commit 70b6fd2

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,49 @@ winston, but also implement custom writers that send logs to a HTTP consumer or
285285
Read more about implementing your own writers in the [Hive Logger documentation](/docs/logger).
286286
</Callout>
287287

288-
#### Pino (only Node.js)
288+
#### Daily File Log Writer (Node.js Only)
289+
290+
Here is an example of a custom log writer that writes logs to a daily log file. It will write to a
291+
file for each day in a given directory.
292+
293+
```ts filename="daily-file-log-writer.ts"
294+
import fs from 'node:fs/promises'
295+
import path from 'node:path'
296+
import { Attributes, jsonStringify, LogLevel, LogWriter } from '@graphql-hive/logger'
297+
298+
export class DailyFileLogWriter implements LogWriter {
299+
constructor(
300+
private dir: string,
301+
private name: string
302+
) {}
303+
write(level: LogLevel, attrs: Attributes | null | undefined, msg: string | null | undefined) {
304+
const date = new Date().toISOString().split('T')[0]
305+
const logfile = path.resolve(this.dir, `${this.name}_${date}.log`)
306+
return fs.appendFile(logfile, jsonStringify({ level, msg, attrs }))
307+
}
308+
}
309+
```
310+
311+
And using it as simple as pluging it into an instance of Hive Logger to the `logging` option:
312+
313+
```ts filename="gateway.config.ts"
314+
import { defineConfig, JSONLogWriter, Logger } from '@graphql-hive/gateway'
315+
import { DailyFileLogWriter } from './daily-file-log-writer'
316+
317+
export const gatewayConfig = defineConfig({
318+
logging: new Logger({
319+
// you can combine multiple writers to log to different places
320+
writers: [
321+
// this will log to the console in JSON format
322+
new JSONLogWriter(),
323+
// and this is our daily file writer
324+
new DailyFileLogWriter('/var/log/hive', 'gateway')
325+
]
326+
})
327+
})
328+
```
329+
330+
#### Pino (Node.js Only)
289331

290332
Use the [Node.js `pino` logger library](https://github.com/pinojs/pino) for writing Hive Logger's
291333
logs.
@@ -321,7 +363,7 @@ export const gatewayConfig = defineConfig({
321363
})
322364
```
323365

324-
#### Winston (only Node.js)
366+
#### Winston (Node.js Only)
325367

326368
Use the [Node.js `winston` logger library](https://github.com/winstonjs/winston) for writing Hive
327369
Logger's logs.

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ $ LOG_JSON_PRETTY=1 node example.js
519519
Hive Logger includes some writers for common loggers of the JavaScript ecosystem with optional peer
520520
dependencies.
521521
522-
#### `PinoLogWriter`
522+
#### `PinoLogWriter` (Node.js Only)
523523
524524
Use the [Node.js `pino` logger library](https://github.com/pinojs/pino) for writing Hive Logger's
525525
logs.
@@ -553,7 +553,7 @@ log.info({ some: 'attributes' }, 'hello world')
553553
```
554554
{/* prettier-ignore-end */}
555555
556-
#### `WinstonLogWriter`
556+
#### `WinstonLogWriter` (Node.js Only)
557557
558558
Use the [Node.js `winston` logger library](https://github.com/winstonjs/winston) for writing Hive
559559
Logger's logs.
@@ -600,6 +600,8 @@ Writers can be synchronous (returning `void`) or asynchronous (returning a `Prom
600600
writer performs asynchronous operations (like network requests or file writes), simply return a
601601
promise from the `write` method.
602602
603+
#### Example of HTTP Writer
604+
603605
```ts
604606
import { Attributes, ConsoleLogWriter, Logger, LogLevel, LogWriter } from '@graphql-hive/logger'
605607
@@ -623,6 +625,29 @@ log.info('Hello World!')
623625
await log.flush() // make sure all async writes settle
624626
```
625627
628+
#### Example of Daily File Log Writer (Node.js Only)
629+
630+
Here is an example of a custom log writer that writes logs to a daily log file. It will write to a
631+
file for each day in a given directory.
632+
633+
```ts filename="daily-file-log-writer.ts"
634+
import fs from 'node:fs/promises'
635+
import path from 'node:path'
636+
import { Attributes, jsonStringify, LogLevel, LogWriter } from '@graphql-hive/logger'
637+
638+
export class DailyFileLogWriter implements LogWriter {
639+
constructor(
640+
private dir: string,
641+
private name: string
642+
) {}
643+
write(level: LogLevel, attrs: Attributes | null | undefined, msg: string | null | undefined) {
644+
const date = new Date().toISOString().split('T')[0]
645+
const logfile = path.resolve(this.dir, `${this.name}_${date}.log`)
646+
return fs.appendFile(logfile, jsonStringify({ level, msg, attrs }))
647+
}
648+
}
649+
```
650+
626651
#### Flushing and Non-Blocking Logging
627652
628653
The logger does not block when you log asynchronously. Instead, it tracks all pending async writes

0 commit comments

Comments
 (0)