diff --git a/packages/better-auth/package.json b/packages/better-auth/package.json index 1c31c30ec4..4721fe30a7 100644 --- a/packages/better-auth/package.json +++ b/packages/better-auth/package.json @@ -86,15 +86,16 @@ "@nanostores/vue": "^0.10.0", "@noble/ciphers": "^0.6.0", "@simplewebauthn/browser": "^10.0.0", - "@simplewebauthn/server": "^10.0.1", + "@simplewebauthn/server": "^10.0.1", "better-call": "0.2.6", "c12": "^1.11.2", "chalk": "^5.3.0", "commander": "^12.1.0", - "defu": "^6.1.4", + "consola": "^3.2.3", + "defu": "^6.1.4", "kysely": "^0.27.4", - "nanoid": "^5.0.7", - "nanostores": "^0.11.2", + "nanoid": "^5.0.7", + "nanostores": "^0.11.2", "oslo": "^1.2.1", "prompts": "^2.4.2", "tinyexec": "^0.3.0", diff --git a/packages/better-auth/src/cli/commands/migrate.ts b/packages/better-auth/src/cli/commands/migrate.ts index 4346cc1246..2185a970e8 100644 --- a/packages/better-auth/src/cli/commands/migrate.ts +++ b/packages/better-auth/src/cli/commands/migrate.ts @@ -67,7 +67,7 @@ export const migrate = new Command("migrate") logger.info(`🔑 The migration will affect the following:`); for (const table of [...toBeCreated, ...toBeAdded]) { - logger.info( + console.log( "->", chalk.magenta(Object.keys(table.fields).join(", ")), chalk.white("fields on"), diff --git a/packages/better-auth/src/cli/get-config.ts b/packages/better-auth/src/cli/get-config.ts index f766384eec..dfc7dae879 100644 --- a/packages/better-auth/src/cli/get-config.ts +++ b/packages/better-auth/src/cli/get-config.ts @@ -86,7 +86,6 @@ export async function getConfig({ try { let configFile: BetterAuthOptions | null = null; if (configPath) { - const alias = getPathAliases(cwd); const { config } = await loadConfig<{ auth: { options: BetterAuthOptions; diff --git a/packages/better-auth/src/utils/logger.ts b/packages/better-auth/src/utils/logger.ts index cd651b4025..73130b6fc5 100644 --- a/packages/better-auth/src/utils/logger.ts +++ b/packages/better-auth/src/utils/logger.ts @@ -1,123 +1,45 @@ -type LogLevel = "debug" | "info" | "success" | "warn" | "error"; -type LogFunction = (message: any, ...args: any[]) => void; -type LoggerOptions = { +import { createConsola } from "consola"; + +const consola = createConsola({ + formatOptions: { + date: false, + colors: true, + compact: true, + }, + defaults: { + tag: "Better Auth", + }, +}); + +export const createLogger = (options?: { disabled?: boolean; - minLevel?: LogLevel; - customFormat?: ( - level: LogLevel, - message: any, - timestamp: string, - libraryName: string, - ...args: any[] - ) => string; -}; - -const LOG_LEVELS: Record = { - debug: 0, - info: 1, - success: 2, - warn: 3, - error: 4, -}; - -const LIBRARY_NAME = "Better Auth"; - -const formatDate = (date: Date): string => { - const pad = (num: number): string => num.toString().padStart(2, "0"); - - const year = date.getFullYear(); - const month = pad(date.getMonth() + 1); - const day = pad(date.getDate()); - const hours = pad(date.getHours()); - const minutes = pad(date.getMinutes()); - const seconds = pad(date.getSeconds()); - - return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; -}; - -const colorize = (text: string, color: string): string => { - const colors: Record = { - reset: "\x1b[0m", - green: "\x1b[32m", - yellow: "\x1b[33m", - red: "\x1b[31m", - blue: "\x1b[34m", - cyan: "\x1b[36m", - }; - return `${colors[color]}${text}${colors.reset}`; -}; - -const defaultFormat = ( - level: LogLevel, - message: any, - timestamp: string, - libraryName: string, - ...args: any[] -): string => { - let formattedLevel = level.toUpperCase(); - let colorizedMessage = message; - - switch (level) { - case "debug": - formattedLevel = colorize(formattedLevel, "cyan"); - break; - case "info": - formattedLevel = colorize(formattedLevel, "blue"); - break; - case "success": - formattedLevel = colorize(`✓ ${formattedLevel}`, "green"); - colorizedMessage = colorize(message, "green"); - break; - case "warn": - formattedLevel = colorize(formattedLevel, "yellow"); - break; - case "error": - formattedLevel = colorize(formattedLevel, "red"); - break; - } - - return `[${timestamp}] [${libraryName}] ${formattedLevel}: ${colorizedMessage} ${ - args.length ? JSON.stringify(args) : "" - }`; -}; - -export function createLogger(options: LoggerOptions = {}) { - const { - disabled = false, - minLevel = "info", - customFormat = defaultFormat, - } = options; - - const loggerFunction = (level: LogLevel): LogFunction => { - return (message: any, ...args: any[]) => { - if (disabled) return; - if (LOG_LEVELS[level] >= LOG_LEVELS[minLevel]) { - const timestamp = formatDate(new Date()); - const formattedMessage = customFormat( - level, - message, - timestamp, - LIBRARY_NAME, - ...args, - ); - console.log(formattedMessage); - } - }; - }; - +}) => { return { - debug: loggerFunction("debug"), - info: loggerFunction("info"), - break: () => { - console.log("\n"); + log: (...args: any[]) => { + !options?.disabled && consola.log("", ...args); }, - success: loggerFunction("success"), - warn: loggerFunction("warn"), - error: loggerFunction("error"), - setMinLevel: (level: LogLevel) => { - options.minLevel = level; + error: (...args: any[]) => { + !options?.disabled && consola.error("", ...args); + }, + warn: (...args: any[]) => { + !options?.disabled && consola.warn("", ...args); + }, + info: (...args: any[]) => { + !options?.disabled && consola.info("", ...args); + }, + debug: (...args: any[]) => { + !options?.disabled && consola.debug("", ...args); + }, + box: (...args: any[]) => { + !options?.disabled && consola.box("", ...args); + }, + success: (...args: any[]) => { + !options?.disabled && consola.success("", ...args); + }, + break: (...args: any[]) => { + !options?.disabled && console.log("\n"); }, }; -} +}; export const logger = createLogger();