-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger-examples.ts
More file actions
170 lines (150 loc) · 3.95 KB
/
logger-examples.ts
File metadata and controls
170 lines (150 loc) · 3.95 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { FetchProxy } from "../src/index"
import pino from "pino"
// Example 1: Basic logger setup with default configuration
const basicProxy = new FetchProxy({
// Uses default logger configuration (info level, pretty printing in dev)
})
// Example 2: Custom logger with production configuration
const productionLogger = pino({
level: "warn",
transport: {
target: "pino/file",
options: { destination: "./proxy.log" },
},
})
const productionProxy = new FetchProxy({
logger: productionLogger,
})
// Example 3: Development logger with detailed debugging
// Simple version without additional dependencies
const developmentLogger = pino({
level: "debug",
transport: {
target: "pino/file",
options: { destination: 1 }, // stdout
},
})
// Alternative with pino-pretty (requires: npm install pino-pretty)
// Uncomment when pino-pretty is installed:
/*
const developmentLoggerPretty = pino({
level: "debug",
transport: {
target: "pino-pretty",
options: {
colorize: true,
translateTime: "SYS:yyyy-mm-dd HH:MM:ss",
ignore: "pid,hostname",
},
},
})
*/
const devProxy = new FetchProxy({
logger: developmentLogger, // Use developmentLoggerPretty if pino-pretty is installed
})
// Example 4: Custom logger with structured logging
const structuredLogger = pino({
level: "info",
formatters: {
level: (label) => ({ level: label }),
log: (object) => ({ ...object, service: "fetch-gate" }),
},
timestamp: pino.stdTimeFunctions.isoTime,
redact: ["password", "token", "authorization"],
})
const structuredProxy = new FetchProxy({
logger: structuredLogger,
timeout: 5000,
circuitBreaker: {
failureThreshold: 3,
resetTimeout: 30000,
enabled: true,
},
})
// Example 5: Logger with custom serializers for enhanced debugging
const debugLogger = pino({
level: "trace",
serializers: {
req: (req) => ({
method: req.method,
url: req.url,
headers: req.headers,
}),
res: (res) => ({
status: res.status,
statusText: res.statusText,
headers: Object.fromEntries(res.headers.entries()),
}),
err: pino.stdSerializers.err,
},
})
const debugProxy = new FetchProxy({
logger: debugLogger,
})
// Example usage with logging
async function exampleUsage() {
try {
console.log("Making request with production proxy...")
const request = new Request("https://api.example.com/data", {
method: "GET",
headers: {
"User-Agent": "fetch-gate-example/1.0",
},
})
const response = await productionProxy.proxy(request)
console.log("Response received:", response.status)
} catch (error) {
console.error("Request failed:", error)
}
}
// Example with request-specific logger options
async function requestSpecificLogging() {
const customRequestLogger = pino({
level: "debug",
// Note: Using console transport instead of pino-pretty to avoid dependency requirement
transport: {
target: "pino/file",
options: { destination: 1 }, // stdout
},
})
const request = new Request("https://httpbin.org/json", {
method: "GET",
})
const response = await basicProxy.proxy(request, undefined, {
logger: customRequestLogger, // Override proxy-level logger for this request
})
return response
}
// Example: Monitoring and alerting setup
const monitoringLogger = pino({
level: "info",
hooks: {
logMethod(inputArgs, method, level) {
// Custom hook for monitoring/alerting
if (level >= 50) {
// Error level
// Send to monitoring system
console.error("ALERT: Proxy error detected", inputArgs[0])
}
return method.apply(this, inputArgs)
},
},
})
const monitoringProxy = new FetchProxy({
logger: monitoringLogger,
circuitBreaker: {
failureThreshold: 5,
timeout: 60000,
resetTimeout: 30000,
},
})
export {
basicProxy,
productionProxy,
devProxy,
structuredProxy,
debugProxy,
monitoringProxy,
exampleUsage,
requestSpecificLogging,
}