package log import ( "os" "strings" ) // Level of log type Level uint8 const ( // DebugLevel defines debug log level. DebugLevel Level = iota + 1 // InfoLevel defines info log level. InfoLevel // WarnLevel defines warn log level. WarnLevel // ErrorLevel defines error log level. ErrorLevel // NoLevel defines an absent log level. NoLevel Level = 254 // Disabled disables the logger. Disabled Level = 255 ) // Logger is the interface for logger. type Logger interface { // SetLevel sets the logger level SetLevel(Level) // SetEncoding sets the logger's encoding SetEncoding(encoding string) // Printf logs a message without level Printf(template string, args ...interface{}) // Debugf logs a message at DebugLevel Debugf(template string, args ...interface{}) // Infof logs a message at InfoLevel Infof(template string, args ...interface{}) // Warnf logs a message at WarnLevel Warnf(template string, args ...interface{}) // Errorf logs a message at ErrorLevel Errorf(template string, args ...interface{}) // Output file path to write log message Output(file string) // ErrorOutput file path to write error message ErrorOutput(file string) } // String the logger level func (l Level) String() string { switch l { case DebugLevel: return "DEBUG" case ErrorLevel: return "ERROR" case WarnLevel: return "WARN" case InfoLevel: return "INFO" default: return "" } } // 实例 var logger Logger func init() { logger = Default(isEnableDebug()) } // SetLogger allows developers to customize the logger instance. func SetLogger(l Logger) { logger = l } // EnableDebug enables the development model for logging. // Deprecated func EnableDebug() { logger = Default(true) } // Printf prints a formatted message without a specified level. func Printf(format string, v ...interface{}) { logger.Printf(format, v...) } // Debugf logs a message at DebugLevel. func Debugf(template string, args ...interface{}) { logger.Debugf(template, args...) } // Infof logs a message at InfoLevel. func Infof(template string, args ...interface{}) { logger.Infof(template, args...) } // Warnf logs a message at WarnLevel. func Warnf(template string, args ...interface{}) { logger.Warnf(template, args...) } // Errorf logs a message at ErrorLevel. func Errorf(template string, args ...interface{}) { logger.Errorf(template, args...) } // isEnableDebug indicates whether the debug is enabled. func isEnableDebug() bool { return os.Getenv("HPDS_ENABLE_DEBUG") == "true" } // isJSONFormat indicates whether the log is in JSON format. func isJSONFormat() bool { return os.Getenv("HPDS_LOG_FORMAT") == "json" } func logFormat() string { return os.Getenv("HPDS_LOG_FORMAT") } func logLevel() Level { envLevel := strings.ToLower(os.Getenv("HPDS_LOG_LEVEL")) level := ErrorLevel switch envLevel { case "debug": return DebugLevel case "info": return InfoLevel case "warn": return WarnLevel case "error": return ErrorLevel } return level } func output() string { return strings.ToLower(os.Getenv("HPDS_LOG_OUTPUT")) } func errorOutput() string { return strings.ToLower(os.Getenv("HPDS_LOG_ERROR_OUTPUT")) }