dubbo.apache.org/dubbo-go/v3@v3.1.1/config/logger_config.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  	"fmt"
    22  	"strconv"
    23  )
    24  
    25  import (
    26  	getty "github.com/apache/dubbo-getty"
    27  
    28  	"github.com/creasty/defaults"
    29  
    30  	"github.com/dubbogo/gost/log/logger"
    31  )
    32  
    33  import (
    34  	"dubbo.apache.org/dubbo-go/v3/common"
    35  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    36  	"dubbo.apache.org/dubbo-go/v3/common/extension"
    37  )
    38  
    39  type LoggerConfig struct {
    40  	// logger driver default zap
    41  	Driver string `default:"zap" yaml:"driver"`
    42  
    43  	// logger level
    44  	Level string `default:"info" yaml:"level"`
    45  
    46  	// logger formatter default text
    47  	Format string `default:"text" yaml:"format"`
    48  
    49  	// supports simultaneous file and console eg: console,file default console
    50  	Appender string `default:"console" yaml:"appender"`
    51  
    52  	// logger file
    53  	File *File `yaml:"file"`
    54  }
    55  
    56  type File struct {
    57  	// log file name default dubbo.log
    58  	Name string `default:"dubbo.log" yaml:"name"`
    59  
    60  	// log max size default 100Mb
    61  	MaxSize int `default:"100" yaml:"max-size"`
    62  
    63  	// log max backups default 5
    64  	MaxBackups int `default:"5" yaml:"max-backups"`
    65  
    66  	// log file max age default 3 day
    67  	MaxAge int `default:"3" yaml:"max-age"`
    68  
    69  	Compress *bool `default:"true" yaml:"compress"`
    70  }
    71  
    72  // Prefix dubbo.logger
    73  func (l *LoggerConfig) Prefix() string {
    74  	return constant.LoggerConfigPrefix
    75  }
    76  
    77  func (l *LoggerConfig) Init() error {
    78  	var (
    79  		log logger.Logger
    80  		err error
    81  	)
    82  	if err = l.check(); err != nil {
    83  		return err
    84  	}
    85  
    86  	if log, err = extension.GetLogger(l.Driver, l.toURL()); err != nil {
    87  		return err
    88  	}
    89  	// set log
    90  	logger.SetLogger(log)
    91  	getty.SetLogger(log)
    92  	return nil
    93  }
    94  
    95  func (l *LoggerConfig) check() error {
    96  	if err := defaults.Set(l); err != nil {
    97  		return err
    98  	}
    99  	return verify(l)
   100  }
   101  
   102  func (l *LoggerConfig) toURL() *common.URL {
   103  	address := fmt.Sprintf("%s://%s", l.Driver, l.Level)
   104  	url, _ := common.NewURL(address,
   105  		common.WithParamsValue(constant.LoggerLevelKey, l.Level),
   106  		common.WithParamsValue(constant.LoggerDriverKey, l.Driver),
   107  		common.WithParamsValue(constant.LoggerFormatKey, l.Format),
   108  		common.WithParamsValue(constant.LoggerAppenderKey, l.Appender),
   109  		common.WithParamsValue(constant.LoggerFileNameKey, l.File.Name),
   110  		common.WithParamsValue(constant.LoggerFileNaxSizeKey, strconv.Itoa(l.File.MaxSize)),
   111  		common.WithParamsValue(constant.LoggerFileMaxBackupsKey, strconv.Itoa(l.File.MaxBackups)),
   112  		common.WithParamsValue(constant.LoggerFileMaxAgeKey, strconv.Itoa(l.File.MaxAge)),
   113  		common.WithParamsValue(constant.LoggerFileCompressKey, strconv.FormatBool(*l.File.Compress)),
   114  	)
   115  	return url
   116  }
   117  
   118  // DynamicUpdateProperties dynamically update properties.
   119  func (l *LoggerConfig) DynamicUpdateProperties(new *LoggerConfig) {
   120  
   121  }
   122  
   123  type LoggerConfigBuilder struct {
   124  	loggerConfig *LoggerConfig
   125  }
   126  
   127  func NewLoggerConfigBuilder() *LoggerConfigBuilder {
   128  	return &LoggerConfigBuilder{loggerConfig: &LoggerConfig{File: &File{}}}
   129  }
   130  
   131  func (lcb *LoggerConfigBuilder) SetDriver(driver string) *LoggerConfigBuilder {
   132  	lcb.loggerConfig.Driver = driver
   133  	return lcb
   134  }
   135  
   136  func (lcb *LoggerConfigBuilder) SetLevel(level string) *LoggerConfigBuilder {
   137  	lcb.loggerConfig.Level = level
   138  	return lcb
   139  }
   140  
   141  func (lcb *LoggerConfigBuilder) SetFormat(format string) *LoggerConfigBuilder {
   142  	lcb.loggerConfig.Format = format
   143  	return lcb
   144  }
   145  
   146  func (lcb *LoggerConfigBuilder) SetAppender(appender string) *LoggerConfigBuilder {
   147  	lcb.loggerConfig.Appender = appender
   148  	return lcb
   149  }
   150  
   151  func (lcb *LoggerConfigBuilder) SetFileName(name string) *LoggerConfigBuilder {
   152  	lcb.loggerConfig.File.Name = name
   153  	return lcb
   154  }
   155  
   156  func (lcb *LoggerConfigBuilder) SetFileMaxSize(maxSize int) *LoggerConfigBuilder {
   157  	lcb.loggerConfig.File.MaxSize = maxSize
   158  	return lcb
   159  }
   160  
   161  func (lcb *LoggerConfigBuilder) SetFileMaxBackups(maxBackups int) *LoggerConfigBuilder {
   162  	lcb.loggerConfig.File.MaxBackups = maxBackups
   163  	return lcb
   164  }
   165  
   166  func (lcb *LoggerConfigBuilder) SetFileMaxAge(maxAge int) *LoggerConfigBuilder {
   167  	lcb.loggerConfig.File.MaxAge = maxAge
   168  	return lcb
   169  }
   170  
   171  func (lcb *LoggerConfigBuilder) SetFileCompress(compress bool) *LoggerConfigBuilder {
   172  	lcb.loggerConfig.File.Compress = &compress
   173  	return lcb
   174  }
   175  
   176  // Build return config and set default value if nil
   177  func (lcb *LoggerConfigBuilder) Build() *LoggerConfig {
   178  	if err := defaults.Set(lcb.loggerConfig); err != nil {
   179  		return nil
   180  	}
   181  	return lcb.loggerConfig
   182  }