@orpc/pino 2.0.0-beta.46 → 2.0.0-beta.48
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 +6 -4
- package/dist/context.d.mts +22 -0
- package/dist/context.mjs +6 -0
- package/dist/handler-plugin.d.mts +63 -0
- package/dist/handler-plugin.mjs +139 -0
- package/dist/index.d.mts +2 -88
- package/dist/index.mjs +2 -149
- package/package.json +5 -5
- package/dist/index.d.ts +0 -88
package/README.md
CHANGED
|
@@ -156,10 +156,11 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
156
156
|
<td align="center"><a href="https://github.com/christ12938?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="christ12938"><img src="https://avatars.githubusercontent.com/u/25758598?v=4" width="167" alt="christ12938"/><br />christ12938</a></td>
|
|
157
157
|
<td align="center"><a href="https://github.com/Ryanjso?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Ryan Soderberg"><img src="https://avatars.githubusercontent.com/u/39172778?u=5ed913c31d57e7221b75784abcad48c7ebddde27&v=4" width="167" alt="Ryan Soderberg"/><br />Ryan Soderberg</a></td>
|
|
158
158
|
<td align="center"><a href="https://github.com/itigoore01?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="shota"><img src="https://avatars.githubusercontent.com/u/11831107?u=c976a6dc7e055eb026304c46c99100ed22b0c8e0&v=4" width="167" alt="shota"/><br />shota</a></td>
|
|
159
|
+
<td align="center"><a href="https://github.com/Nic13Gamer?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Nicholas"><img src="https://avatars.githubusercontent.com/u/54724556?u=56a7ab430ce7a80d648ab6eba051d454a818ed0b&v=4" width="167" alt="Nicholas"/><br />Nicholas</a></td>
|
|
159
160
|
<td align="center"><a href="https://github.com/ellis-driscoll?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Ellis Driscoll"><img src="https://avatars.githubusercontent.com/u/70685966?u=c5f95bc33b5991d9744abe00052542e4a2ed3cb9&v=4" width="167" alt="Ellis Driscoll"/><br />Ellis Driscoll</a></td>
|
|
160
|
-
<td align="center"><a href="https://github.com/hoangbn?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Hoang Nguyen"><img src="https://avatars.githubusercontent.com/u/38968280?u=c90084c6de65c56facabab7ba13a72a49ddbc3e4&v=4" width="167" alt="Hoang Nguyen"/><br />Hoang Nguyen</a></td>
|
|
161
161
|
</tr>
|
|
162
162
|
<tr>
|
|
163
|
+
<td align="center"><a href="https://github.com/hoangbn?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Hoang Nguyen"><img src="https://avatars.githubusercontent.com/u/38968280?u=c90084c6de65c56facabab7ba13a72a49ddbc3e4&v=4" width="167" alt="Hoang Nguyen"/><br />Hoang Nguyen</a></td>
|
|
163
164
|
<td align="center"><a href="https://opencollective.com/guest-ac41de3b?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Orestis Ioannou"><img src="https://images.opencollective.com/guest-ac41de3b/avatar/460.png" width="167" alt="Orestis Ioannou"/><br />Orestis Ioannou</a></td>
|
|
164
165
|
<td align="center"><a href="https://automatio.ai/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Stefan Smiljkovic"><img src="https://avatars.githubusercontent.com/u/1984909?u=b7bf5bc40ed49df3c22f69d2da7b8d78709c49ed&v=4" width="167" alt="Stefan Smiljkovic"/><br />Stefan Smiljkovic</a></td>
|
|
165
166
|
</tr>
|
|
@@ -190,10 +191,9 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
190
191
|
<td align="center"><a href="https://kellychan.im/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&v=4" width="139" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
|
|
191
192
|
<td align="center"><a href="https://guyariely.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Guy Ariely"><img src="https://avatars.githubusercontent.com/u/42813496?u=edb6b7f563bf28e160a290832e7da57c0506f8ca&v=4" width="139" alt="Guy Ariely"/><br />Guy Ariely</a></td>
|
|
192
193
|
<td align="center"><a href="https://paulsenon.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="PaulSenon"><img src="https://avatars.githubusercontent.com/u/19531087?u=6385741eb91d200b8c513ec6045482e301767ca6&v=4" width="139" alt="PaulSenon"/><br />PaulSenon</a></td>
|
|
193
|
-
<td align="center"><a href="https://
|
|
194
|
+
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
194
195
|
</tr>
|
|
195
196
|
<tr>
|
|
196
|
-
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
197
197
|
<td align="center"><a href="https://opensource.gubanov.eu/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Andrey Gubanov"><img src="https://avatars.githubusercontent.com/u/1082083?u=c5f2daf7ebece498e85c83367bb37b4e10e2649d&v=4" width="139" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td>
|
|
198
198
|
</tr>
|
|
199
199
|
</table>
|
|
@@ -206,7 +206,9 @@ With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
|
|
|
206
206
|
|
|
207
207
|
<table>
|
|
208
208
|
<tr>
|
|
209
|
-
<td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude
|
|
209
|
+
<td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude</a></td>
|
|
210
|
+
<td align="center"><a href="https://pullfrog.com/for-oss" target="_blank" rel="noopener" title="Pullfrog for OSS"><img src="https://avatars.githubusercontent.com/in/1768019?s=128&v=4" width="64" alt="Pullfrog"/><br />Pullfrog</a></td>
|
|
211
|
+
<td align="center"><a href="https://macroscope.com/open-source" target="_blank" rel="noopener" title="Macroscope for Open Source"><img src="https://avatars.githubusercontent.com/in/900172?s=128&v=4" width="64" alt="Macroscope"/><br />Macroscope</a></td>
|
|
210
212
|
</tr>
|
|
211
213
|
</table>
|
|
212
214
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Logger } from "pino";
|
|
2
|
+
/**
|
|
3
|
+
* Context symbol under which the Pino logger is stored. Use it to provide a
|
|
4
|
+
* custom logger instance per request, e.g. when integrating with pino-http.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://orpc.dev/docs/integrations/pino#providing-custom-logger-per-request | Pino Integration - Providing Custom Logger per Request}
|
|
7
|
+
*/
|
|
8
|
+
export declare const LOGGER_CONTEXT_SYMBOL: unique symbol;
|
|
9
|
+
/**
|
|
10
|
+
* Context shape that carries the Pino logger injected by `PinoHandlerPlugin`.
|
|
11
|
+
*
|
|
12
|
+
* @see {@link https://orpc.dev/docs/integrations/pino#using-the-logger-in-your-code | Pino Integration - Using the Logger in Your Code}
|
|
13
|
+
*/
|
|
14
|
+
export interface LoggerContext {
|
|
15
|
+
[LOGGER_CONTEXT_SYMBOL]?: Logger | undefined;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Retrieves the Pino logger from the oRPC context, if available.
|
|
19
|
+
*
|
|
20
|
+
* @see {@link https://orpc.dev/docs/integrations/pino#using-the-logger-in-your-code | Pino Integration - Using the Logger in Your Code}
|
|
21
|
+
*/
|
|
22
|
+
export declare function getLogger(context: LoggerContext): Logger | undefined;
|
package/dist/context.mjs
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Context } from "@orpc/server";
|
|
2
|
+
import type { StandardHandlerOptions, StandardHandlerPlugin, StandardHandlerRoutingInterceptorOptions } from "@orpc/server/standard";
|
|
3
|
+
import type { Logger } from "pino";
|
|
4
|
+
import pino from "pino";
|
|
5
|
+
export interface PinoHandlerPluginOptions<T extends Context> {
|
|
6
|
+
/**
|
|
7
|
+
* Logger instance to use for logging.
|
|
8
|
+
*
|
|
9
|
+
* @default pino()
|
|
10
|
+
*/
|
|
11
|
+
logger?: Logger | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Function to generate a unique ID for each request.
|
|
14
|
+
*
|
|
15
|
+
* @default ({ request }) => flattenStandardHeader(request.headers['x-request-id']) ?? crypto.randomUUID()
|
|
16
|
+
*/
|
|
17
|
+
generateRequestId?: ((options: StandardHandlerRoutingInterceptorOptions<T>) => string) | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* If true, this plugin will log information about request lifecycle,
|
|
20
|
+
* including when a request is received, handled, or no matching procedure is found.
|
|
21
|
+
*
|
|
22
|
+
* @default false
|
|
23
|
+
*/
|
|
24
|
+
logLifecycle?: boolean | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* If true, this plugin will log when a request signal is aborted.
|
|
27
|
+
*
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
30
|
+
logAbort?: boolean | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Customizes the log level for errors thrown from procedures;
|
|
33
|
+
* internal handler failures are always logged at error level.
|
|
34
|
+
* Receives the level applied by default; return it to keep the default behavior:
|
|
35
|
+
* 'info' for abort errors, 'warn' for ORPCError except INTERNAL_SERVER_ERROR, 'error' otherwise.
|
|
36
|
+
*
|
|
37
|
+
* @default (error, level) => level
|
|
38
|
+
*/
|
|
39
|
+
procedureErrorLevel?: ((error: unknown, level: pino.Level) => pino.Level) | undefined;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Instruments an oRPC handler with Pino structured logging, request tracking,
|
|
43
|
+
* and error monitoring.
|
|
44
|
+
*
|
|
45
|
+
* @see {@link https://orpc.dev/docs/integrations/pino | Pino Integration}
|
|
46
|
+
*/
|
|
47
|
+
export declare class PinoHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
48
|
+
name: string;
|
|
49
|
+
/**
|
|
50
|
+
* - Logging interceptors should run after tracing interceptors
|
|
51
|
+
* so they execute within the active request span.
|
|
52
|
+
* - Logging interceptors should run after batch interceptors
|
|
53
|
+
* so they log each individual request instead of the batch request.
|
|
54
|
+
*/
|
|
55
|
+
before: string[];
|
|
56
|
+
private readonly logger;
|
|
57
|
+
private readonly generateRequestId;
|
|
58
|
+
private readonly logLifecycle;
|
|
59
|
+
private readonly logAbort;
|
|
60
|
+
private readonly procedureErrorLevel;
|
|
61
|
+
constructor(options?: PinoHandlerPluginOptions<T>);
|
|
62
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
63
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { ORPCError, wrapAsyncIteratorPreservingEventMeta } from "@orpc/client";
|
|
2
|
+
import { isAbortError, isAsyncIteratorObject, ORPC_NAME, override, toArray, wrapReadableStream } from "@orpc/shared";
|
|
3
|
+
import { flattenStandardHeader } from "@standard-server/core";
|
|
4
|
+
import pino from "pino";
|
|
5
|
+
import { getLogger, LOGGER_CONTEXT_SYMBOL } from "./context.mjs";
|
|
6
|
+
|
|
7
|
+
export class PinoHandlerPlugin {
|
|
8
|
+
name = "~pino";
|
|
9
|
+
|
|
10
|
+
before = [
|
|
11
|
+
"~tracing",
|
|
12
|
+
"~batch",
|
|
13
|
+
"~hibernation"
|
|
14
|
+
];
|
|
15
|
+
logger;
|
|
16
|
+
generateRequestId;
|
|
17
|
+
logLifecycle;
|
|
18
|
+
logAbort;
|
|
19
|
+
procedureErrorLevel;
|
|
20
|
+
constructor(options = {}) {
|
|
21
|
+
this.logger = options.logger ?? pino();
|
|
22
|
+
this.generateRequestId = options.generateRequestId ?? (({ request }) => flattenStandardHeader(request.headers["x-request-id"]) ?? crypto.randomUUID());
|
|
23
|
+
this.logLifecycle = options.logLifecycle ?? false;
|
|
24
|
+
this.logAbort = options.logAbort ?? false;
|
|
25
|
+
this.procedureErrorLevel = options.procedureErrorLevel ?? ((_, level) => level);
|
|
26
|
+
}
|
|
27
|
+
init(options) {
|
|
28
|
+
const routingInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
29
|
+
const startMs = Date.now();
|
|
30
|
+
const logger = (interceptorOptions.context[LOGGER_CONTEXT_SYMBOL] ?? this.logger).child({});
|
|
31
|
+
|
|
32
|
+
if (!logger.bindings().req) {
|
|
33
|
+
logger.setBindings({ req: {
|
|
34
|
+
id: this.generateRequestId(interceptorOptions),
|
|
35
|
+
url: interceptorOptions.request.url,
|
|
36
|
+
method: interceptorOptions.request.method,
|
|
37
|
+
headers: {
|
|
38
|
+
"content-type": interceptorOptions.request.headers["content-type"],
|
|
39
|
+
"content-length": interceptorOptions.request.headers["content-length"],
|
|
40
|
+
"content-disposition": interceptorOptions.request.headers["content-disposition"],
|
|
41
|
+
"standard-server": interceptorOptions.request.headers["standard-server"]
|
|
42
|
+
}
|
|
43
|
+
} });
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
if (this.logLifecycle) {
|
|
47
|
+
logger?.info("request received");
|
|
48
|
+
}
|
|
49
|
+
const result = await next({
|
|
50
|
+
...interceptorOptions,
|
|
51
|
+
context: {
|
|
52
|
+
...interceptorOptions.context,
|
|
53
|
+
[LOGGER_CONTEXT_SYMBOL]: logger
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
if (this.logLifecycle) {
|
|
57
|
+
if (result.matched) {
|
|
58
|
+
logger?.info({
|
|
59
|
+
msg: "request handled",
|
|
60
|
+
res: { status: result.response.status },
|
|
61
|
+
responseTime: Date.now() - startMs
|
|
62
|
+
});
|
|
63
|
+
} else {
|
|
64
|
+
logger?.info("no matching procedure found");
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
|
|
70
|
+
logger.error(error);
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
const interceptor = async ({ next, context, path, request }) => {
|
|
75
|
+
const logger = getLogger(context);
|
|
76
|
+
logger?.setBindings({ rpc: {
|
|
77
|
+
system: ORPC_NAME,
|
|
78
|
+
method: path.join(".")
|
|
79
|
+
} });
|
|
80
|
+
if (this.logAbort) {
|
|
81
|
+
const signal = request.signal;
|
|
82
|
+
if (signal?.aborted) {
|
|
83
|
+
logger?.info(`request was aborted before handling (${String(signal.reason)})`);
|
|
84
|
+
} else {
|
|
85
|
+
signal?.addEventListener("abort", () => {
|
|
86
|
+
logger?.info(`request is aborted (${String(signal.reason)})`);
|
|
87
|
+
}, { once: true });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
return await next();
|
|
92
|
+
} catch (error) {
|
|
93
|
+
logProcedureError(logger, error, this.procedureErrorLevel);
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const clientInterceptor = async ({ next, context }) => {
|
|
98
|
+
const output = await next();
|
|
99
|
+
if (isAsyncIteratorObject(output)) {
|
|
100
|
+
|
|
101
|
+
return override(output, wrapAsyncIteratorPreservingEventMeta(output, { onError: (error) => {
|
|
102
|
+
logProcedureError(getLogger(context), error, this.procedureErrorLevel);
|
|
103
|
+
} }));
|
|
104
|
+
}
|
|
105
|
+
if (output instanceof ReadableStream) {
|
|
106
|
+
|
|
107
|
+
return override(output, wrapReadableStream(output, { onError: (error) => {
|
|
108
|
+
logProcedureError(getLogger(context), error, this.procedureErrorLevel);
|
|
109
|
+
} }));
|
|
110
|
+
}
|
|
111
|
+
return output;
|
|
112
|
+
};
|
|
113
|
+
return {
|
|
114
|
+
...options,
|
|
115
|
+
routingInterceptors: [routingInterceptor, ...toArray(options.routingInterceptors)],
|
|
116
|
+
interceptors: [interceptor, ...toArray(options.interceptors)],
|
|
117
|
+
clientInterceptors: [clientInterceptor, ...toArray(options.clientInterceptors)]
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function logProcedureError(logger, error, procedureErrorLevel) {
|
|
122
|
+
logger?.[procedureErrorLevel(error, defaultProcedureErrorLevel(error))](error);
|
|
123
|
+
}
|
|
124
|
+
function defaultProcedureErrorLevel(error) {
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
if (isAbortError(error)) {
|
|
128
|
+
return "info";
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
if (error instanceof ORPCError && error.code !== "INTERNAL_SERVER_ERROR") {
|
|
136
|
+
return "warn";
|
|
137
|
+
}
|
|
138
|
+
return "error";
|
|
139
|
+
}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,88 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { StandardHandlerPlugin, StandardHandlerRoutingInterceptorOptions, StandardHandlerOptions } from '@orpc/server/standard';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Context symbol under which the Pino logger is stored. Use it to provide a
|
|
7
|
-
* custom logger instance per request, e.g. when integrating with pino-http.
|
|
8
|
-
*
|
|
9
|
-
* @see {@link https://orpc.dev/docs/integrations/pino#providing-custom-logger-per-request | Pino Integration - Providing Custom Logger per Request}
|
|
10
|
-
*/
|
|
11
|
-
declare const LOGGER_CONTEXT_SYMBOL: unique symbol;
|
|
12
|
-
/**
|
|
13
|
-
* Context shape that carries the Pino logger injected by `PinoHandlerPlugin`.
|
|
14
|
-
*
|
|
15
|
-
* @see {@link https://orpc.dev/docs/integrations/pino#using-the-logger-in-your-code | Pino Integration - Using the Logger in Your Code}
|
|
16
|
-
*/
|
|
17
|
-
interface LoggerContext {
|
|
18
|
-
[LOGGER_CONTEXT_SYMBOL]?: Logger | undefined;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Retrieves the Pino logger from the oRPC context, if available.
|
|
22
|
-
*
|
|
23
|
-
* @see {@link https://orpc.dev/docs/integrations/pino#using-the-logger-in-your-code | Pino Integration - Using the Logger in Your Code}
|
|
24
|
-
*/
|
|
25
|
-
declare function getLogger(context: LoggerContext): Logger | undefined;
|
|
26
|
-
|
|
27
|
-
interface PinoHandlerPluginOptions<T extends Context> {
|
|
28
|
-
/**
|
|
29
|
-
* Logger instance to use for logging.
|
|
30
|
-
*
|
|
31
|
-
* @default pino()
|
|
32
|
-
*/
|
|
33
|
-
logger?: Logger | undefined;
|
|
34
|
-
/**
|
|
35
|
-
* Function to generate a unique ID for each request.
|
|
36
|
-
*
|
|
37
|
-
* @default ({ request }) => flattenStandardHeader(request.headers['x-request-id']) ?? crypto.randomUUID()
|
|
38
|
-
*/
|
|
39
|
-
generateRequestId?: ((options: StandardHandlerRoutingInterceptorOptions<T>) => string) | undefined;
|
|
40
|
-
/**
|
|
41
|
-
* If true, this plugin will log information about request lifecycle,
|
|
42
|
-
* including when a request is received, handled, or no matching procedure is found.
|
|
43
|
-
*
|
|
44
|
-
* @default false
|
|
45
|
-
*/
|
|
46
|
-
logLifecycle?: boolean | undefined;
|
|
47
|
-
/**
|
|
48
|
-
* If true, this plugin will log when a request signal is aborted.
|
|
49
|
-
*
|
|
50
|
-
* @default false
|
|
51
|
-
*/
|
|
52
|
-
logAbort?: boolean | undefined;
|
|
53
|
-
/**
|
|
54
|
-
* Customizes the log level for errors thrown from procedures;
|
|
55
|
-
* internal handler failures are always logged at error level.
|
|
56
|
-
* Receives the level applied by default; return it to keep the default behavior:
|
|
57
|
-
* 'info' for abort errors, 'warn' for ORPCError except INTERNAL_SERVER_ERROR, 'error' otherwise.
|
|
58
|
-
*
|
|
59
|
-
* @default (error, level) => level
|
|
60
|
-
*/
|
|
61
|
-
procedureErrorLevel?: ((error: unknown, level: pino.Level) => pino.Level) | undefined;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Instruments an oRPC handler with Pino structured logging, request tracking,
|
|
65
|
-
* and error monitoring.
|
|
66
|
-
*
|
|
67
|
-
* @see {@link https://orpc.dev/docs/integrations/pino | Pino Integration}
|
|
68
|
-
*/
|
|
69
|
-
declare class PinoHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
70
|
-
name: string;
|
|
71
|
-
/**
|
|
72
|
-
* - Logging interceptors should run after tracing interceptors
|
|
73
|
-
* so they execute within the active request span.
|
|
74
|
-
* - Logging interceptors should run after batch interceptors
|
|
75
|
-
* so they log each individual request instead of the batch request.
|
|
76
|
-
*/
|
|
77
|
-
before: string[];
|
|
78
|
-
private readonly logger;
|
|
79
|
-
private readonly generateRequestId;
|
|
80
|
-
private readonly logLifecycle;
|
|
81
|
-
private readonly logAbort;
|
|
82
|
-
private readonly procedureErrorLevel;
|
|
83
|
-
constructor(options?: PinoHandlerPluginOptions<T>);
|
|
84
|
-
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export { LOGGER_CONTEXT_SYMBOL, PinoHandlerPlugin, getLogger };
|
|
88
|
-
export type { LoggerContext, PinoHandlerPluginOptions };
|
|
1
|
+
export * from "./context.mjs";
|
|
2
|
+
export * from "./handler-plugin.mjs";
|
package/dist/index.mjs
CHANGED
|
@@ -1,149 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { flattenStandardHeader } from '@standard-server/core';
|
|
4
|
-
import pino from 'pino';
|
|
5
|
-
|
|
6
|
-
const LOGGER_CONTEXT_SYMBOL = Symbol.for("ORPC_PINO_LOGGER_CONTEXT");
|
|
7
|
-
function getLogger(context) {
|
|
8
|
-
return context[LOGGER_CONTEXT_SYMBOL];
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
class PinoHandlerPlugin {
|
|
12
|
-
name = "~pino";
|
|
13
|
-
/**
|
|
14
|
-
* - Logging interceptors should run after tracing interceptors
|
|
15
|
-
* so they execute within the active request span.
|
|
16
|
-
* - Logging interceptors should run after batch interceptors
|
|
17
|
-
* so they log each individual request instead of the batch request.
|
|
18
|
-
*/
|
|
19
|
-
before = ["~tracing", "~batch", "~hibernation"];
|
|
20
|
-
logger;
|
|
21
|
-
generateRequestId;
|
|
22
|
-
logLifecycle;
|
|
23
|
-
logAbort;
|
|
24
|
-
procedureErrorLevel;
|
|
25
|
-
constructor(options = {}) {
|
|
26
|
-
this.logger = options.logger ?? pino();
|
|
27
|
-
this.generateRequestId = options.generateRequestId ?? (({ request }) => flattenStandardHeader(request.headers["x-request-id"]) ?? crypto.randomUUID());
|
|
28
|
-
this.logLifecycle = options.logLifecycle ?? false;
|
|
29
|
-
this.logAbort = options.logAbort ?? false;
|
|
30
|
-
this.procedureErrorLevel = options.procedureErrorLevel ?? ((_, level) => level);
|
|
31
|
-
}
|
|
32
|
-
init(options) {
|
|
33
|
-
const routingInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
34
|
-
const startMs = Date.now();
|
|
35
|
-
const logger = (interceptorOptions.context[LOGGER_CONTEXT_SYMBOL] ?? this.logger).child({});
|
|
36
|
-
if (!logger.bindings().req) {
|
|
37
|
-
logger.setBindings({
|
|
38
|
-
req: {
|
|
39
|
-
id: this.generateRequestId(interceptorOptions),
|
|
40
|
-
url: interceptorOptions.request.url,
|
|
41
|
-
method: interceptorOptions.request.method,
|
|
42
|
-
headers: {
|
|
43
|
-
"content-type": interceptorOptions.request.headers["content-type"],
|
|
44
|
-
"content-length": interceptorOptions.request.headers["content-length"],
|
|
45
|
-
"content-disposition": interceptorOptions.request.headers["content-disposition"],
|
|
46
|
-
"standard-server": interceptorOptions.request.headers["standard-server"]
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
try {
|
|
52
|
-
if (this.logLifecycle) {
|
|
53
|
-
logger?.info("request received");
|
|
54
|
-
}
|
|
55
|
-
const result = await next({
|
|
56
|
-
...interceptorOptions,
|
|
57
|
-
context: {
|
|
58
|
-
...interceptorOptions.context,
|
|
59
|
-
[LOGGER_CONTEXT_SYMBOL]: logger
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
if (this.logLifecycle) {
|
|
63
|
-
if (result.matched) {
|
|
64
|
-
logger?.info({
|
|
65
|
-
msg: "request handled",
|
|
66
|
-
res: {
|
|
67
|
-
status: result.response.status
|
|
68
|
-
},
|
|
69
|
-
responseTime: Date.now() - startMs
|
|
70
|
-
});
|
|
71
|
-
} else {
|
|
72
|
-
logger?.info("no matching procedure found");
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return result;
|
|
76
|
-
} catch (error) {
|
|
77
|
-
logger.error(error);
|
|
78
|
-
throw error;
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
const interceptor = async ({ next, context, path, request }) => {
|
|
82
|
-
const logger = getLogger(context);
|
|
83
|
-
logger?.setBindings({ rpc: { system: ORPC_NAME, method: path.join(".") } });
|
|
84
|
-
if (this.logAbort) {
|
|
85
|
-
const signal = request.signal;
|
|
86
|
-
if (signal?.aborted) {
|
|
87
|
-
logger?.info(`request was aborted before handling (${String(signal.reason)})`);
|
|
88
|
-
} else {
|
|
89
|
-
signal?.addEventListener("abort", () => {
|
|
90
|
-
logger?.info(`request is aborted (${String(signal.reason)})`);
|
|
91
|
-
}, { once: true });
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
try {
|
|
95
|
-
return await next();
|
|
96
|
-
} catch (error) {
|
|
97
|
-
logProcedureError(logger, error, this.procedureErrorLevel);
|
|
98
|
-
throw error;
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
const clientInterceptor = async ({ next, context }) => {
|
|
102
|
-
const output = await next();
|
|
103
|
-
if (isAsyncIteratorObject(output)) {
|
|
104
|
-
return override(output, wrapAsyncIteratorPreservingEventMeta(output, {
|
|
105
|
-
onError: (error) => {
|
|
106
|
-
logProcedureError(getLogger(context), error, this.procedureErrorLevel);
|
|
107
|
-
}
|
|
108
|
-
}));
|
|
109
|
-
}
|
|
110
|
-
if (output instanceof ReadableStream) {
|
|
111
|
-
return override(output, wrapReadableStream(output, {
|
|
112
|
-
onError: (error) => {
|
|
113
|
-
logProcedureError(getLogger(context), error, this.procedureErrorLevel);
|
|
114
|
-
}
|
|
115
|
-
}));
|
|
116
|
-
}
|
|
117
|
-
return output;
|
|
118
|
-
};
|
|
119
|
-
return {
|
|
120
|
-
...options,
|
|
121
|
-
routingInterceptors: [
|
|
122
|
-
routingInterceptor,
|
|
123
|
-
...toArray(options.routingInterceptors)
|
|
124
|
-
],
|
|
125
|
-
interceptors: [
|
|
126
|
-
interceptor,
|
|
127
|
-
...toArray(options.interceptors)
|
|
128
|
-
],
|
|
129
|
-
clientInterceptors: [
|
|
130
|
-
clientInterceptor,
|
|
131
|
-
...toArray(options.clientInterceptors)
|
|
132
|
-
]
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
function logProcedureError(logger, error, procedureErrorLevel) {
|
|
137
|
-
logger?.[procedureErrorLevel(error, defaultProcedureErrorLevel(error))](error);
|
|
138
|
-
}
|
|
139
|
-
function defaultProcedureErrorLevel(error) {
|
|
140
|
-
if (isAbortError(error)) {
|
|
141
|
-
return "info";
|
|
142
|
-
}
|
|
143
|
-
if (error instanceof ORPCError && error.code !== "INTERNAL_SERVER_ERROR") {
|
|
144
|
-
return "warn";
|
|
145
|
-
}
|
|
146
|
-
return "error";
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export { LOGGER_CONTEXT_SYMBOL, PinoHandlerPlugin, getLogger };
|
|
1
|
+
export * from "./context.mjs";
|
|
2
|
+
export * from "./handler-plugin.mjs";
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/pino",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.48",
|
|
5
5
|
"description": "Pino integration for oRPC: structured logging for typesafe APIs",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"funding": [
|
|
@@ -38,10 +38,10 @@
|
|
|
38
38
|
"pino": ">=10.1.0"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@orpc/client": "2.0.0-beta.
|
|
42
|
-
"@orpc/server": "2.0.0-beta.
|
|
43
|
-
"@orpc/shared": "2.0.0-beta.
|
|
44
|
-
"@standard-server/core": "~0.
|
|
41
|
+
"@orpc/client": "2.0.0-beta.48",
|
|
42
|
+
"@orpc/server": "2.0.0-beta.48",
|
|
43
|
+
"@orpc/shared": "2.0.0-beta.48",
|
|
44
|
+
"@standard-server/core": "~0.14.0"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"pino": "^10.4.0"
|
package/dist/index.d.ts
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import pino, { Logger } from 'pino';
|
|
2
|
-
import { Context } from '@orpc/server';
|
|
3
|
-
import { StandardHandlerPlugin, StandardHandlerRoutingInterceptorOptions, StandardHandlerOptions } from '@orpc/server/standard';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Context symbol under which the Pino logger is stored. Use it to provide a
|
|
7
|
-
* custom logger instance per request, e.g. when integrating with pino-http.
|
|
8
|
-
*
|
|
9
|
-
* @see {@link https://orpc.dev/docs/integrations/pino#providing-custom-logger-per-request | Pino Integration - Providing Custom Logger per Request}
|
|
10
|
-
*/
|
|
11
|
-
declare const LOGGER_CONTEXT_SYMBOL: unique symbol;
|
|
12
|
-
/**
|
|
13
|
-
* Context shape that carries the Pino logger injected by `PinoHandlerPlugin`.
|
|
14
|
-
*
|
|
15
|
-
* @see {@link https://orpc.dev/docs/integrations/pino#using-the-logger-in-your-code | Pino Integration - Using the Logger in Your Code}
|
|
16
|
-
*/
|
|
17
|
-
interface LoggerContext {
|
|
18
|
-
[LOGGER_CONTEXT_SYMBOL]?: Logger | undefined;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Retrieves the Pino logger from the oRPC context, if available.
|
|
22
|
-
*
|
|
23
|
-
* @see {@link https://orpc.dev/docs/integrations/pino#using-the-logger-in-your-code | Pino Integration - Using the Logger in Your Code}
|
|
24
|
-
*/
|
|
25
|
-
declare function getLogger(context: LoggerContext): Logger | undefined;
|
|
26
|
-
|
|
27
|
-
interface PinoHandlerPluginOptions<T extends Context> {
|
|
28
|
-
/**
|
|
29
|
-
* Logger instance to use for logging.
|
|
30
|
-
*
|
|
31
|
-
* @default pino()
|
|
32
|
-
*/
|
|
33
|
-
logger?: Logger | undefined;
|
|
34
|
-
/**
|
|
35
|
-
* Function to generate a unique ID for each request.
|
|
36
|
-
*
|
|
37
|
-
* @default ({ request }) => flattenStandardHeader(request.headers['x-request-id']) ?? crypto.randomUUID()
|
|
38
|
-
*/
|
|
39
|
-
generateRequestId?: ((options: StandardHandlerRoutingInterceptorOptions<T>) => string) | undefined;
|
|
40
|
-
/**
|
|
41
|
-
* If true, this plugin will log information about request lifecycle,
|
|
42
|
-
* including when a request is received, handled, or no matching procedure is found.
|
|
43
|
-
*
|
|
44
|
-
* @default false
|
|
45
|
-
*/
|
|
46
|
-
logLifecycle?: boolean | undefined;
|
|
47
|
-
/**
|
|
48
|
-
* If true, this plugin will log when a request signal is aborted.
|
|
49
|
-
*
|
|
50
|
-
* @default false
|
|
51
|
-
*/
|
|
52
|
-
logAbort?: boolean | undefined;
|
|
53
|
-
/**
|
|
54
|
-
* Customizes the log level for errors thrown from procedures;
|
|
55
|
-
* internal handler failures are always logged at error level.
|
|
56
|
-
* Receives the level applied by default; return it to keep the default behavior:
|
|
57
|
-
* 'info' for abort errors, 'warn' for ORPCError except INTERNAL_SERVER_ERROR, 'error' otherwise.
|
|
58
|
-
*
|
|
59
|
-
* @default (error, level) => level
|
|
60
|
-
*/
|
|
61
|
-
procedureErrorLevel?: ((error: unknown, level: pino.Level) => pino.Level) | undefined;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Instruments an oRPC handler with Pino structured logging, request tracking,
|
|
65
|
-
* and error monitoring.
|
|
66
|
-
*
|
|
67
|
-
* @see {@link https://orpc.dev/docs/integrations/pino | Pino Integration}
|
|
68
|
-
*/
|
|
69
|
-
declare class PinoHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
70
|
-
name: string;
|
|
71
|
-
/**
|
|
72
|
-
* - Logging interceptors should run after tracing interceptors
|
|
73
|
-
* so they execute within the active request span.
|
|
74
|
-
* - Logging interceptors should run after batch interceptors
|
|
75
|
-
* so they log each individual request instead of the batch request.
|
|
76
|
-
*/
|
|
77
|
-
before: string[];
|
|
78
|
-
private readonly logger;
|
|
79
|
-
private readonly generateRequestId;
|
|
80
|
-
private readonly logLifecycle;
|
|
81
|
-
private readonly logAbort;
|
|
82
|
-
private readonly procedureErrorLevel;
|
|
83
|
-
constructor(options?: PinoHandlerPluginOptions<T>);
|
|
84
|
-
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export { LOGGER_CONTEXT_SYMBOL, PinoHandlerPlugin, getLogger };
|
|
88
|
-
export type { LoggerContext, PinoHandlerPluginOptions };
|