widelogger 0.2.0 → 0.2.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 +62 -59
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -54,58 +54,58 @@ const { widelog, destroy } = widelogger({
|
|
|
54
54
|
Create a shared logger instance, use middleware to wrap requests in a context, and accumulate fields from anywhere in your codebase.
|
|
55
55
|
|
|
56
56
|
```ts
|
|
57
|
-
//
|
|
57
|
+
// src/logger.ts
|
|
58
58
|
import { widelogger } from "widelogger";
|
|
59
59
|
|
|
60
|
-
const { widelog, destroy } = widelogger({
|
|
60
|
+
export const { widelog, destroy } = widelogger({
|
|
61
61
|
service: "checkout-api",
|
|
62
62
|
defaultEventName: "http_request",
|
|
63
63
|
});
|
|
64
|
-
|
|
65
|
-
export { widelog, destroy };
|
|
66
64
|
```
|
|
67
65
|
|
|
68
66
|
```ts
|
|
69
|
-
// middleware/logging.ts
|
|
70
|
-
import { widelog } from "../
|
|
71
|
-
|
|
72
|
-
export const
|
|
73
|
-
widelog.context(
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
67
|
+
// src/middleware/logging.ts
|
|
68
|
+
import { widelog } from "../logger";
|
|
69
|
+
|
|
70
|
+
export const logging = (request, response, next) => {
|
|
71
|
+
widelog.context(
|
|
72
|
+
() =>
|
|
73
|
+
new Promise((resolve) => {
|
|
74
|
+
widelog.set("method", request.method);
|
|
75
|
+
widelog.set("path", request.path);
|
|
76
|
+
widelog.time.start("duration_ms");
|
|
77
|
+
|
|
78
|
+
response.on("finish", () => {
|
|
79
|
+
widelog.set("status_code", response.statusCode);
|
|
80
|
+
widelog.time.stop("duration_ms");
|
|
81
|
+
widelog.flush();
|
|
82
|
+
resolve();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
next();
|
|
86
|
+
}),
|
|
87
|
+
);
|
|
86
88
|
};
|
|
87
89
|
```
|
|
88
90
|
|
|
89
91
|
```ts
|
|
90
|
-
// routes/checkout.ts
|
|
91
|
-
import { widelog } from "../
|
|
92
|
-
|
|
93
|
-
export const checkout =
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
widelog.set("user.
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
res.status(500).json({ error: "checkout failed" });
|
|
108
|
-
}
|
|
92
|
+
// src/routes/checkout.ts
|
|
93
|
+
import { widelog } from "../logger";
|
|
94
|
+
|
|
95
|
+
export const checkout = (request, response) => {
|
|
96
|
+
const { userId } = request.body;
|
|
97
|
+
|
|
98
|
+
widelog.set("user.id", userId);
|
|
99
|
+
widelog.set("user.plan", "premium");
|
|
100
|
+
|
|
101
|
+
widelog.time.start("db_ms");
|
|
102
|
+
const order = await processOrder(userId);
|
|
103
|
+
widelog.time.stop("db_ms");
|
|
104
|
+
|
|
105
|
+
widelog.set("order.total_cents", order.totalCents);
|
|
106
|
+
widelog.count("order.items", order.itemCount);
|
|
107
|
+
|
|
108
|
+
response.json({ orderId: order.id });
|
|
109
109
|
};
|
|
110
110
|
```
|
|
111
111
|
|
|
@@ -116,49 +116,52 @@ The handler doesn't need to know about context setup or flushing — it just imp
|
|
|
116
116
|
The same pattern works with Bun's built-in server.
|
|
117
117
|
|
|
118
118
|
```ts
|
|
119
|
-
//
|
|
119
|
+
// src/logger.ts
|
|
120
120
|
import { widelogger } from "widelogger";
|
|
121
121
|
|
|
122
|
-
const { widelog, destroy } = widelogger({
|
|
122
|
+
export const { widelog, destroy } = widelogger({
|
|
123
123
|
service: "checkout-api",
|
|
124
124
|
defaultEventName: "http_request",
|
|
125
125
|
});
|
|
126
|
-
|
|
127
|
-
export { widelog, destroy };
|
|
128
126
|
```
|
|
129
127
|
|
|
130
128
|
```ts
|
|
131
|
-
// routes/checkout.ts
|
|
132
|
-
import { widelog } from "../
|
|
129
|
+
// src/routes/checkout.ts
|
|
130
|
+
import { widelog } from "../logger";
|
|
131
|
+
|
|
132
|
+
export const checkout = async (request: Request) => {
|
|
133
|
+
const { userId } = await request.json();
|
|
134
|
+
|
|
135
|
+
widelog.set("user.id", userId);
|
|
136
|
+
widelog.set("user.plan", "premium");
|
|
133
137
|
|
|
134
|
-
|
|
135
|
-
const
|
|
136
|
-
widelog.
|
|
137
|
-
widelog.set("user.plan", user.plan);
|
|
138
|
+
widelog.time.start("db_ms");
|
|
139
|
+
const order = await processOrder(userId);
|
|
140
|
+
widelog.time.stop("db_ms");
|
|
138
141
|
|
|
139
|
-
const order = await processOrder(user);
|
|
140
142
|
widelog.set("order.total_cents", order.totalCents);
|
|
141
|
-
widelog.count("order.items", order.
|
|
143
|
+
widelog.count("order.items", order.itemCount);
|
|
142
144
|
|
|
143
145
|
return Response.json({ orderId: order.id });
|
|
144
146
|
};
|
|
145
147
|
```
|
|
146
148
|
|
|
147
149
|
```ts
|
|
148
|
-
// server.ts
|
|
149
|
-
import {
|
|
150
|
+
// src/server.ts
|
|
151
|
+
import { serve } from "bun";
|
|
152
|
+
import { widelog } from "./logger";
|
|
150
153
|
import { checkout } from "./routes/checkout";
|
|
151
154
|
|
|
152
|
-
|
|
153
|
-
fetch: (
|
|
155
|
+
serve({
|
|
156
|
+
fetch: (request) =>
|
|
154
157
|
widelog.context(async () => {
|
|
155
|
-
const url = new URL(
|
|
156
|
-
widelog.set("method",
|
|
158
|
+
const url = new URL(request.url);
|
|
159
|
+
widelog.set("method", request.method);
|
|
157
160
|
widelog.set("path", url.pathname);
|
|
158
161
|
widelog.time.start("duration_ms");
|
|
159
162
|
|
|
160
163
|
try {
|
|
161
|
-
const response = await checkout(
|
|
164
|
+
const response = await checkout(request);
|
|
162
165
|
widelog.set("status_code", response.status);
|
|
163
166
|
widelog.set("outcome", "success");
|
|
164
167
|
return response;
|