Skip to content

Commit

Permalink
even if intecepted, log chunks from workerd to debug file
Browse files Browse the repository at this point in the history
  • Loading branch information
RamIdeas committed Nov 17, 2023
1 parent 27e0a76 commit 7461576
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
3 changes: 2 additions & 1 deletion packages/wrangler/src/dev/miniflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,8 @@ function handleRuntimeStdio(stdout: Readable, stderr: Readable) {
`Address already in use (${address}). Please check that you are not already running a server on this address or specify a different port with --port.`
);

return;
// even though we've intercepted the chunk and logged a better error to stderr
// fallthrough to log the original chunk to the debug log file for observability
}

// IGNORABLE:
Expand Down
28 changes: 15 additions & 13 deletions packages/wrangler/src/utils/debug-log-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async function ensureDirectoryExists(filepath: string) {
await mkdir(dirpath, { recursive: true });

Check warning on line 35 in packages/wrangler/src/utils/debug-log-file.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/utils/debug-log-file.ts#L35

Added line #L35 was not covered by tests
}

export const debugLogFilepath = path.resolve(getDebugFilepath());
export const debugLogFilepath = getDebugFilepath();
const mutex = new Mutex();

let hasLoggedLocation = false;
Expand All @@ -54,25 +54,27 @@ ${message}
---
`;

if (!hasLoggedLocation) {
hasLoggedLocation = true;
const relativeFilepath = path.relative(process.cwd(), debugLogFilepath);
console.info(`🐛 Writing debug logs to "${relativeFilepath}"`);
onExit(() => {
console.info(`🐛 Debug logs were written to "${relativeFilepath}"`);

Check warning on line 62 in packages/wrangler/src/utils/debug-log-file.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/utils/debug-log-file.ts#L58-L62

Added lines #L58 - L62 were not covered by tests
});
}

await mutex.runWith(async () => {
try {
await ensureDirectoryExists(debugLogFilepath);
await appendFile(debugLogFilepath, entry);

Check warning on line 69 in packages/wrangler/src/utils/debug-log-file.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/utils/debug-log-file.ts#L66-L69

Added lines #L66 - L69 were not covered by tests
} catch (err) {
if (hasLoggedError) return;
hasLoggedError = true;
console.error(`Failed to write to debug log file`, err);
console.error(`Would have written:`, entry);
if (!hasLoggedError) {
hasLoggedError = true;
console.error(`Failed to write to debug log file`, err);
console.error(`Would have written:`, entry);

Check warning on line 74 in packages/wrangler/src/utils/debug-log-file.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/utils/debug-log-file.ts#L72-L74

Added lines #L72 - L74 were not covered by tests
}
}
});

if (hasLoggedLocation) return;
hasLoggedLocation = true;
const relativeFilepath = path.relative(process.cwd(), debugLogFilepath);
console.info(`🐛 Writing debug logs to "${relativeFilepath}"`);
onExit(() => {
console.info(`🐛 Debug logs were written to "${relativeFilepath}"`);
});
}

/**
Expand Down

0 comments on commit 7461576

Please sign in to comment.