Go: refactor log and config (#17684)

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-08-02 20:55:19 +08:00
committed by GitHub
parent d4ceeee4ed
commit b2521ebf51
20 changed files with 78 additions and 71 deletions

View File

@@ -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)
}

View File

@@ -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 settings
common.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, hea
return 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 {

View File

@@ -1,5 +1,5 @@
general:
heartbeat_interval: 3
heartbeat_interval: 3s
ragflow:
host: 0.0.0.0
http_port: 9380

View File

@@ -268,6 +268,7 @@ EMBEDDING_BATCH_SIZE=${EMBEDDING_BATCH_SIZE:-16}
# - Enable registration: 1
# - Disable registration: 0
REGISTER_ENABLED=1
ENABLE_REGISTER=1
# -----------------------------------------------------------------------------
# Sandbox

View File

@@ -1,5 +1,5 @@
general:
heartbeat_interval: 3
heartbeat_interval: 3s
ragflow:
host: ${RAGFLOW_HOST:-0.0.0.0}
http_port: 9380

View File

@@ -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"

View File

@@ -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.InfoLevel

View File

@@ -38,8 +38,8 @@ var (
once sync.Once
)
// Init initializes document engine
func Init() error {
// InitDocEngine initializes document engine
func InitDocEngine() error {
var initErr error
once.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 {

View File

@@ -105,7 +105,7 @@ const (
`
)
// Init initializes Redis client
// Init InitRedis initializes Redis client
func Init() error {
var initErr error
once.Do(func() {

View File

@@ -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 {

View File

@@ -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)

View File

@@ -19,6 +19,7 @@ package server
import (
"errors"
"fmt"
"ragflow/internal/common"
"ragflow/internal/server/config"
"strings"
"time"
@@ -33,7 +34,6 @@ const DefaultConnectTimeout = 5 * time.Second
var (
globalConfig *config.Config
globalViper *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 settings
func 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 ===")
}

View File

@@ -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.apiServer
apiServerConfig.Authentication.DisablePasswordLogin = false
apiServerConfig.Authentication.RegisterEnabled = true
apiServerConfig.Authentication.EnableRegister = true
if !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 {

View File

@@ -18,8 +18,8 @@ package config
import (
"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 == "" {
return fmt.Errorf("empty host of Redis configuration")
host, portStr, err := net.SplitHostPort(hostStr)
if err != nil {
return fmt.Errorf("error address format of Redis: %s", hostStr)
}
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 host == "" {
return fmt.Errorf("empty host of Redis configuration")
}
c.cacheEngine.Redis.Host = host
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)
}
}

View File

@@ -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
}
}

View File

@@ -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.Second
c.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
}

View File

@@ -45,19 +45,15 @@ func NewSystemService() *SystemService {
// ConfigResponse system configuration response
type ConfigResponse struct {
RegisterEnabled int `json:"registerEnabled"`
EnableRegister bool `json:"registerEnabled"`
DisablePasswordLogin bool `json:"disablePasswordLogin"`
}
// GetConfig get system configuration
func (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
}

View File

@@ -106,7 +106,7 @@ type UserResponse struct {
// Register user registration
func (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")
}

View File

@@ -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()

View File

@@ -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)
}
}