增加日志系统

This commit is contained in:
yml2213
2026-05-29 12:40:12 +08:00
parent eb80a3ba84
commit 3b8b65ab56
6 changed files with 181 additions and 4 deletions
+26
View File
@@ -15,6 +15,7 @@ type Config struct {
JWTSecret string
Storage StorageConfig
SMS SMSConfig
Log LogConfig
}
type StorageConfig struct {
@@ -33,6 +34,13 @@ type SMSConfig struct {
AliyunLoginTemplateCode string
}
type LogConfig struct {
Level string
Dir string
EnableConsole bool
EnableFile bool
}
func Load() Config {
return Config{
AppEnv: getEnv("APP_ENV", "development"),
@@ -56,6 +64,12 @@ func Load() Config {
AliyunSignName: getEnv("ALIYUN_SMS_SIGN_NAME", ""),
AliyunLoginTemplateCode: getEnv("ALIYUN_SMS_LOGIN_TEMPLATE_CODE", ""),
},
Log: LogConfig{
Level: getEnv("LOG_LEVEL", "info"),
Dir: getEnv("LOG_DIR", "logs"),
EnableConsole: getEnvBool("LOG_ENABLE_CONSOLE", true),
EnableFile: getEnvBool("LOG_ENABLE_FILE", true),
},
}
}
@@ -78,3 +92,15 @@ func getEnvInt(key string, fallback int) int {
}
return parsed
}
func getEnvBool(key string, fallback bool) bool {
value := os.Getenv(key)
if value == "" {
return fallback
}
parsed, err := strconv.ParseBool(value)
if err != nil {
return fallback
}
return parsed
}