diff --git a/src/enum/spec/proj/config.in.ts b/src/enum/spec/proj/config.in.ts new file mode 100644 index 0000000..1c8708e --- /dev/null +++ b/src/enum/spec/proj/config.in.ts @@ -0,0 +1,61 @@ +enum LogLevel { + DEBUG = "debug", + INFO = "info", + WARN = "warn", + ERROR = "error", +} + +enum Environment { + Development = "development", + Staging = "staging", + Production = "production", +} + +interface AppConfig { + environment: Environment; + logLevel: LogLevel; + port: number; + databaseUrl?: string; +} + +class ConfigManager { + private config: AppConfig; + + constructor() { + this.config = { + environment: Environment.Development, + logLevel: LogLevel.INFO, + port: 3000, + }; + } + + setEnvironment(env: Environment): void { + this.config.environment = env; + + if (env === Environment.Production) { + this.config.logLevel = LogLevel.ERROR; + this.config.port = 8080; + } else if (env === Environment.Staging) { + this.config.logLevel = LogLevel.WARN; + this.config.port = 4000; + } + } + + setLogLevel(level: LogLevel): void { + this.config.logLevel = level; + } + + getConfig(): Readonly { + return { ...this.config }; + } + + isProduction(): boolean { + return this.config.environment === Environment.Production; + } +} + +const configManager = new ConfigManager(); +configManager.setEnvironment(Environment.Staging); +configManager.setLogLevel(LogLevel.DEBUG); + +export { LogLevel, Environment, ConfigManager, type AppConfig }; \ No newline at end of file diff --git a/src/enum/spec/proj/config.out.ts b/src/enum/spec/proj/config.out.ts new file mode 100644 index 0000000..bcb8600 --- /dev/null +++ b/src/enum/spec/proj/config.out.ts @@ -0,0 +1,63 @@ +const LogLevel = { + DEBUG: "debug", + INFO: "info", + WARN: "warn", + ERROR: "error", +} as const; +type LogLevelType = typeof LogLevel[keyof typeof LogLevel]; + +const Environment = { + Development: "development", + Staging: "staging", + Production: "production", +} as const; +type EnvironmentType = typeof Environment[keyof typeof Environment]; + +interface AppConfig { + environment: EnvironmentType; + logLevel: LogLevelType; + port: number; + databaseUrl?: string; +} + +class ConfigManager { + private config: AppConfig; + + constructor() { + this.config = { + environment: Environment.Development, + logLevel: LogLevel.INFO, + port: 3000, + }; + } + + setEnvironment(env: EnvironmentType): void { + this.config.environment = env; + + if (env === Environment.Production) { + this.config.logLevel = LogLevel.ERROR; + this.config.port = 8080; + } else if (env === Environment.Staging) { + this.config.logLevel = LogLevel.WARN; + this.config.port = 4000; + } + } + + setLogLevel(level: LogLevelType): void { + this.config.logLevel = level; + } + + getConfig(): Readonly { + return { ...this.config }; + } + + isProduction(): boolean { + return this.config.environment === Environment.Production; + } +} + +const configManager = new ConfigManager(); +configManager.setEnvironment(Environment.Staging); +configManager.setLogLevel(LogLevel.DEBUG); + +export { LogLevel, Environment, ConfigManager, type AppConfig }; \ No newline at end of file diff --git a/src/enum/spec/proj/logger.in.ts b/src/enum/spec/proj/logger.in.ts new file mode 100644 index 0000000..847baf9 --- /dev/null +++ b/src/enum/spec/proj/logger.in.ts @@ -0,0 +1,64 @@ +import { LogLevel, Environment, ConfigManager } from './config.in.ts'; + +class Logger { + private configManager: ConfigManager; + private name: string; + + constructor(name: string, configManager: ConfigManager) { + this.name = name; + this.configManager = configManager; + } + + private shouldLog(level: LogLevel): boolean { + const config = this.configManager.getConfig(); + const levels = [LogLevel.DEBUG, LogLevel.INFO, LogLevel.WARN, LogLevel.ERROR]; + return levels.indexOf(level) >= levels.indexOf(config.logLevel); + } + + private formatMessage(level: LogLevel, message: string): string { + const timestamp = new Date().toISOString(); + const env = this.configManager.getConfig().environment; + return `[${timestamp}] [${env.toUpperCase()}] [${level.toUpperCase()}] ${this.name}: ${message}`; + } + + debug(message: string): void { + if (this.shouldLog(LogLevel.DEBUG)) { + console.log(this.formatMessage(LogLevel.DEBUG, message)); + } + } + + info(message: string): void { + if (this.shouldLog(LogLevel.INFO)) { + console.log(this.formatMessage(LogLevel.INFO, message)); + } + } + + warn(message: string): void { + if (this.shouldLog(LogLevel.WARN)) { + console.warn(this.formatMessage(LogLevel.WARN, message)); + } + } + + error(message: string): void { + if (this.shouldLog(LogLevel.ERROR)) { + console.error(this.formatMessage(LogLevel.ERROR, message)); + } + } +} + +// Usage example +const configManager = new ConfigManager(); +configManager.setLogLevel(LogLevel.INFO); + +const appLogger = new Logger('App', configManager); +const dbLogger = new Logger('Database', configManager); + +appLogger.debug('This will not be logged'); +appLogger.info('Application starting'); +appLogger.warn('Low memory warning'); + +dbLogger.error('Connection failed'); + +// Test different environments +configManager.setEnvironment(Environment.Production); +appLogger.info('Running in production mode'); \ No newline at end of file diff --git a/src/enum/spec/proj/logger.out.ts b/src/enum/spec/proj/logger.out.ts new file mode 100644 index 0000000..7392b37 --- /dev/null +++ b/src/enum/spec/proj/logger.out.ts @@ -0,0 +1,64 @@ +import { LogLevel, Environment, ConfigManager } from './config.out.ts'; + +class Logger { + private configManager: ConfigManager; + private name: string; + + constructor(name: string, configManager: ConfigManager) { + this.name = name; + this.configManager = configManager; + } + + private shouldLog(level: typeof LogLevel[keyof typeof LogLevel]): boolean { + const config = this.configManager.getConfig(); + const levels = [LogLevel.DEBUG, LogLevel.INFO, LogLevel.WARN, LogLevel.ERROR]; + return levels.indexOf(level) >= levels.indexOf(config.logLevel); + } + + private formatMessage(level: typeof LogLevel[keyof typeof LogLevel], message: string): string { + const timestamp = new Date().toISOString(); + const env = this.configManager.getConfig().environment; + return `[${timestamp}] [${env.toUpperCase()}] [${level.toUpperCase()}] ${this.name}: ${message}`; + } + + debug(message: string): void { + if (this.shouldLog(LogLevel.DEBUG)) { + console.log(this.formatMessage(LogLevel.DEBUG, message)); + } + } + + info(message: string): void { + if (this.shouldLog(LogLevel.INFO)) { + console.log(this.formatMessage(LogLevel.INFO, message)); + } + } + + warn(message: string): void { + if (this.shouldLog(LogLevel.WARN)) { + console.warn(this.formatMessage(LogLevel.WARN, message)); + } + } + + error(message: string): void { + if (this.shouldLog(LogLevel.ERROR)) { + console.error(this.formatMessage(LogLevel.ERROR, message)); + } + } +} + +// Usage example +const configManager = new ConfigManager(); +configManager.setLogLevel(LogLevel.INFO); + +const appLogger = new Logger('App', configManager); +const dbLogger = new Logger('Database', configManager); + +appLogger.debug('This will not be logged'); +appLogger.info('Application starting'); +appLogger.warn('Low memory warning'); + +dbLogger.error('Connection failed'); + +// Test different environments +configManager.setEnvironment(Environment.Production); +appLogger.info('Running in production mode'); \ No newline at end of file