RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
Go: refactor log and config (#17684)
Jin Haib2521ebf5119
diff --git a/cmd/ragflow-cli.go b/cmd/ragflow-cli.goindex 74c89d59cf..f6dcb6e6ea 100644--- a/cmd/ragflow-cli.go+++ b/cmd/ragflow-cli.go@@ -46,7 +46,7 @@ func main() {logLevel = "info"}- if err = common.Init(logLevel, common.FileOutput{}, "ragflow-cli"); err != nil {+ if err = common.InitLogger(logLevel, common.FileOutput{}, "ragflow-cli"); err != nil {fmt.Printf("Warning: Failed to initialize logger: %v\n", err)}diff --git a/cmd/ragflow_server.go b/cmd/ragflow_server.goindex 8407707673..732d4d3608 100644--- a/cmd/ragflow_server.go+++ b/cmd/ragflow_server.go@@ -256,7 +256,7 @@ func main() {logLevel = "debug"}- if err = common.Init(logLevel, common.FileOutput{Path: logFile}, serverName); err != nil {+ if err = common.InitLogger(logLevel, common.FileOutput{Path: logFile}, serverName); err != nil {panic("failed to initialize logger: " + err.Error())}@@ -341,12 +341,10 @@ func main() {}common.SyncLog()- if err = common.Init(logLevel, fileOut, serverName); err != nil {+ if err = common.InitLogger(logLevel, fileOut, serverName); err != nil {common.Error("Failed to reinitialize logger with configured level", err)}- server.SetLogger(common.Logger)-// Print all configuration settingscommon.Info(fmt.Sprintf("Starting %s server: %s, mode: %s", *arguments.mode, serverName, globalConfig.GetMode()))server.PrintAll()@@ -357,7 +355,7 @@ func main() {}// Initialize doc engine- if err = engine.Init(); err != nil {+ if err = engine.InitDocEngine(); err != nil {common.Fatal("Failed to initialize doc engine", zap.Error(err))}defer engine.Close()@@ -368,12 +366,12 @@ func main() {}defer redis.Close()- if err = storage.InitStorageFactory(); err != nil {+ if err = storage.Init(); err != nil {common.Error("Failed to initialize storage factory", err)}defer storage.CloseStorage()- if err = engine.InitMessageQueueEngine(); err != nil {+ if err = engine.InitMessageQueue(); err != nil {common.Error("Failed to initialize message queue engine", err)}@@ -527,7 +525,7 @@ func startHeartbeat(serverType common.ServerType, serverID string, port int, heareturn nil}- heartbeatReporter := utility.NewScheduledTask("Heartbeat reporter", heartBeatInterval*time.Second, func() {+ heartbeatReporter := utility.NewScheduledTask("Heartbeat reporter", heartBeatInterval, func() {if err = service.AdminServiceClient.SendHeartbeat(); err == nil {local.SetAdminStatus(0, "")} else {diff --git a/conf/service_conf.yaml b/conf/service_conf.yamlindex e275e31aca..25073a0fa3 100644--- a/conf/service_conf.yaml+++ b/conf/service_conf.yaml@@ -1,5 +1,5 @@general:- heartbeat_interval: 3+ heartbeat_interval: 3sragflow:host: 0.0.0.0http_port: 9380diff --git a/docker/.env b/docker/.envindex 1e7cb413f0..008398d4a1 100644--- a/docker/.env+++ b/docker/.env@@ -268,6 +268,7 @@ EMBEDDING_BATCH_SIZE=${EMBEDDING_BATCH_SIZE:-16}# - Enable registration: 1# - Disable registration: 0REGISTER_ENABLED=1+ENABLE_REGISTER=1# -----------------------------------------------------------------------------# Sandboxdiff --git a/docker/service_conf.yaml.template b/docker/service_conf.yaml.templateindex 06ca10dcc4..c6ba55fd22 100644--- a/docker/service_conf.yaml.template+++ b/docker/service_conf.yaml.template@@ -1,5 +1,5 @@general:- heartbeat_interval: 3+ heartbeat_interval: 3sragflow:host: ${RAGFLOW_HOST:-0.0.0.0}http_port: 9380diff --git a/internal/common/environments.go b/internal/common/environments.goindex 1e0fd91db4..ecf3d2855f 100644--- a/internal/common/environments.go+++ b/internal/common/environments.go@@ -168,7 +168,7 @@ const (EnvTCADPApiServerURL = "TCADP_APISERVER_URL"EnvTCADPApiKey = "TCADP_API_KEY"EnvRAGFlowSecretKey = "RAGFLOW_SECRET_KEY"- EnvRegisterEnabled = "REGISTER_ENABLED"+ EnvEnableRegister = "ENABLE_REGISTER"EnvDisablePasswordLogin = "DISABLE_PASSWORD_LOGIN"EnvMinioHost = "MINIO_HOST"EnvMinioRegion = "MINIO_REGION"diff --git a/internal/common/logger.go b/internal/common/logger.goindex cfef59f40d..3e694abe9a 100644--- a/internal/common/logger.go+++ b/internal/common/logger.go@@ -87,7 +87,7 @@ func logLevelName(level zapcore.Level) string {return strings.ToUpper(level.String())}-// Init initializes the global logger. stdout is always written. If file.Path+// InitLogger initializes the global logger. stdout is always written. If file.Path// is non-empty, a rotated file is also written via lumberjack.//// Callers should pass a non-empty Path so that file logging is preserved@@ -96,7 +96,7 @@ func logLevelName(level zapcore.Level) string {//// Numeric fields (MaxSize, MaxBackups, MaxAge) are defaulted to 100/10/30// when zero. Compress is taken as supplied.-func Init(level string, file FileOutput, serviceName string) error {+func InitLogger(level string, file FileOutput, serviceName string) error {zapLevel, err := parseZapLevel(level)if err != nil {zapLevel = zapcore.InfoLeveldiff --git a/internal/engine/global.go b/internal/engine/global.goindex eff017bcf3..9f4564a118 100644--- a/internal/engine/global.go+++ b/internal/engine/global.go@@ -38,8 +38,8 @@ var (once sync.Once)-// Init initializes document engine-func Init() error {+// InitDocEngine initializes document engine+func InitDocEngine() error {var initErr erroronce.Do(func() {@@ -89,12 +89,12 @@ func GetMessageQueueEngine() MessageQueue {// SetMessageQueueEngine installs the global message-queue engine. It exists// primarily as a test seam so callers can drive Start() without a real server-// config; production code uses InitMessageQueueEngine.+// config; production code uses InitMessageQueue.func SetMessageQueueEngine(mq MessageQueue) {messageQueueEngine = mq}-func InitMessageQueueEngine() error {+func InitMessageQueue() error {globalConfig := server.GetConfig()messageQueueType := globalConfig.QueueEngineType()switch messageQueueType {diff --git a/internal/engine/redis/redis.go b/internal/engine/redis/redis.goindex d32d84d9a7..3a460083c4 100644--- a/internal/engine/redis/redis.go+++ b/internal/engine/redis/redis.go@@ -105,7 +105,7 @@ const (`)-// Init initializes Redis client+// Init InitRedis initializes Redis clientfunc Init() error {var initErr erroronce.Do(func() {diff --git a/internal/handler/system.go b/internal/handler/system.goindex d5d68a5879..6b471ee10d 100644--- a/internal/handler/system.go+++ b/internal/handler/system.go@@ -72,7 +72,7 @@ func (h *SystemHandler) Healthz(c *gin.Context) {// @Accept json// @Produce json// @Success 200 {object} map[string]interface{}-// @Router /v1/system/config [get]+// @Router /api/v1/system/config [get]func (h *SystemHandler) GetConfig(c *gin.Context) {config, err := h.systemService.GetConfig()if err != nil {diff --git a/internal/ingestion/task/pipeline_real_integration_test.go b/internal/ingestion/task/pipeline_real_integration_test.goindex 5c46d9c8b5..0042ff90f4 100644--- a/internal/ingestion/task/pipeline_real_integration_test.go+++ b/internal/ingestion/task/pipeline_real_integration_test.go@@ -26,7 +26,6 @@ import ("github.com/glebarez/sqlite"- "go.uber.org/zap""gorm.io/gorm"gormlogger "gorm.io/gorm/logger")@@ -392,10 +391,9 @@ func taskRepoRoot(t *testing.T) string {func mustLoadTaskTestConfig(t *testing.T) *config.Config {t.Helper()- if err := common.Init("info", common.FileOutput{}, ""); err != nil {+ if err := common.InitLogger("info", common.FileOutput{}, ""); err != nil {t.Fatalf("init common logger: %v", err)}- server.SetLogger(zap.NewNop())configPath := filepath.Join(taskRepoRoot(t), "conf", "service_conf.yaml")if err := server.Init(configPath); err != nil {t.Fatalf("init service config from %s: %v", configPath, err)diff --git a/internal/server/config.go b/internal/server/config.goindex 9d119fdae4..ea25b22c42 100644--- a/internal/server/config.go+++ b/internal/server/config.go@@ -19,6 +19,7 @@ package serverimport ("errors""fmt"+ "ragflow/internal/common""ragflow/internal/server/config""strings""time"@@ -33,7 +34,6 @@ const DefaultConnectTimeout = 5 * time.Secondvar (globalConfig *config.ConfigglobalViper *viper.Viper- zapLogger *zap.Logger)// Init initialize configuration@@ -64,7 +64,7 @@ func Init(configPath string) error {if !errors.As(err, &configFileNotFoundError) {return fmt.Errorf("read config file error: %w", err)}- zapLogger.Info("Config file not found, using environment variables only")+ common.Info("Config file not found, using environment variables only")}// Save viper instance@@ -169,11 +169,6 @@ func GetConfig() *config.Config {return globalConfig}-// SetLogger sets the logger instance-func SetLogger(l *zap.Logger) {- zapLogger = l-}-func GetAllConfigs() ([]map[string]interface{}, error) {var allConfigs []map[string]interface{}@@ -270,14 +265,14 @@ func GetAllConfigs() ([]map[string]interface{}, error) {// PrintAll prints all configuration settingsfunc PrintAll() {if globalViper == nil {- zapLogger.Info("Configuration not initialized")+ common.Info("Configuration not initialized")return}allSettings := globalViper.AllSettings()- zapLogger.Info("=== All Configurations ===")+ common.Info("=== All Configurations ===")for key, value := range allSettings {- zapLogger.Info("config", zap.String("key", key), zap.Any("value", value))+ common.Info("config", zap.String("key", key), zap.Any("value", value))}- zapLogger.Info("=== End Configurations ===")+ common.Info("=== End Configurations ===")}diff --git a/internal/server/config/api_server_config.go b/internal/server/config/api_server_config.goindex cd4bfbd064..7184a71732 100644--- a/internal/server/config/api_server_config.go+++ b/internal/server/config/api_server_config.go@@ -20,7 +20,7 @@ import "github.com/spf13/viper"type AuthenticationConfig struct {DisablePasswordLogin bool `mapstructure:"disable_password_login"`- RegisterEnabled bool `mapstructure:"register_enabled"`+ EnableRegister bool `mapstructure:"enable_register"`}type APIServerConfig struct {@@ -63,7 +63,7 @@ func (c *Config) ParseAPIServerConfig(v *viper.Viper) error {func (c *Config) parseAuthenticationConfig(v *viper.Viper) {apiServerConfig := &c.apiServerapiServerConfig.Authentication.DisablePasswordLogin = false- apiServerConfig.Authentication.RegisterEnabled = true+ apiServerConfig.Authentication.EnableRegister = trueif !v.IsSet("authentication") {return@@ -78,16 +78,22 @@ func (c *Config) parseAuthenticationConfig(v *viper.Viper) {}if sub.IsSet("enable_register") {- apiServerConfig.Authentication.RegisterEnabled = sub.GetBool("enable_register")+ apiServerConfig.Authentication.EnableRegister = sub.GetBool("enable_register")}}func (c *Config) DisablePasswordLogin() bool {+ if c.environments.DisablePasswordLogin != nil {+ return *c.environments.DisablePasswordLogin+ }return c.apiServer.Authentication.DisablePasswordLogin}-func (c *Config) RegisterEnabled() bool {- return c.apiServer.Authentication.RegisterEnabled+func (c *Config) EnableRegister() bool {+ if c.environments.EnableRegister != nil {+ return *c.environments.EnableRegister+ }+ return c.apiServer.Authentication.EnableRegister}func (c *Config) GetAPIServerConfig() APIServerConfig {diff --git a/internal/server/config/cache_engine_config.go b/internal/server/config/cache_engine_config.goindex 7fcc5b6637..942e8a6ccb 100644--- a/internal/server/config/cache_engine_config.go+++ b/internal/server/config/cache_engine_config.go@@ -18,8 +18,8 @@ package configimport ("fmt"+ "net""strconv"- "strings""github.com/spf13/viper")@@ -69,19 +69,21 @@ func (c *Config) parseRedisConfig(v *viper.Viper) error {if sub.IsSet("host") {hostStr := sub.GetString("host")// Handle host:port format (e.g., "localhost:6379")- if hostStr == "" {+ host, portStr, err := net.SplitHostPort(hostStr)+ if err != nil {+ return fmt.Errorf("error address format of Redis: %s", hostStr)+ }++ if host == "" {return fmt.Errorf("empty host of Redis configuration")}+ c.cacheEngine.Redis.Host = host- if idx := strings.LastIndex(hostStr, ":"); idx != -1 {- c.cacheEngine.Redis.Host = hostStr[:idx]- if portStr := hostStr[idx+1:]; portStr != "" {- if port, err := strconv.Atoi(portStr); err == nil {- c.cacheEngine.Redis.Port = port- }+ if portStr != "" {+ var port int+ if port, err = strconv.Atoi(portStr); err == nil {+ c.cacheEngine.Redis.Port = port}- } else {- return fmt.Errorf("error address format of Redis: %s", hostStr)}}diff --git a/internal/server/config/environments.go b/internal/server/config/environments.goindex a5becdc8da..ae0f25e8c1 100644--- a/internal/server/config/environments.go+++ b/internal/server/config/environments.go@@ -28,8 +28,8 @@ type Environments struct {SecretKey string `mapstructure:"secret_key"`- RegisterEnabled bool `mapstructure:"register_enabled"`- DisablePasswordLogin bool `mapstructure:"disable_password_login"`+ EnableRegister *bool `mapstructure:"enable_register"`+ DisablePasswordLogin *bool `mapstructure:"disable_password_login"`DocumentEngineType string `mapstructure:"document_engine_type"`DatabaseType string `mapstructure:"database_type"`@@ -54,23 +54,27 @@ func (c *Config) GetEnvironments() error {c.environments.SecretKey = envVal}- // Load REGISTER_ENABLED from environment variable (default: true)- if envVal := common.GetEnv(common.EnvRegisterEnabled); envVal != "" {+ if envVal := common.GetEnv(common.EnvEnableRegister); envVal != "" {+ if c.environments.EnableRegister == nil {+ c.environments.EnableRegister = new(bool)+ }str := strings.ToLower(envVal)if str == "true" || str == "1" || str == "yes" {- c.environments.RegisterEnabled = true+ *c.environments.EnableRegister = true} else {- c.environments.RegisterEnabled = false+ *c.environments.EnableRegister = false}}- // Load DISABLE_PASSWORD_LOGIN from environment variable (default: false)if envVal := common.GetEnv(common.EnvDisablePasswordLogin); envVal != "" {+ if c.environments.DisablePasswordLogin == nil {+ c.environments.DisablePasswordLogin = new(bool)+ }str := strings.ToLower(envVal)if str == "true" || str == "1" || str == "yes" {- c.environments.DisablePasswordLogin = true+ *c.environments.DisablePasswordLogin = true} else {- c.environments.DisablePasswordLogin = false+ *c.environments.DisablePasswordLogin = false}}diff --git a/internal/server/config/general_config.go b/internal/server/config/general_config.goindex 4482b3aa9a..638268ab4a 100644--- a/internal/server/config/general_config.go+++ b/internal/server/config/general_config.go@@ -38,7 +38,7 @@ type GeneralConfig struct {func (c *Config) ParseGeneralConfig(v *viper.Viper) error {// Default General config- c.general.HeartbeatInterval = 3+ c.general.HeartbeatInterval = 3 * time.Secondc.general.Mode = "release"c.general.Database = "mysql"c.general.DocEngine = "elasticsearch"@@ -46,7 +46,7 @@ func (c *Config) ParseGeneralConfig(v *viper.Viper) error {c.general.CacheEngine = "redis"c.general.QueueEngine = "nats"c.general.AnalyticEngine = "clickhouse"- c.general.Language = "english"+ c.general.Language = "en"if !v.IsSet("general") {return nil@@ -120,3 +120,10 @@ func (c *Config) QueueEngineType() string {func (c *Config) AnalyticEngineType() string {return c.general.AnalyticEngine}++func (c *Config) Language() string {+ if c.environments.Language != "" {+ return c.environments.Language+ }+ return c.general.Language+}diff --git a/internal/service/system.go b/internal/service/system.goindex 50634f79d9..64112a0fb0 100644--- a/internal/service/system.go+++ b/internal/service/system.go@@ -45,19 +45,15 @@ func NewSystemService() *SystemService {// ConfigResponse system configuration responsetype ConfigResponse struct {- RegisterEnabled int `json:"registerEnabled"`+ EnableRegister bool `json:"registerEnabled"`DisablePasswordLogin bool `json:"disablePasswordLogin"`}// GetConfig get system configurationfunc (s *SystemService) GetConfig() (*ConfigResponse, error) {cfg := server.GetConfig()- registerEnabled := 1- if !cfg.RegisterEnabled() {- registerEnabled = 0- }return &ConfigResponse{- RegisterEnabled: registerEnabled,+ EnableRegister: cfg.EnableRegister(),DisablePasswordLogin: cfg.DisablePasswordLogin(),}, nil}diff --git a/internal/service/user.go b/internal/service/user.goindex c6ee432435..58cbf8edfd 100644--- a/internal/service/user.go+++ b/internal/service/user.go@@ -106,7 +106,7 @@ type UserResponse struct {// Register user registrationfunc (s *UserService) Register(ctx context.Context, req *RegisterRequest) (*entity.User, common.ErrorCode, error) {cfg := server.GetConfig()- if !cfg.RegisterEnabled() {+ if !cfg.EnableRegister() {return nil, common.CodeOperatingError, fmt.Errorf("user registration is disabled")}diff --git a/internal/storage/storage_factory.go b/internal/storage/storage_factory.goindex e65897f79e..e47f09583f 100644--- a/internal/storage/storage_factory.go+++ b/internal/storage/storage_factory.go@@ -42,8 +42,8 @@ func GetStorageFactory() *StorageFactory {return globalFactory}-// InitStorageFactory initializes the storage factory with configuration-func InitStorageFactory() error {+// Init initializes the storage factory with configuration+func Init() error {factory := GetStorageFactory()globalConfig := server.GetConfig()diff --git a/internal/tokenizer/tokenizer_concurrent_test.go b/internal/tokenizer/tokenizer_concurrent_test.goindex f154e0e016..175d8651b6 100644--- a/internal/tokenizer/tokenizer_concurrent_test.go+++ b/internal/tokenizer/tokenizer_concurrent_test.go@@ -30,7 +30,7 @@ import (func init() {// Initialize logger for tests- if err := common.Init("info", common.FileOutput{}, "tokenizer_test"); err != nil {+ if err := common.InitLogger("info", common.FileOutput{}, "tokenizer_test"); err != nil {fmt.Printf("Failed to initialize logger: %v\n", err)}}