|
1 | | -import * as path from "node:path"; |
2 | | -import { pino, type TransportMultiOptions, type TransportTargetOptions } from "pino"; |
3 | | - |
4 | 1 | import config from "@/config"; |
| 2 | +import { PinoClient } from "@vaylo/pino"; |
5 | 3 |
|
6 | | -const { loki } = config.logging; |
7 | | -const startingDate = new Date().toISOString().split("T")[0]; |
8 | | - |
9 | | -type PinoOpts = Parameters<typeof pino>[0] & { |
10 | | - transport: TransportMultiOptions & { targets: TransportTargetOptions[] }; |
11 | | -}; |
12 | | - |
13 | | -// https://github.com/pinojs/pino/issues/1791 |
14 | | -// if don't take out the options separately, it willn't work |
15 | | -const opts: PinoOpts = { |
16 | | - level: config.logging.level, |
17 | | - redact: { |
18 | | - // these just cause clutter |
19 | | - paths: ["pid", "hostname"], |
20 | | - remove: true, |
21 | | - }, |
22 | | - transport: { |
23 | | - targets: [], |
24 | | - }, |
25 | | -}; |
| 4 | +const { |
| 5 | + logging: { loki, level, logPath, logToFile }, |
| 6 | +} = config; |
26 | 7 |
|
27 | | -// it may be higher level than global, but it cann't be lower |
28 | | -opts.transport.targets.push({ |
29 | | - level: config.logging.level, |
30 | | - target: "pino-pretty", |
31 | | - options: { |
32 | | - colorized: true, |
33 | | - }, |
| 8 | +export const loggerClient = new PinoClient({ |
| 9 | + loki, |
| 10 | + level, |
| 11 | + logToFile, |
| 12 | + logPath, |
34 | 13 | }); |
35 | 14 |
|
36 | | -opts.transport.targets.push({ |
37 | | - level: config.logging.level, |
38 | | - target: "pino/file", |
39 | | - options: { |
40 | | - destination: path.join(config.logging.logPath, `${startingDate}.log`), |
41 | | - }, |
42 | | -}); |
43 | | - |
44 | | -if (loki.host) { |
45 | | - opts.transport.targets.push({ |
46 | | - level: config.logging.level, |
47 | | - target: "pino-loki", |
48 | | - options: { |
49 | | - batching: true, |
50 | | - interval: 5, |
51 | | - labels: { application: config.logging.loki.label }, |
52 | | - host: loki.host, |
53 | | - basicAuth: |
54 | | - loki.user && loki.password |
55 | | - ? { |
56 | | - username: loki.user, |
57 | | - password: loki.password, |
58 | | - } |
59 | | - : undefined, |
60 | | - }, |
61 | | - }); |
62 | | -} |
63 | | - |
64 | | -export const log = pino(opts); |
| 15 | +export const log = loggerClient.init(); |
0 commit comments