@fabbahiense/pulsar-pino-transport 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +105 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
# @pulsar
|
|
1
|
+
# @fabbahiense/pulsar-pino-transport
|
|
2
2
|
|
|
3
3
|
Pino transport for [Pulsar](https://github.com/your-org/pulsar) — send your application logs to Pulsar in real-time.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install @pulsar
|
|
8
|
+
npm install @fabbahiense/pulsar-pino-transport
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
@@ -15,7 +15,7 @@ import pino from 'pino'
|
|
|
15
15
|
|
|
16
16
|
const logger = pino({
|
|
17
17
|
transport: {
|
|
18
|
-
target: '@pulsar
|
|
18
|
+
target: '@fabbahiense/pulsar-pino-transport',
|
|
19
19
|
options: {
|
|
20
20
|
url: 'https://your-pulsar-instance.com',
|
|
21
21
|
apiKey: 'psk_your_api_key',
|
|
@@ -62,6 +62,108 @@ The transport automatically picks up trace IDs from these fields:
|
|
|
62
62
|
logger.info({ traceId: 'my-trace-id' }, 'Processing request')
|
|
63
63
|
```
|
|
64
64
|
|
|
65
|
+
## Capturing request/response bodies (with `pino-http`)
|
|
66
|
+
|
|
67
|
+
The transport itself doesn't intercept HTTP — pino is a logger, not a middleware. To get rich request/response logs in Pulsar, pair it with [`pino-http`](https://github.com/pinojs/pino-http):
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
npm install pino-http
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import express from 'express'
|
|
75
|
+
import pino from 'pino'
|
|
76
|
+
import pinoHttp from 'pino-http'
|
|
77
|
+
|
|
78
|
+
const logger = pino({
|
|
79
|
+
transport: {
|
|
80
|
+
target: '@fabbahiense/pulsar-pino-transport',
|
|
81
|
+
options: { url: process.env.PULSAR_URL, apiKey: process.env.PULSAR_API_KEY },
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
const app = express()
|
|
86
|
+
app.use(express.json())
|
|
87
|
+
|
|
88
|
+
app.use(pinoHttp({
|
|
89
|
+
logger,
|
|
90
|
+
// Serialize request body
|
|
91
|
+
serializers: {
|
|
92
|
+
req(req) {
|
|
93
|
+
return {
|
|
94
|
+
method: req.method,
|
|
95
|
+
url: req.url,
|
|
96
|
+
headers: redact(req.headers),
|
|
97
|
+
requestBody: req.raw?.body, // Pulsar UI recognizes this key
|
|
98
|
+
query: req.query,
|
|
99
|
+
params: req.params,
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
res(res) {
|
|
103
|
+
return {
|
|
104
|
+
statusCode: res.statusCode,
|
|
105
|
+
headers: redact(res.getHeaders?.() ?? {}),
|
|
106
|
+
// responseBody requires intercepting res.write/res.end — see below
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
customLogLevel(_req, res, err) {
|
|
111
|
+
if (err || res.statusCode >= 500) return 'error'
|
|
112
|
+
if (res.statusCode >= 400) return 'warn'
|
|
113
|
+
return 'info'
|
|
114
|
+
},
|
|
115
|
+
}))
|
|
116
|
+
|
|
117
|
+
function redact(headers: Record<string, unknown>): Record<string, unknown> {
|
|
118
|
+
const out: Record<string, unknown> = {}
|
|
119
|
+
const drop = new Set(['authorization', 'cookie', 'set-cookie', 'x-api-key'])
|
|
120
|
+
for (const [k, v] of Object.entries(headers)) {
|
|
121
|
+
out[k] = drop.has(k.toLowerCase()) ? '[REDACTED]' : v
|
|
122
|
+
}
|
|
123
|
+
return out
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
**No painel lateral do Pulsar** isso aparece em seções dedicadas: Request body, Headers, Query, Path params, status, duration.
|
|
128
|
+
|
|
129
|
+
### Capturando response body (avançado)
|
|
130
|
+
|
|
131
|
+
Pino-http não captura response body nativamente. Para capturar, intercepte `res.write`/`res.end`:
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
app.use((req, res, next) => {
|
|
135
|
+
const chunks: Buffer[] = []
|
|
136
|
+
const originalWrite = res.write.bind(res)
|
|
137
|
+
const originalEnd = res.end.bind(res)
|
|
138
|
+
res.write = function (chunk: any) { if (chunk) chunks.push(Buffer.from(chunk)); return originalWrite(chunk) }
|
|
139
|
+
res.end = function (chunk: any) { if (chunk) chunks.push(Buffer.from(chunk)); return originalEnd(chunk) }
|
|
140
|
+
res.on('finish', () => {
|
|
141
|
+
try {
|
|
142
|
+
const body = Buffer.concat(chunks).toString('utf8').slice(0, 10240)
|
|
143
|
+
logger.info({ responseBody: tryParseJson(body), statusCode: res.statusCode, traceId: req.id }, `${req.method} ${req.url} ${res.statusCode}`)
|
|
144
|
+
} catch {}
|
|
145
|
+
})
|
|
146
|
+
next()
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
function tryParseJson(s: string) { try { return JSON.parse(s) } catch { return s } }
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
> **Mais simples?** Se você puder, considere `@fabbahiense/pulsar-node` que já vem com `Pulsar.middleware({ captureResponseBody: true })` configurado.
|
|
153
|
+
|
|
154
|
+
## Recognized metadata keys
|
|
155
|
+
|
|
156
|
+
Pulsar UI auto-detects these key names and renders dedicated sections:
|
|
157
|
+
|
|
158
|
+
| Section | Recognized keys |
|
|
159
|
+
|---|---|
|
|
160
|
+
| Request body | `body`, `requestBody`, `reqBody`, `payload`, `input` |
|
|
161
|
+
| Response body | `response`, `responseBody`, `output`, `result`, `data` |
|
|
162
|
+
| Request headers | `headers`, `requestHeaders`, `reqHeaders` |
|
|
163
|
+
| Response headers | `responseHeaders`, `resHeaders` |
|
|
164
|
+
| Query | `query`, `queryParams`, `qs` |
|
|
165
|
+
| Path params | `params`, `pathParams` |
|
|
166
|
+
|
|
65
167
|
## License
|
|
66
168
|
|
|
67
169
|
MIT
|