Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .changeset/deep-lizards-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
"wrangler": patch
---

fix `wrangler dev` logs being logged on the incorrect level in some cases

currently the way `wrangler dev` prints logs is faulty, for example the following code

```js
console.error("this is an error");
console.warn("this is a warning");
console.debug("this is a debug");
```

inside a worker would cause the following logs:

```text
✘ [ERROR] this is an error

✘ [ERROR] this is a warning

this is a debug
```

(note that the warning is printed as an error and the debug log is printed even if by default it should not)

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:

```text
✘ [ERROR] this is an error

▲ [WARNING] this is a warning
```

(running `wrangler dev` with the `--log-level=debug` flag will also cause the debug log to be included as well)
7 changes: 7 additions & 0 deletions .changeset/full-areas-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"miniflare": minor
---

add `structuredWorkerdLogs` option

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
4 changes: 3 additions & 1 deletion fixtures/shared/src/run-wrangler-long-lived.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ async function runLongLivedWrangler(
chunks.push(chunk);
});
wranglerProcess.stderr?.on("data", (chunk) => {
console.log(`[${command}]`, chunk.toString());
if (process.env.WRANGLER_LOG === "debug") {
console.log(`[${command}]`, chunk.toString());
}
chunks.push(chunk);
});
wranglerProcess.once("exit", (exitCode) => {
Expand Down
22 changes: 22 additions & 0 deletions fixtures/worker-logs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@fixture/worker-logs",
"private": true,
"scripts": {
"check:type": "tsc",
"dev": "pnpm dev.module",
"dev.module": "wrangler dev -c wrangler.module.jsonc",
"dev.service": "wrangler dev -c wrangler.service.jsonc",
"start": "pnpm dev.module",
"test:ci": "vitest run",
"test:watch": "vitest"
},
"devDependencies": {
"@cloudflare/workers-tsconfig": "workspace:^",
"typescript": "catalog:default",
"vitest": "catalog:default",
"wrangler": "workspace:*"
},
"volta": {
"extends": "../../package.json"
}
}
25 changes: 25 additions & 0 deletions fixtures/worker-logs/src/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default {
async fetch(request, env) {
const response = new Response("Hello");

const customMessage = request.headers.get("x-custom-message");
if (customMessage) {
if (customMessage === "%__VERY_VERY_LONG_MESSAGE_%") {
// We can't simply pass a huge long message as a header thus
// why a placeholder is used here
console.log("z".repeat(2 ** 20));
} else {
console.log(customMessage);
}
return response;
}

console.log("<<<<<this is a log>>>>>");
console.warn("<<<<<this is a warning>>>>>");
console.error("<<<<<this is an error>>>>>");
console.debug("<<<<<this is a debug message>>>>>");
console.info("<<<<<this is an info message>>>>>");

return response;
},
};
12 changes: 12 additions & 0 deletions fixtures/worker-logs/src/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
async function handler(request) {
console.log("<<<<<this is a log>>>>>");
console.warn("<<<<<this is a warning>>>>>");
console.error("<<<<<this is an error>>>>>");
console.debug("<<<<<this is a debug message>>>>>");
console.info("<<<<<this is an info message>>>>>");
return new Response("Hello");
}

addEventListener("fetch", (event) => {
event.respondWith(handler(event.request));
});
Loading
Loading