Skip to content

Commit

Permalink
Log file fixes (#4488)
Browse files Browse the repository at this point in the history
* rename debug-log-file to reflect non-debug-only behaviour

* change default log directory to global config dir
instead of project root .wrangler
frameworks may be watching the project root .wrangler dir for changes
causing an infinite loop of update logs
e.g. #4482

* only print log file location if seen error message

+ update log emoji to 🪵 instead of 🐛
since it's not just a debug log file anymore

* print absolute log file path
since it's now in the home directory, no longer near the cwd

* use console.warn instead of logger.warn
so not to have include the *very* visible bright-yellow [WARNING] indicator
  • Loading branch information
RamIdeas authored Nov 22, 2023
1 parent 5f54f72 commit 3bd5723
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .changeset/honest-jeans-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"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

Also includes a fix for commands with `--json` where the log file location message would cause stdout to not be valid JSON. That message now goes to stderr.
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 { appendFile, mkdir, readFile } from "node:fs/promises";
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();

return path.join(configDir, ".wrangler", "logs");
return path.join(gobalWranglerConfigDir, "logs");
},
});

Expand Down Expand Up @@ -45,6 +44,7 @@ const mutex = new Mutex();

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 @@ -61,22 +61,32 @@ ${message}

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 "${debugLogFilepath}"`); // use logger.debug here to not show this message by default -- since logging to a file is no longer opt-in
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) {
// use console.*warn* here so not to pollute stdout -- some commands print json to stdout
// use *console*.warn here so not to have include the *very* visible bright-yellow [WARNING] indicator
console.warn(`🪵 Logs were written to "${debugLogFilepath}"`);
}
});
}

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

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);
}
}
});
Expand Down

0 comments on commit 3bd5723

Please sign in to comment.