-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpino-pretty.ts
73 lines (60 loc) · 1.63 KB
/
pino-pretty.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
61
62
63
64
65
66
67
68
69
70
71
72
73
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger, LogFormat } from "../src/types";
import { pino } from "pino";
// Define Pino logger
const logger : Logger = 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`
},
// Configure the "pino-pretty" transport
transport: {
target: 'pino-pretty',
options: {
colorize: true,
customLevels: "http:35",
customColors: "http:gray",
colorizeObjects: true,
ignore: 'request,response',
useOnlyCustomProps: false,
messageKey: 'message',
errorKey: 'error',
timestampKey: 'ts',
}
},
});
const elysiaLogging = ElysiaLogging(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.info(`🦊 Running at http://${app.server?.hostname}:${app.server?.port}`);