improvement(log-level): make log level configurable via envvar (#1091)

This commit is contained in:
Waleed Latif
2025-08-21 19:40:47 -07:00
committed by GitHub
parent 4cd707fadb
commit 9ea9f2d52e
2 changed files with 28 additions and 3 deletions

View File

@@ -81,6 +81,7 @@ export const env = createEnv({
SENTRY_ORG: z.string().optional(), // Sentry organization for error tracking
SENTRY_PROJECT: z.string().optional(), // Sentry project for error tracking
SENTRY_AUTH_TOKEN: z.string().optional(), // Sentry authentication token
LOG_LEVEL: z.enum(['DEBUG', 'INFO', 'WARN', 'ERROR']).optional(), // Minimum log level to display (defaults to ERROR in production, DEBUG in development)
// External Services
JWT_SECRET: z.string().min(1).optional(), // JWT signing secret for custom tokens

View File

@@ -29,6 +29,30 @@ export enum LogLevel {
ERROR = 'ERROR',
}
/**
* Get the minimum log level from environment variable or use defaults
* - Development: DEBUG (show all logs)
* - Production: ERROR (only show errors, but can be overridden by LOG_LEVEL env var)
* - Test: ERROR (only show errors in tests)
*/
const getMinLogLevel = (): LogLevel => {
if (env.LOG_LEVEL) {
return env.LOG_LEVEL as LogLevel
}
const ENV = (env.NODE_ENV || 'development') as string
switch (ENV) {
case 'development':
return LogLevel.DEBUG
case 'production':
return LogLevel.ERROR
case 'test':
return LogLevel.ERROR
default:
return LogLevel.DEBUG
}
}
/**
* Configuration for different environments
*
@@ -40,17 +64,17 @@ export enum LogLevel {
const LOG_CONFIG = {
development: {
enabled: true,
minLevel: LogLevel.DEBUG, // Show all logs in development
minLevel: getMinLogLevel(),
colorize: true,
},
production: {
enabled: true, // Will be checked at runtime
minLevel: LogLevel.ERROR,
minLevel: getMinLogLevel(),
colorize: false,
},
test: {
enabled: false, // Disable logs in test environment
minLevel: LogLevel.ERROR,
minLevel: getMinLogLevel(),
colorize: false,
},
}