dubbo.apache.org/dubbo-go/v3@v3.1.1/config/logger_config_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package config 19 20 import ( 21 "testing" 22 ) 23 24 import ( 25 "github.com/dubbogo/gost/log/logger" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestLoggerInit(t *testing.T) { 31 t.Run("empty use default", func(t *testing.T) { 32 err := Load(WithPath("./testdata/config/logger/empty_log.yaml")) 33 assert.Nil(t, err) 34 assert.NotNil(t, rootConfig) 35 loggerConfig := rootConfig.Logger 36 assert.NotNil(t, loggerConfig) 37 }) 38 39 t.Run("use config", func(t *testing.T) { 40 err := Load(WithPath("./testdata/config/logger/log.yaml")) 41 assert.Nil(t, err) 42 loggerConfig := rootConfig.Logger 43 assert.NotNil(t, loggerConfig) 44 // default 45 logger.Info("hello") 46 }) 47 48 t.Run("use config with file", func(t *testing.T) { 49 err := Load(WithPath("./testdata/config/logger/file_log.yaml")) 50 assert.Nil(t, err) 51 loggerConfig := rootConfig.Logger 52 assert.NotNil(t, loggerConfig) 53 logger.Debug("debug") 54 logger.Info("info") 55 logger.Warn("warn") 56 logger.Error("error") 57 logger.Debugf("%s", "debug") 58 logger.Infof("%s", "info") 59 logger.Warnf("%s", "warn") 60 logger.Errorf("%s", "error") 61 }) 62 } 63 64 func TestNewLoggerConfigBuilder(t *testing.T) { 65 config := NewLoggerConfigBuilder(). 66 SetDriver("zap"). 67 SetLevel("info"). 68 SetFileName("dubbo.log"). 69 SetFileMaxAge(10). 70 Build() 71 72 assert.NotNil(t, config) 73 74 assert.Equal(t, config.File.Name, "dubbo.log") 75 assert.Equal(t, config.Driver, "zap") 76 assert.Equal(t, config.Level, "info") 77 assert.Equal(t, config.File.MaxAge, 10) 78 79 // default value 80 assert.Equal(t, config.Appender, "console") 81 assert.Equal(t, config.Format, "text") 82 assert.Equal(t, config.File.MaxSize, 100) 83 assert.Equal(t, *config.File.Compress, true) 84 assert.Equal(t, config.File.MaxBackups, 5) 85 }