Skip to content

Commit b5d9bb0

Browse files
V3 Backport [#10004]: fix wrangler dev logs being logged on the incorrect level in some cases (#10015)
* [miniflare] add `structuredWorkerdLogs` option * fix `wrangler dev` logs being logged on the incorrect level in some cases * update lock file * adapt tests for v3
1 parent 8453c05 commit b5d9bb0

File tree

19 files changed

+1828
-326
lines changed

19 files changed

+1828
-326
lines changed

.changeset/deep-lizards-move.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix `wrangler dev` logs being logged on the incorrect level in some cases
6+
7+
currently the way `wrangler dev` prints logs is faulty, for example the following code
8+
9+
```js
10+
console.error("this is an error");
11+
console.warn("this is a warning");
12+
console.debug("this is a debug");
13+
```
14+
15+
inside a worker would cause the following logs:
16+
17+
```text
18+
✘ [ERROR] this is an error
19+
20+
✘ [ERROR] this is a warning
21+
22+
this is a debug
23+
```
24+
25+
(note that the warning is printed as an error and the debug log is printed even if by default it should not)
26+
27+
the changes here make sure that the logs are instead logged to their correct level, so for the code about the following will be logged instead:
28+
29+
```text
30+
✘ [ERROR] this is an error
31+
32+
▲ [WARNING] this is a warning
33+
```
34+
35+
(running `wrangler dev` with the `--log-level=debug` flag will also cause the debug log to be included as well)

.changeset/full-areas-lick.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"miniflare": minor
3+
---
4+
5+
add `structuredWorkerdLogs` option
6+
7+
add a new top-level option named `structuredWorkerdLogs` that makes workerd print to stdout structured logs (stringified jsons of the following shape: `{ timestamp: number, level: string, message: string }`) instead of printing logs to stdout and stderr

fixtures/shared/src/run-wrangler-long-lived.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ async function runLongLivedWrangler(
8787
chunks.push(chunk);
8888
});
8989
wranglerProcess.stderr?.on("data", (chunk) => {
90-
console.log(`[${command}]`, chunk.toString());
90+
if (process.env.WRANGLER_LOG === "debug") {
91+
console.log(`[${command}]`, chunk.toString());
92+
}
9193
chunks.push(chunk);
9294
});
9395
wranglerProcess.once("exit", (exitCode) => {

fixtures/worker-logs/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "@fixture/worker-logs",
3+
"private": true,
4+
"scripts": {
5+
"check:type": "tsc",
6+
"dev": "pnpm dev.module",
7+
"dev.module": "wrangler dev -c wrangler.module.jsonc",
8+
"dev.service": "wrangler dev -c wrangler.service.jsonc",
9+
"start": "pnpm dev.module",
10+
"test:ci": "vitest run",
11+
"test:watch": "vitest"
12+
},
13+
"devDependencies": {
14+
"@cloudflare/workers-tsconfig": "workspace:^",
15+
"typescript": "catalog:default",
16+
"vitest": "catalog:default",
17+
"wrangler": "workspace:*"
18+
},
19+
"volta": {
20+
"extends": "../../package.json"
21+
}
22+
}

fixtures/worker-logs/src/module.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export default {
2+
async fetch(request, env) {
3+
const response = new Response("Hello");
4+
5+
const customMessage = request.headers.get("x-custom-message");
6+
if (customMessage) {
7+
if (customMessage === "%__VERY_VERY_LONG_MESSAGE_%") {
8+
// We can't simply pass a huge long message as a header thus
9+
// why a placeholder is used here
10+
console.log("z".repeat(2 ** 20));
11+
} else {
12+
console.log(customMessage);
13+
}
14+
return response;
15+
}
16+
17+
console.log("<<<<<this is a log>>>>>");
18+
console.warn("<<<<<this is a warning>>>>>");
19+
console.error("<<<<<this is an error>>>>>");
20+
console.debug("<<<<<this is a debug message>>>>>");
21+
console.info("<<<<<this is an info message>>>>>");
22+
23+
return response;
24+
},
25+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
async function handler(request) {
2+
console.log("<<<<<this is a log>>>>>");
3+
console.warn("<<<<<this is a warning>>>>>");
4+
console.error("<<<<<this is an error>>>>>");
5+
console.debug("<<<<<this is a debug message>>>>>");
6+
console.info("<<<<<this is an info message>>>>>");
7+
return new Response("Hello");
8+
}
9+
10+
addEventListener("fetch", (event) => {
11+
event.respondWith(handler(event.request));
12+
});

0 commit comments

Comments
 (0)