Skip to content

Commit

Permalink
Ensure logging only once, and remove unnecssary message
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjessop committed Feb 6, 2025
1 parent d247c81 commit d56f91f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 46 deletions.
2 changes: 0 additions & 2 deletions packages/wrangler/e2e/__snapshots__/pages-dev.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ exports[`Pages 'wrangler pages dev' > should merge (with override) \`wrangler.to
- {"name":"DO_BINDING_1_TOML","class_name":"DO_1_TOML","script_name":"DO_SCRIPT_1_TOML"}
- {"name":"DO_BINDING_2_TOML","class_name":"DO_2_TOML","script_name":"DO_SCRIPT_2_TOML"}
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:
- DO_BINDING_1_TOML: NEW_DO_1 (defined in NEW_DO_SCRIPT_1 [not connected])
Expand Down Expand Up @@ -37,7 +36,6 @@ Your worker has access to the following bindings:
- VAR2: "VAR_2_TOML"
- VAR3: "(hidden)"
Service bindings & durable object bindings connect to other \`wrangler dev\` processes running locally, with their connection status indicated by [connected] or [not connected]. For more details, refer to https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/#local-development
Use "wrangler dev --remote" to run both your Worker and all bindings remotely (https://developers.cloudflare.com/workers/testing/local-development/#develop-using-remote-resources-and-bindings).
▲ [WARNING] Using Workers AI always accesses your Cloudflare account in order to run AI models, and so will incur usage charges even in local development.
"
`;
4 changes: 2 additions & 2 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12140,8 +12140,8 @@ export default{
"Total Upload: xx KiB / gzip: xx KiB
Worker Startup Time: 100 ms
Your worker has access to the following bindings:
- Images:
- Name: IMAGES_BIND
- Images:
- Name: IMAGES_BIND
Uploaded test-name (TIMINGS)
Deployed test-name triggers (TIMINGS)
https://test-name.test-sub-domain.workers.dev
Expand Down
24 changes: 4 additions & 20 deletions packages/wrangler/src/__tests__/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import registerDevHotKeys from "../dev/hotkeys";
import { getWorkerAccountAndContext } from "../dev/remote";
import { FatalError } from "../errors";
import { CI } from "../is-ci";
import { logger } from "../logger";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { mockConsoleMethods } from "./helpers/mock-console";
import { useMockIsTTY } from "./helpers/mock-istty";
Expand Down Expand Up @@ -129,6 +130,7 @@ describe.sequential("wrangler dev", () => {
...mswSuccessOauthHandlers,
...mswSuccessUserHandlers
);
logger.clearHistory();
});

runInTempDir();
Expand Down Expand Up @@ -1212,13 +1214,10 @@ describe.sequential("wrangler dev", () => {
Your worker has access to the following bindings:
- Durable Objects:
- NAME_1: CLASS_1 [simulated locally]
- NAME_1: CLASS_1
- NAME_2: CLASS_2 (defined in SCRIPT_A [not connected])
- NAME_3: CLASS_3 [simulated locally]
- NAME_3: CLASS_3
- NAME_4: CLASS_4 (defined in SCRIPT_B [not connected])
Use \\"wrangler dev --remote\\" to run both your Worker and all bindings remotely (https://developers.cloudflare.com/workers/testing/local-development/#develop-using-remote-resources-and-bindings).
"
`);
expect(std.warn).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -1315,9 +1314,6 @@ describe.sequential("wrangler dev", () => {
- VAR_MULTI_LINE_2: \\"(hidden)\\"
- EMPTY: \\"(hidden)\\"
- UNQUOTED: \\"(hidden)\\"
Use \\"wrangler dev --remote\\" to run both your Worker and all bindings remotely (https://developers.cloudflare.com/workers/testing/local-development/#develop-using-remote-resources-and-bindings).
"
`);
});
Expand Down Expand Up @@ -1348,9 +1344,6 @@ describe.sequential("wrangler dev", () => {
Your worker has access to the following bindings:
- Vars:
- CUSTOM_VAR: \\"(hidden)\\"
Use \\"wrangler dev --remote\\" to run both your Worker and all bindings remotely (https://developers.cloudflare.com/workers/testing/local-development/#develop-using-remote-resources-and-bindings).
"
`);
});
Expand Down Expand Up @@ -1874,9 +1867,6 @@ describe.sequential("wrangler dev", () => {
- Services:
- WorkerA: A [not connected]
- WorkerB: B [not connected]
Use \\"wrangler dev --remote\\" to run both your Worker and all bindings remotely (https://developers.cloudflare.com/workers/testing/local-development/#develop-using-remote-resources-and-bindings).
"
`);
expect(std.warn).toMatchInlineSnapshot(`""`);
Expand All @@ -1900,9 +1890,6 @@ describe.sequential("wrangler dev", () => {
- Services:
- WorkerA: A [not connected]
- WorkerB: B [not connected]
Use \\"wrangler dev --remote\\" to run both your Worker and all bindings remotely (https://developers.cloudflare.com/workers/testing/local-development/#develop-using-remote-resources-and-bindings).
"
`);
expect(std.warn).toMatchInlineSnapshot(`""`);
Expand Down Expand Up @@ -1933,9 +1920,6 @@ describe.sequential("wrangler dev", () => {
- variable: 123
- overriden: \\"(hidden)\\"
- SECRET: \\"(hidden)\\"
Use \\"wrangler dev --remote\\" to run both your Worker and all bindings remotely (https://developers.cloudflare.com/workers/testing/local-development/#develop-using-remote-resources-and-bindings).
"
`);
});
Expand Down
14 changes: 8 additions & 6 deletions packages/wrangler/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class Logger {
constructor() {}

private overrideLoggerLevel?: LoggerLevel;
private onceHistory = new Set<string>();

get loggerLevel() {
return this.overrideLoggerLevel ?? getLoggerLevel();
Expand Down Expand Up @@ -107,7 +108,6 @@ export class Logger {
Logger.#afterLogHook?.();
}

static onceHistory = new Set();
get once() {
return {
info: (...args: unknown[]) => this.doLogOnce("info", args),
Expand All @@ -116,14 +116,16 @@ export class Logger {
error: (...args: unknown[]) => this.doLogOnce("error", args),
};
}
doLogOnce(messageLevel: Exclude<LoggerLevel, "none">, args: unknown[]) {
// using this.constructor.onceHistory, instead of hard-coding Logger.onceHistory, allows for subclassing
const { onceHistory } = this.constructor as typeof Logger;

clearHistory() {
this.onceHistory.clear();
}

doLogOnce(messageLevel: Exclude<LoggerLevel, "none">, args: unknown[]) {
const cacheKey = `${messageLevel}: ${args.join(" ")}`;

if (!onceHistory.has(cacheKey)) {
onceHistory.add(cacheKey);
if (!this.onceHistory.has(cacheKey)) {
this.onceHistory.add(cacheKey);
this.doLog(messageLevel, args);
}
}
Expand Down
24 changes: 8 additions & 16 deletions packages/wrangler/src/utils/print-bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export function printBindings(
analytics_engine_datasets,
text_blobs,
browser,
ai,
images,
ai,
version_metadata,
unsafe,
vars,
Expand Down Expand Up @@ -133,11 +133,7 @@ export function printBindings(

return {
key: name,
value: script_name
? value
: addSuffix(value, {
isSimulatedLocally: true,
}),
value: value,
};
}
),
Expand Down Expand Up @@ -356,12 +352,16 @@ export function printBindings(
}

if (images !== undefined) {
const addImagesSuffix = createAddSuffix({
isProvisioning: context.provisioning,
isLocalDev: !!context.imagesLocalMode,
});
output.push({
name: friendlyBindingNames.images,
entries: [
{
key: "Name",
value: addLocalSuffix(images.binding, !!context.imagesLocalMode),
value: addImagesSuffix(images.binding),
},
],
});
Expand Down Expand Up @@ -487,7 +487,7 @@ export function printBindings(
}

if (context.local) {
logger.log(
logger.once.log(
`Your Worker and resources are simulated locally via Miniflare. For more information, see: https://developers.cloudflare.com/workers/testing/local-development.\n`
);
}
Expand Down Expand Up @@ -522,12 +522,6 @@ export function printBindings(
`\nService bindings & durable object bindings connect to other \`wrangler dev\` processes running locally, with their connection status indicated by ${chalk.green("[connected]")} or ${chalk.red("[not connected]")}. For more details, refer to https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/#local-development\n`
);
}

if (context.local) {
logger.log(
`\nUse "wrangler dev --remote" to run both your Worker and all bindings remotely (https://developers.cloudflare.com/workers/testing/local-development/#develop-using-remote-resources-and-bindings).\n`
);
}
}

function normalizeValue(value: string | symbol | undefined) {
Expand All @@ -543,8 +537,6 @@ function normalizeValue(value: string | symbol | undefined) {
*
* The suffix is only for local dev so it can be used to determine whether a binding is
* simulated locally or connected to a remote resource.
*
* We don't show the suffix when provisioning because the bindings are not yet available in local dev.
*/
function createAddSuffix({
isProvisioning = false,
Expand Down

0 comments on commit d56f91f

Please sign in to comment.