forked from trent_larson/crowd-funder-for-time-pwa
feat(logging): implement configurable log levels via VITE_LOG_LEVEL
Add comprehensive logging configuration system with environment variable support. Environment files now include appropriate log levels per build mode: - Development: debug (maximum visibility) - Production: warn (minimal noise) - Testing: info (balanced output) Includes smart default behavior based on platform and environment, enhanced logger methods for level checking, and comprehensive documentation. All existing logging calls remain backward compatible. Closes logging configuration request
This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
* Enhanced logger with self-contained database logging
|
||||
*
|
||||
* Provides comprehensive logging with console and database output.
|
||||
* Supports configurable log levels via VITE_LOG_LEVEL environment variable.
|
||||
*
|
||||
* @author Matthew Raymer
|
||||
* @version 2.0.0
|
||||
* @version 2.1.0
|
||||
* @since 2025-01-25
|
||||
*/
|
||||
|
||||
@@ -46,6 +47,42 @@ export function safeStringify(obj: unknown) {
|
||||
const isElectron = process.env.VITE_PLATFORM === "electron";
|
||||
const isProduction = process.env.NODE_ENV === "production";
|
||||
|
||||
// Log level configuration via environment variable
|
||||
const LOG_LEVELS = {
|
||||
error: 0,
|
||||
warn: 1,
|
||||
info: 2,
|
||||
debug: 3,
|
||||
} as const;
|
||||
|
||||
type LogLevel = keyof typeof LOG_LEVELS;
|
||||
|
||||
// Parse VITE_LOG_LEVEL environment variable
|
||||
const getLogLevel = (): LogLevel => {
|
||||
const envLogLevel = process.env.VITE_LOG_LEVEL?.toLowerCase();
|
||||
|
||||
if (envLogLevel && envLogLevel in LOG_LEVELS) {
|
||||
return envLogLevel as LogLevel;
|
||||
}
|
||||
|
||||
// Default log levels based on environment
|
||||
if (isProduction && !isElectron) {
|
||||
return "warn"; // Production web: warnings and errors only
|
||||
} else if (isElectron) {
|
||||
return "error"; // Electron: errors only
|
||||
} else {
|
||||
return "info"; // Development/Capacitor: info and above
|
||||
}
|
||||
};
|
||||
|
||||
const currentLogLevel = getLogLevel();
|
||||
const currentLevelValue = LOG_LEVELS[currentLogLevel];
|
||||
|
||||
// Helper function to check if a log level should be displayed
|
||||
const shouldLog = (level: LogLevel): boolean => {
|
||||
return LOG_LEVELS[level] <= currentLevelValue;
|
||||
};
|
||||
|
||||
// Track initialization state to prevent circular dependencies
|
||||
let isInitializing = true;
|
||||
|
||||
@@ -105,11 +142,11 @@ async function logToDatabase(
|
||||
}
|
||||
}
|
||||
|
||||
// Enhanced logger with self-contained database methods
|
||||
// Enhanced logger with self-contained database methods and log level control
|
||||
export const logger = {
|
||||
debug: (message: string, ...args: unknown[]) => {
|
||||
// Debug logs are very verbose - only show in development mode for web
|
||||
if (!isProduction && !isElectron) {
|
||||
// Debug logs only show if VITE_LOG_LEVEL allows it
|
||||
if (shouldLog("debug")) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.debug(message, ...args);
|
||||
}
|
||||
@@ -117,11 +154,8 @@ export const logger = {
|
||||
},
|
||||
|
||||
log: (message: string, ...args: unknown[]) => {
|
||||
// Regular logs - show in development or for capacitor, but quiet for Electron
|
||||
if (
|
||||
(!isProduction && !isElectron) ||
|
||||
process.env.VITE_PLATFORM === "capacitor"
|
||||
) {
|
||||
// Regular logs - show if VITE_LOG_LEVEL allows info level
|
||||
if (shouldLog("info")) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(message, ...args);
|
||||
}
|
||||
@@ -132,11 +166,7 @@ export const logger = {
|
||||
},
|
||||
|
||||
info: (message: string, ...args: unknown[]) => {
|
||||
if (
|
||||
process.env.NODE_ENV !== "production" ||
|
||||
process.env.VITE_PLATFORM === "capacitor" ||
|
||||
process.env.VITE_PLATFORM === "electron"
|
||||
) {
|
||||
if (shouldLog("info")) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.info(message, ...args);
|
||||
}
|
||||
@@ -147,8 +177,7 @@ export const logger = {
|
||||
},
|
||||
|
||||
warn: (message: string, ...args: unknown[]) => {
|
||||
// Always show warnings, but for Electron, suppress routine database warnings
|
||||
if (!isElectron || !message.includes("[CapacitorPlatformService]")) {
|
||||
if (shouldLog("warn")) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn(message, ...args);
|
||||
}
|
||||
@@ -159,9 +188,10 @@ export const logger = {
|
||||
},
|
||||
|
||||
error: (message: string, ...args: unknown[]) => {
|
||||
// Errors will always be logged to console
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(message, ...args);
|
||||
if (shouldLog("error")) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(message, ...args);
|
||||
}
|
||||
|
||||
// Database logging
|
||||
const messageString = safeStringify(message);
|
||||
@@ -175,11 +205,11 @@ export const logger = {
|
||||
},
|
||||
|
||||
toConsoleAndDb: async (message: string, isError = false): Promise<void> => {
|
||||
// Console output
|
||||
if (isError) {
|
||||
// Console output based on log level
|
||||
if (isError && shouldLog("error")) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(message);
|
||||
} else {
|
||||
} else if (!isError && shouldLog("info")) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(message);
|
||||
}
|
||||
@@ -194,6 +224,12 @@ export const logger = {
|
||||
error: (message: string) =>
|
||||
logToDatabase(`[${componentName}] ${message}`, "error"),
|
||||
}),
|
||||
|
||||
// Log level information methods
|
||||
getCurrentLevel: (): LogLevel => currentLogLevel,
|
||||
getCurrentLevelValue: (): number => currentLevelValue,
|
||||
isLevelEnabled: (level: LogLevel): boolean => shouldLog(level),
|
||||
getAvailableLevels: (): LogLevel[] => Object.keys(LOG_LEVELS) as LogLevel[],
|
||||
};
|
||||
|
||||
// Function to manually mark initialization as complete
|
||||
|
||||
Reference in New Issue
Block a user