forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.ts
More file actions
36 lines (27 loc) · 867 Bytes
/
logger.ts
File metadata and controls
36 lines (27 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// src/lib/logger.ts
//
// Exports: log
import pino from "pino"
import pretty from "pino-pretty"
const g = globalThis as typeof globalThis & { __logger?: pino.Logger }
function createLogger(): pino.Logger {
const level = process.env.LOG_LEVEL || "info"
const logFile = process.env.LOG_FILE
const prettyStream = pretty({
colorize: process.stdout.isTTY ?? false,
translateTime: "SYS:yyyy-mm-dd HH:MM:ss",
ignore: "pid,hostname",
})
const streams: pino.StreamEntry[] = [{ level: "trace", stream: prettyStream }]
if (logFile) {
streams.push({
level: "trace",
stream: pino.destination({ dest: logFile, mkdir: true, sync: false }),
})
}
return pino({ level }, pino.multistream(streams))
}
export const log: pino.Logger = g.__logger ?? createLogger()
if (process.env.NODE_ENV !== "production") {
g.__logger = log
}