diff --git a/components/configstores/nacos/config_test.go b/components/configstores/nacos/config_test.go index 52b147582a..4e859dc13f 100644 --- a/components/configstores/nacos/config_test.go +++ b/components/configstores/nacos/config_test.go @@ -27,13 +27,13 @@ func TestParseNacosMetadata(t *testing.T) { properties := make(map[string]string) // success - metadata, err := ParseNacosMetadata(properties) + _, err = ParseNacosMetadata(properties) assert.Nil(t, err) // test set namespace namespaceId := "namespace" properties[namespaceIdKey] = namespaceId - metadata, err = ParseNacosMetadata(properties) + metadata, err := ParseNacosMetadata(properties) assert.Nil(t, err) assert.Equal(t, namespaceId, metadata.NameSpaceId) } diff --git a/components/configstores/nacos/configstore.go b/components/configstores/nacos/configstore.go index f9f90b719e..2527391795 100644 --- a/components/configstores/nacos/configstore.go +++ b/components/configstores/nacos/configstore.go @@ -16,6 +16,7 @@ package nacos import ( "context" "errors" + "path/filepath" "strconv" "strings" "sync" @@ -24,6 +25,7 @@ import ( "github.com/nacos-group/nacos-sdk-go/v2/clients" "github.com/nacos-group/nacos-sdk-go/v2/clients/config_client" "github.com/nacos-group/nacos-sdk-go/v2/common/constant" + nacoslog "github.com/nacos-group/nacos-sdk-go/v2/common/logger" "github.com/nacos-group/nacos-sdk-go/v2/vo" "mosn.io/pkg/log" @@ -94,7 +96,8 @@ func (n *ConfigStore) Init(config *configstores.StoreConfig) (err error) { } n.client = client - return nil + // replace nacos sdk log + return n.setupLogger(metadata) } // Connect to self built nacos services @@ -125,9 +128,7 @@ func (n *ConfigStore) init(address []string, timeoutMs uint64, metadata *Metadat constant.WithUsername(metadata.Username), constant.WithPassword(metadata.Password), constant.WithNotLoadCacheAtStart(true), - constant.WithLogDir(metadata.LogDir), constant.WithCacheDir(metadata.CacheDir), - constant.WithLogLevel(metadata.LogLevel), ) // 3.create config client @@ -157,9 +158,7 @@ func (n *ConfigStore) initWithACM(timeoutMs uint64, metadata *Metadata) (config_ OpenKMS: true, TimeoutMs: timeoutMs, NotLoadCacheAtStart: true, - LogDir: metadata.LogDir, CacheDir: metadata.CacheDir, - LogLevel: metadata.LogLevel, } // a more graceful way to create config client @@ -174,6 +173,35 @@ func (n *ConfigStore) initWithACM(timeoutMs uint64, metadata *Metadata) (config_ return client, nil } +func (n *ConfigStore) setupLogger(metadata *Metadata) error { + roller := log.DefaultRoller() + logFilePath := filepath.Join(metadata.LogDir, defaultLogFileName) + logger, err := log.GetOrCreateLogger(logFilePath, roller) + if err != nil { + return err + } + + errLogger := &log.SimpleErrorLog{ + Logger: logger, + } + + switch metadata.LogLevel { + case DEBUG: + errLogger.Level = log.DEBUG + case INFO: + errLogger.Level = log.INFO + case WARN: + errLogger.Level = log.WARN + case ERROR: + errLogger.Level = log.ERROR + default: + return errors.New("unknown log level") + } + + nacoslog.SetLogger(NewDefaultLogger(errLogger)) + return nil +} + // Get gets configuration from configuration store. func (n *ConfigStore) Get(ctx context.Context, request *configstores.GetRequest) ([]*configstores.ConfigurationItem, error) { // use the configuration's app_name instead of the app_id in request diff --git a/components/configstores/nacos/const.go b/components/configstores/nacos/const.go index 61022ebcdf..5fa30d7593 100644 --- a/components/configstores/nacos/const.go +++ b/components/configstores/nacos/const.go @@ -17,7 +17,8 @@ const ( defaultNamespaceId = "" // if this is not set, then nacos will use the default namespaceId. defaultGroup = "default" defaultLabel = "default" - defaultLogDir = "/tmp/layotto/nacos/log" + defaultLogDir = "/tmp/layotto/nacos/logs" + defaultLogFileName = "nacos-sdk-log" defaultCacheDir = "/tmp/layotto/nacos/cache" defaultLogLevel = "debug" defaultTimeout = 10 // second diff --git a/components/configstores/nacos/default_logger.go b/components/configstores/nacos/default_logger.go index 64fa006025..f25f403a20 100644 --- a/components/configstores/nacos/default_logger.go +++ b/components/configstores/nacos/default_logger.go @@ -20,6 +20,13 @@ import ( "mosn.io/pkg/log" ) +const ( + DEBUG = "debug" + INFO = "info" + WARN = "warn" + ERROR = "error" +) + // An adapter to implement log.LoggerInterface in agollo package. type DefaultLogger struct { logger log.ErrorLogger @@ -31,19 +38,19 @@ func NewDefaultLogger(logger log.ErrorLogger) *DefaultLogger { } } func (d *DefaultLogger) Debugf(format string, params ...interface{}) { - d.logger.Debugf(format, params) + d.logger.Debugf(format, params...) } func (d *DefaultLogger) Infof(format string, params ...interface{}) { - d.logger.Infof(format, params) + d.logger.Infof(format, params...) } func (d *DefaultLogger) Warnf(format string, params ...interface{}) { - d.logger.Warnf(format, params) + d.logger.Warnf(format, params...) } func (d *DefaultLogger) Errorf(format string, params ...interface{}) { - d.logger.Errorf(format, params) + d.logger.Errorf(format, params...) } func (d *DefaultLogger) Debug(v ...interface{}) { diff --git a/components/configstores/nacos/default_logger_test.go b/components/configstores/nacos/default_logger_test.go index c58d528a26..5181ff59b5 100644 --- a/components/configstores/nacos/default_logger_test.go +++ b/components/configstores/nacos/default_logger_test.go @@ -19,13 +19,22 @@ package nacos import ( "testing" + "github.com/stretchr/testify/assert" + "mosn.io/pkg/log" ) func TestNewDefaultLogger(t *testing.T) { - // logger - logger := NewDefaultLogger(log.DefaultLogger) - logger.Debugf("test Debugf") + mosnLogger, err := log.GetOrCreateLogger("stdout", nil) + assert.Nil(t, err) + errorLog := &log.SimpleErrorLog{ + Logger: mosnLogger, + Level: log.DEBUG, + } + + logger := NewDefaultLogger(errorLog) + logger.Debugf("test Debugf %d", 100) + logger.Debugf("test Debugf", 100) logger.Infof("test Infof") logger.Warnf("test Warnf") logger.Errorf("test Errorf") diff --git a/configs/config_nacos.json b/configs/config_nacos.json index 511bc01c65..379c61740b 100644 --- a/configs/config_nacos.json +++ b/configs/config_nacos.json @@ -45,7 +45,7 @@ ], "metadata": { "cache_dir": "./nacos/cache", - "log_dir": "./nacos/log", + "log_dir": "./nacos/logs", "log_level": "debug" } }