fix(logging): keep debug logging in a packaged debug build
import.meta.env.DEV is true only under the vite dev server, but scripts/build-android.sh produces the debug APK with a plain `bun run build` — so the logger defaulted to warn there too and the debug package lost every frontend message from logcat. `bun run android:logs` is a documented workflow that depends on them. vite now defines __JT_DEBUG_BUILD__ from Tauri's TAURI_ENV_DEBUG, which the CLI sets while running beforeBuildCommand. The decision is split into a pure resolveDefaultLogLevel(isDevServer, isDebugBuild) because neither import.meta.env.DEV nor a vite define can be varied from inside a test. Also replaces the pinned requirement counts in extract-traces.test.ts with invariants. The pins guarded nothing the computeCoverage fixtures don't already cover, while forcing every branch that adds a requirement to edit the numbers — the comment above them had become a ledger of which branch contributed which row. TRACES: | DR-204 | UT-201
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
parseLogLevel,
|
||||
readStoredLogLevel,
|
||||
resetLogLevel,
|
||||
resolveDefaultLogLevel,
|
||||
setLogLevel,
|
||||
type LogLevel,
|
||||
} from "./logger";
|
||||
@@ -316,3 +317,28 @@ describe("localStorage override", () => {
|
||||
expect(spies.error).toHaveBeenCalledWith("[Boot] still reaches the console");
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* A packaged *debug* build must still log at debug level.
|
||||
*
|
||||
* `import.meta.env.DEV` is true only under the vite dev server. Both the debug
|
||||
* and the release APK are produced by a plain `vite build`
|
||||
* (scripts/build-android.sh runs `bun run build`), so gating on DEV alone
|
||||
* silences the debug APK too — and `bun run android:logs` is a documented
|
||||
* workflow that depends on those messages reaching logcat.
|
||||
*
|
||||
* TRACES: | DR-204 | UT-201
|
||||
*/
|
||||
describe("resolveDefaultLogLevel", () => {
|
||||
it("logs at debug under the dev server", () => {
|
||||
expect(resolveDefaultLogLevel(true, false)).toBe("debug");
|
||||
});
|
||||
|
||||
it("logs at debug in a packaged debug build", () => {
|
||||
expect(resolveDefaultLogLevel(false, true)).toBe("debug");
|
||||
});
|
||||
|
||||
it("stays quiet in a packaged release build", () => {
|
||||
expect(resolveDefaultLogLevel(false, false)).toBe("warn");
|
||||
});
|
||||
});
|
||||
|
||||
+29
-1
@@ -115,9 +115,37 @@ export function parseLogLevel(raw: unknown): LogLevel | null {
|
||||
return normalised in LEVEL_RANK ? (normalised as LogLevel) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Injected by vite (see `vite.config.js`) from Tauri's `TAURI_ENV_DEBUG`, which
|
||||
* the CLI sets while running `beforeBuildCommand`. Undefined outside a Tauri
|
||||
* build — a bare `vite build`, or vitest — hence the `typeof` guard.
|
||||
*/
|
||||
declare const __JT_DEBUG_BUILD__: boolean | undefined;
|
||||
|
||||
/** Is this bundle inside a *debug* Tauri package (a debug APK, say)? */
|
||||
function isDebugBuild(): boolean {
|
||||
return typeof __JT_DEBUG_BUILD__ !== "undefined" && __JT_DEBUG_BUILD__ === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The default level, as a pure function of the two build facts it depends on.
|
||||
*
|
||||
* Split out from {@link defaultLogLevel} so it can be tested — neither
|
||||
* `import.meta.env.DEV` nor a vite `define` can be varied from inside a test.
|
||||
*
|
||||
* 🔴 `isDevServer` alone is not enough. `import.meta.env.DEV` is true only under
|
||||
* the vite dev server, and `scripts/build-android.sh` produces the debug APK
|
||||
* with a plain `bun run build` — so gating on it silences the debug package as
|
||||
* thoroughly as the release one, and `bun run android:logs` stops showing
|
||||
* anything from the frontend.
|
||||
*/
|
||||
export function resolveDefaultLogLevel(isDevServer: boolean, isDebugBuild: boolean): LogLevel {
|
||||
return isDevServer || isDebugBuild ? "debug" : "warn";
|
||||
}
|
||||
|
||||
/** The level a build defaults to with no override present. */
|
||||
export function defaultLogLevel(): LogLevel {
|
||||
return import.meta.env?.DEV ? "debug" : "warn";
|
||||
return resolveDefaultLogLevel(Boolean(import.meta.env?.DEV), isDebugBuild());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user