Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log file fixes #4488

Merged
merged 7 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions .changeset/honest-jeans-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Changes the default directory for log files to workaround frameworks that are watching the entire `.wrangler` directory in the project root for changes
2 changes: 1 addition & 1 deletion packages/wrangler/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import CLITable from "cli-table3";
import { formatMessagesSync } from "esbuild";
import { getEnvironmentVariableFactory } from "./environment-variables/factory";
import { getSanitizeLogs } from "./environment-variables/misc-variables";
import { appendToDebugLogFile } from "./utils/debug-log-file";
import { appendToDebugLogFile } from "./utils/log-file";
import type { Message } from "esbuild";
export const LOGGER_LEVELS = {
none: -1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
import path from "node:path";
import { Mutex } from "miniflare";
import onExit from "signal-exit";
import { findWranglerToml } from "../config";
import { getEnvironmentVariableFactory } from "../environment-variables/factory";
import { getGlobalWranglerConfigPath } from "../global-wrangler-config-path";
import { logger, type LoggerLevel } from "../logger";

const getDebugFileDir = getEnvironmentVariableFactory({
variableName: "WRANGLER_LOG_PATH",
defaultValue() {
const configPath = findWranglerToml();
const configDir = configPath ? path.dirname(configPath) : process.cwd();
const gobalWranglerConfigDir = getGlobalWranglerConfigPath();

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

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/utils/log-file.ts#L12

Added line #L12 was not covered by tests

return path.join(configDir, ".wrangler", "logs");
RamIdeas marked this conversation as resolved.
Show resolved Hide resolved
return path.join(gobalWranglerConfigDir, "logs");

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

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/utils/log-file.ts#L14

Added line #L14 was not covered by tests
},
});

Expand Down Expand Up @@ -45,6 +44,7 @@

let hasLoggedLocation = false;
let hasLoggedError = false;
let hasSeenErrorMessage = false;

/**
* Appends a message to the log file after waiting for pending writes to complete
Expand All @@ -62,21 +62,30 @@
if (!hasLoggedLocation) {
hasLoggedLocation = true;
const relativeFilepath = path.relative(process.cwd(), debugLogFilepath);
logger.debug(`🐛 Writing logs to "${relativeFilepath}"`); // use logger.debug here to not show this message by default -- since logging to a file is no longer opt-in
logger.debug(`🪵 Writing logs to "${relativeFilepath}"`); // use logger.debug here to not show this message by default -- since logging to a file is no longer opt-in

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

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/utils/log-file.ts#L65

Added line #L65 was not covered by tests
onExit(() => {
console.info(`🐛 Logs were written to "${relativeFilepath}"`);
// only print the log file location if the log file contains an error message
// TODO(consider): recommend opening an issue with the contents of this file?
if (hasSeenErrorMessage) {
logger.warn(`🪵 Logs were written to "${relativeFilepath}"`); // use logger.warn here so not to pollute stdout -- some commands print json to stdout

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

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/utils/log-file.ts#L70

Added line #L70 was not covered by tests
}
});
}

if (!hasSeenErrorMessage) {
// TODO(consider): adding `|| messageLevel === "warn"`
hasSeenErrorMessage = messageLevel === "error";

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

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/utils/log-file.ts#L77

Added line #L77 was not covered by tests
}

RamIdeas marked this conversation as resolved.
Show resolved Hide resolved
await mutex.runWith(async () => {
try {
await ensureDirectoryExists(debugLogFilepath);
await appendFile(debugLogFilepath, entry);
} catch (err) {
if (!hasLoggedError) {
hasLoggedError = true;
console.error(`Failed to write to log file`, err);
console.error(`Would have written:`, entry);
logger.error(`Failed to write to log file`, err);
logger.error(`Would have written:`, entry);

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

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/utils/log-file.ts#L87-L88

Added lines #L87 - L88 were not covered by tests
}
}
});
Expand Down