Skip to content

Commit

Permalink
fix: respect WRANGLER_LOG in dev (#8061)
Browse files Browse the repository at this point in the history
* fix debug issue

* changeset

* add logger.resetLoggerLevel()

* add WRANGLER_LOG test
  • Loading branch information
emily-shen authored Feb 12, 2025
1 parent 1b07419 commit 35710e5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 69 deletions.
7 changes: 7 additions & 0 deletions .changeset/wicked-pets-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: respect `WRANGLER_LOG` in `wrangler dev`

Previously, `--log-level=debug` was the only way to see debug logs in `wrangler dev`, which was unlike all other commands.
82 changes: 18 additions & 64 deletions packages/wrangler/src/__tests__/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ describe.sequential("wrangler dev", () => {
msw.resetHandlers();
spy.mockClear();
setSpy.mockClear();
logger.resetLoggerLevel();
});

async function runWranglerUntilConfig(
Expand Down Expand Up @@ -1211,7 +1212,7 @@ describe.sequential("wrangler dev", () => {
);
expect(std.out).toMatchInlineSnapshot(`
"Your Worker and resources are simulated locally via Miniflare. For more information, see: https://developers.cloudflare.com/workers/testing/local-development.
Your worker has access to the following bindings:
- Durable Objects:
- NAME_1: CLASS_1
Expand Down Expand Up @@ -1304,7 +1305,7 @@ describe.sequential("wrangler dev", () => {
expect(std.out).toMatchInlineSnapshot(`
"Using vars defined in .dev.vars
Your Worker and resources are simulated locally via Miniflare. For more information, see: https://developers.cloudflare.com/workers/testing/local-development.
Your worker has access to the following bindings:
- Vars:
- VAR_1: \\"(hidden)\\"
Expand Down Expand Up @@ -1340,7 +1341,7 @@ describe.sequential("wrangler dev", () => {
expect(std.out).toMatchInlineSnapshot(`
"Using vars defined in .dev.vars.custom
Your Worker and resources are simulated locally via Miniflare. For more information, see: https://developers.cloudflare.com/workers/testing/local-development.
Your worker has access to the following bindings:
- Vars:
- CUSTOM_VAR: \\"(hidden)\\"
Expand All @@ -1356,64 +1357,6 @@ describe.sequential("wrangler dev", () => {
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: Not enough arguments following: site]`
);

expect(std).toMatchInlineSnapshot(`
Object {
"debug": "",
"err": "X [ERROR] Not enough arguments following: site
",
"info": "",
"out": "
wrangler dev [script]
👂 Start a local server for developing your Worker
POSITIONALS
script The path to an entry point for your Worker [string]
GLOBAL FLAGS
-c, --config Path to Wrangler configuration file [string]
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
OPTIONS
--name Name of the Worker [string]
--compatibility-date Date to use for compatibility checks [string]
--compatibility-flags, --compatibility-flag Flags to use for compatibility checks [array]
--latest Use the latest version of the Workers runtime [boolean] [default: true]
--assets Static assets to be served. Replaces Workers Sites. [string]
--no-bundle Skip internal build steps and directly deploy script [boolean] [default: false]
--ip IP address to listen on [string]
--port Port to listen on [number]
--inspector-port Port for devtools to connect to [number]
--routes, --route Routes to upload [array]
--host Host to forward requests to, defaults to the zone of project [string]
--local-protocol Protocol to listen to requests on, defaults to http. [choices: \\"http\\", \\"https\\"]
--https-key-path Path to a custom certificate key [string]
--https-cert-path Path to a custom certificate [string]
--local-upstream Host to act as origin in local mode, defaults to dev.host or route [string]
--upstream-protocol Protocol to forward requests to host on, defaults to https. [choices: \\"http\\", \\"https\\"]
--var A key-value pair to be injected into the script as a variable [array]
--define A key-value pair to be substituted in the script [array]
--alias A module pair to be substituted in the script [array]
--jsx-factory The function that is called for each JSX element [string]
--jsx-fragment The function that is called for each JSX fragment [string]
--tsconfig Path to a custom tsconfig.json file [string]
-r, --remote Run on the global Cloudflare network with access to production resources [boolean] [default: false]
--minify Minify the script [boolean]
--node-compat Enable Node.js compatibility [boolean]
--persist-to Specify directory to use for local persistence (defaults to .wrangler/state) [string]
--live-reload Auto reload HTML pages when change is detected in local mode [boolean]
--test-scheduled Test scheduled events by visiting /__scheduled in browser [boolean] [default: false]
--log-level Specify logging level [choices: \\"debug\\", \\"info\\", \\"log\\", \\"warn\\", \\"error\\", \\"none\\"] [default: \\"log\\"]
--show-interactive-dev-session Show interactive dev session (defaults to true if the terminal supports interactivity) [boolean]
--experimental-vectorize-bind-to-prod Bind to production Vectorize indexes in local development mode [boolean] [default: false]
--experimental-images-local-mode Use a local lower-fidelity implementation of the Images binding [boolean] [default: false]",
"warn": "",
}
`);
});

it("should error if --legacy-assets and --site are used together", async () => {
Expand Down Expand Up @@ -1831,6 +1774,17 @@ describe.sequential("wrangler dev", () => {
"
`);
});

it("should also set log level using WRANGLER_LOG'", async () => {
fs.writeFileSync("index.js", `export default {};`);
vi.stubEnv("WRANGLER_LOG", "none");
await runWranglerUntilConfig("dev index.js --inspect");
expect(std.warn).toMatchInlineSnapshot(`""`);

vi.stubEnv("WRANGLER_LOG", "debug");
await runWranglerUntilConfig("dev index.js");
expect(std.debug).toContain(".env file not found at");
});
});

describe("--show-interactive-dev-session", () => {
Expand Down Expand Up @@ -1862,7 +1816,7 @@ describe.sequential("wrangler dev", () => {
await runWranglerUntilConfig("dev index.js");
expect(std.out).toMatchInlineSnapshot(`
"Your Worker and resources are simulated locally via Miniflare. For more information, see: https://developers.cloudflare.com/workers/testing/local-development.
Your worker has access to the following bindings:
- Services:
- WorkerA: A [not connected]
Expand All @@ -1885,7 +1839,7 @@ describe.sequential("wrangler dev", () => {
await runWranglerUntilConfig("dev index.js");
expect(std.out).toMatchInlineSnapshot(`
"Your Worker and resources are simulated locally via Miniflare. For more information, see: https://developers.cloudflare.com/workers/testing/local-development.
Your worker has access to the following bindings:
- Services:
- WorkerA: A [not connected]
Expand Down Expand Up @@ -1914,7 +1868,7 @@ describe.sequential("wrangler dev", () => {
expect(std.out).toMatchInlineSnapshot(`
"Using vars defined in .dev.vars
Your Worker and resources are simulated locally via Miniflare. For more information, see: https://developers.cloudflare.com/workers/testing/local-development.
Your worker has access to the following bindings:
- Vars:
- variable: 123
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/__tests__/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe("metrics", () => {
afterEach(() => {
vi.unstubAllEnvs();
isCISpy.mockClear();
logger.resetLoggerLevel();
});

describe("getMetricsDispatcher()", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import { writeWranglerConfig } from "../helpers/write-wrangler-config";
describe("pages build env", () => {
const std = mockConsoleMethods();
runInTempDir();
const originalLoggerLevel = logger.loggerLevel;

afterEach(() => {
logger.loggerLevel = originalLoggerLevel;
logger.resetLoggerLevel();
});
beforeEach(() => {
vi.stubEnv("PAGES_ENVIRONMENT", "production");
Expand Down
3 changes: 0 additions & 3 deletions packages/wrangler/src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import type {
} from "./deployment-bundle/worker";
import type { WorkerRegistry } from "./dev-registry";
import type { CfAccount } from "./dev/create-worker-preview";
import type { LoggerLevel } from "./logger";
import type { EnablePagesAssetsServiceBindingOptions } from "./miniflare-cli/types";
import type { watch } from "chokidar";
import type { Json } from "miniflare";
Expand Down Expand Up @@ -310,8 +309,6 @@ export const dev = createCommand({
"log-level": {
choices: ["debug", "info", "log", "warn", "error", "none"] as const,
describe: "Specify logging level",
// Yargs requires this to type log-level properly
default: "log" as LoggerLevel,
},
"show-interactive-dev-session": {
describe:
Expand Down
4 changes: 4 additions & 0 deletions packages/wrangler/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export class Logger {
this.overrideLoggerLevel = val;
}

resetLoggerLevel() {
this.overrideLoggerLevel = undefined;
}

columns = process.stdout.columns;

debug = (...args: unknown[]) => this.doLog("debug", args);
Expand Down

0 comments on commit 35710e5

Please sign in to comment.