Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update DevLogger with channels and local mute config (#2159)
Leendert de Borst1d5d8e3c987a
diff --git a/apps/browser-extension/.gitignore b/apps/browser-extension/.gitignoreindex 7c0502804..d582013ff 100644--- a/apps/browser-extension/.gitignore+++ b/apps/browser-extension/.gitignore@@ -25,6 +25,9 @@ web-ext.config.ts*.sln*.sw?+# Local dev-log channel filter (see src/utils/devLogger/devlog.local.example.ts)+src/utils/devLogger/devlog.local.ts+# Test related output filesplaywright-reporttests/screenshotsdiff --git a/apps/browser-extension/src/entrypoints/content.ts b/apps/browser-extension/src/entrypoints/content.tsindex 6d5dc80b3..4826bd012 100644--- a/apps/browser-extension/src/entrypoints/content.ts+++ b/apps/browser-extension/src/entrypoints/content.ts@@ -11,7 +11,7 @@ import { initializeWebAuthnInterceptor } from '@/entrypoints/contentScript/WebAuimport { isAvAutofillAllowed, isAvSuppressSave } from '@/utils/autofill/Autofill';import { DEFAULT_POPUP_TYPE, isPopupType, popupTypeForFieldType, POPUP_TYPES, type PopupType } from '@/utils/autofill/PopupTypes';-import { devLog } from '@/utils/DevLogger';+import { devLog } from '@/utils/devLogger/DevLogger';import type { Item } from '@/utils/dist/core/models/vault';import { FormDetector } from '@/utils/formDetector/FormDetector';import { LocalPreferencesService } from '@/utils/LocalPreferencesService';diff --git a/apps/browser-extension/src/entrypoints/popup/hooks/useVaultMutate.ts b/apps/browser-extension/src/entrypoints/popup/hooks/useVaultMutate.tsindex bd38d17bf..68b58115a 100644--- a/apps/browser-extension/src/entrypoints/popup/hooks/useVaultMutate.ts+++ b/apps/browser-extension/src/entrypoints/popup/hooks/useVaultMutate.ts@@ -2,7 +2,7 @@ import { useCallback, useRef } from 'react';import { useDb } from '@/entrypoints/popup/context/DbContext';-import { devLog } from '@/utils/DevLogger';+import { devLog } from '@/utils/devLogger/DevLogger';import { EncryptionUtility } from '@/utils/EncryptionUtility';import { sendMessage } from '@/utils/messaging/ExtensionMessaging';import type { VaultMutationOptions, VaultMutationScope } from '@/utils/types/VaultMutationScope';diff --git a/apps/browser-extension/src/utils/DevLogger.ts b/apps/browser-extension/src/utils/DevLogger.tsdeleted file mode 100644index 311716e69..000000000--- a/apps/browser-extension/src/utils/DevLogger.ts+++ /dev/null@@ -1,10 +0,0 @@-/**- * Log a timestamped trace message for debugging. Only logs in dev builds.- */-export function devLog(message: string, ...args: unknown[]): void {- if (!import.meta.env.DEV) {- return;- }- const time = new Date().toISOString().slice(11, 23);- console.info(`[${time}] ${message}`, ...args);-}diff --git a/apps/browser-extension/src/utils/VaultMergeService.ts b/apps/browser-extension/src/utils/VaultMergeService.tsindex cb2ac9404..fc9189900 100644--- a/apps/browser-extension/src/utils/VaultMergeService.ts+++ b/apps/browser-extension/src/utils/VaultMergeService.ts@@ -2,7 +2,7 @@ import initSqlJs, { Database, SqlJsStatic, SqlValue } from 'sql.js';import { browser } from 'wxt/browser';import { TRASH_RETENTION_DAYS } from '@/utils/constants/vault';-import { devLog } from '@/utils/DevLogger';+import { devLog } from '@/utils/devLogger/DevLogger';import init, { getPruneTableQueries, getSyncableTableNames, mergeVaults, pruneVault } from './dist/core/rust/aliasvault_core.js';diff --git a/apps/browser-extension/src/utils/devLogger/DevLogger.ts b/apps/browser-extension/src/utils/devLogger/DevLogger.tsnew file mode 100644index 000000000..38f8df9eb--- /dev/null+++ b/apps/browser-extension/src/utils/devLogger/DevLogger.ts@@ -0,0 +1,109 @@+/**+ * Dev-only, channel-scoped console logging.+ *+ * Every trace carries a channel tag as its first bracketed token and allows to+ * focus traces on one subsystem and mute the rest.+ *+ * Which channels are shown is decided by an optional, gitignored `devlog.local.ts`+ * file: copy `devlog.local.example.ts` to `devlog.local.ts`, edit it, and restart the+ * dev server. With no such file, every channel is shown.+ */++/**+ * A channel filter. `only` is an allowlist (when non-empty, ONLY these channels log);+ * `mute` is a denylist applied afterwards. Channel names are matched case-insensitively+ * and without the surrounding brackets (e.g. `'V2Push'`, not `'[V2Push]'`).+ */+export type DevLogFilter = {+ /** Allowlist. When present and non-empty, only these channels are shown. */+ only?: string[];+ /** Denylist. These channels are never shown (applied after `only`). */+ mute?: string[];+};++type LocalConfigModule = { default?: DevLogFilter; devLogFilter?: DevLogFilter };++/*+ * Optionally pull in the gitignored `devlog.local.ts`.+ */+const localConfigModules = import.meta.glob<LocalConfigModule>('./devlog.local.{ts,js}', { eager: true });+const firstLocalConfig = Object.values(localConfigModules)[0];+const fileFilter: DevLogFilter = firstLocalConfig?.default ?? firstLocalConfig?.devLogFilter ?? {};++/**+ * Pull the channel name out of a message's leading `[Tag]`. Returns `''` for an+ * unchannelled message (no leading bracket), those are always shown.+ * @param message - the log message+ */+function parseChannel(message: string): string {+ const match = /^\s*\[([^\]]+)\]/.exec(message);+ return match ? match[1] : '';+}++/**+ * Whether a channel should be shown under the currently active filter.+ * @param channel - channel name (without brackets); `''` (unchannelled) is always shown+ */+function isChannelEnabled(channel: string): boolean {+ if (!channel) {+ return true;+ }+ const ch = channel.toLowerCase();+ const only = (fileFilter.only ?? []).map(c => c.toLowerCase());+ const mute = (fileFilter.mute ?? []).map(c => c.toLowerCase());+ if (only.length > 0 && !only.includes(ch)) {+ return false;+ }+ return !mute.includes(ch);+}++/**+ * Emit a channel-filtered, timestamped dev trace.+ * @param level - console method to use+ * @param message - the message (should start with a `[Channel]` tag)+ * @param args - additional console arguments+ */+function emit(level: 'info' | 'warn' | 'error', message: string, args: unknown[]): void {+ if (!import.meta.env.DEV) {+ return;+ }+ if (!isChannelEnabled(parseChannel(message))) {+ return;+ }+ const time = new Date().toISOString().slice(11, 23);+ const line = `[${time}] ${message}`;+ if (level === 'error') {+ console.error(line, ...args);+ } else if (level === 'warn') {+ console.warn(line, ...args);+ } else {+ console.info(line, ...args);+ }+}++/**+ * Log a timestamped, channel-scoped trace for debugging.+ * @param message - the message (should start with a `[Channel]` tag)+ * @param args - additional console arguments+ */+export function devLog(message: string, ...args: unknown[]): void {+ emit('info', message, args);+}++/**+ * Like {@link devLog} but at `console.warn` level.+ * @param message - the message (should start with a `[Channel]` tag)+ * @param args - additional console arguments+ */+export function devWarn(message: string, ...args: unknown[]): void {+ emit('warn', message, args);+}++/**+ * Like {@link devLog} but at `console.error` level.+ * @param message - the message (should start with a `[Channel]` tag)+ * @param args - additional console arguments+ */+export function devError(message: string, ...args: unknown[]): void {+ emit('error', message, args);+}diff --git a/apps/browser-extension/src/utils/devLogger/devlog.local.example.ts b/apps/browser-extension/src/utils/devLogger/devlog.local.example.tsnew file mode 100644index 000000000..0c1580bca--- /dev/null+++ b/apps/browser-extension/src/utils/devLogger/devlog.local.example.ts@@ -0,0 +1,21 @@+/**+ * Local dev-log channel filter. Copy to `devlog.local.ts` to control which dev traces are shown.+ */++import type { DevLogFilter } from './DevLogger';++const devLogFilter: DevLogFilter = {+ /**+ * Allowlist: when non-empty, only these channels are shown. Leave empty to show all.+ * Example: focus on vault sync only: only: ['V2Push', 'V2Pull', 'V2Sync']+ */+ only: [],++ /**+ * Denylist: these channels are hidden after `only` is applied.+ * Example: everything except the noisy autofill traces: mute: ['Autofill', 'FormDetector']+ */+ mute: [],+};++export default devLogFilter;diff --git a/apps/browser-extension/src/utils/formDetector/FormDetector.ts b/apps/browser-extension/src/utils/formDetector/FormDetector.tsindex 89ec2b5ca..20f03a456 100644--- a/apps/browser-extension/src/utils/formDetector/FormDetector.ts+++ b/apps/browser-extension/src/utils/formDetector/FormDetector.ts@@ -1,4 +1,4 @@-import { devLog } from "@/utils/DevLogger";+import { devLog } from "@/utils/devLogger/DevLogger";import { CombinedEmailVerificationPatterns, CombinedFieldExclusionPatterns, CombinedFieldPatterns, CombinedGenderOptionPatterns, CombinedStopWords, FieldPatternEntry, includeTerms, specificIncludeTerms } from "./FieldPatterns";import { DetectedFieldType, FormFields } from "./types/FormFields";