-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpino.ts
60 lines (49 loc) · 1.5 KB
/
pino.ts
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger, LogFormat } from "../src/types";
import { pino, type Logger as PinoLogger, LoggerOptions } from "pino";
// Define a custom Pino logger interface that includes the "http" level
interface CustomPinoLogger extends PinoLogger<LoggerOptions> {
http: <T extends unknown[]>(...args: T) => void;
}
// Define Pino logger
const logger : CustomPinoLogger = pino({
// Use the LOG_LEVEL environment variable, or default to "info"
level: Bun.env.LOG_LEVEL ?? "info",
// Rename 'msg' to 'message'
messageKey: "message",
// Rename 'err' to 'error'
errorKey: "error",
// Rename 'time' to 'ts'
timestamp: () => `,"ts":"${Date.now()}"`,
formatters: {
//Use `level` label instead of integer values
level: (label) => {
return { level: label };
},
},
// Define a custom "http" level
customLevels: {
http: 35 // same as `info`
},
});
const elysiaLogging = ElysiaLogging(logger as Logger, {
// Use the pino "http" custom level defined above
level: "http",
// Access logs in JSON format
format: LogFormat.JSON,
});
//
const app = new Elysia()
.use(elysiaLogging)
.get("/", () => {
if (Math.random() < 0.75) {
return new Response("Welcome to Bun!");
}
throw new Error("Whoops!");
})
.listen({
port: Bun.env.PORT ?? 3000,
maxRequestBodySize: Number.MAX_SAFE_INTEGER,
});
logger.http(`🦊 Running at http://${app.server?.hostname}:${app.server?.port}`);