github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/infra/conf/log.go (about)

     1  package conf
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/xmplusdev/xmcore/app/log"
     7  	clog "github.com/xmplusdev/xmcore/common/log"
     8  )
     9  
    10  func DefaultLogConfig() *log.Config {
    11  	return &log.Config{
    12  		AccessLogType: log.LogType_None,
    13  		ErrorLogType:  log.LogType_Console,
    14  		ErrorLogLevel: clog.Severity_Warning,
    15  	}
    16  }
    17  
    18  type LogConfig struct {
    19  	AccessLog string `json:"access"`
    20  	ErrorLog  string `json:"error"`
    21  	LogLevel  string `json:"loglevel"`
    22  	DNSLog    bool   `json:"dnsLog"`
    23  }
    24  
    25  func (v *LogConfig) Build() *log.Config {
    26  	if v == nil {
    27  		return nil
    28  	}
    29  	config := &log.Config{
    30  		ErrorLogType:  log.LogType_Console,
    31  		AccessLogType: log.LogType_Console,
    32  		EnableDnsLog:  v.DNSLog,
    33  	}
    34  
    35  	if v.AccessLog == "none" {
    36  		config.AccessLogType = log.LogType_None
    37  	} else if len(v.AccessLog) > 0 {
    38  		config.AccessLogPath = v.AccessLog
    39  		config.AccessLogType = log.LogType_File
    40  	}
    41  	if v.ErrorLog == "none" {
    42  		config.ErrorLogType = log.LogType_None
    43  	} else if len(v.ErrorLog) > 0 {
    44  		config.ErrorLogPath = v.ErrorLog
    45  		config.ErrorLogType = log.LogType_File
    46  	}
    47  
    48  	level := strings.ToLower(v.LogLevel)
    49  	switch level {
    50  	case "debug":
    51  		config.ErrorLogLevel = clog.Severity_Debug
    52  	case "info":
    53  		config.ErrorLogLevel = clog.Severity_Info
    54  	case "error":
    55  		config.ErrorLogLevel = clog.Severity_Error
    56  	case "none":
    57  		config.ErrorLogType = log.LogType_None
    58  		config.AccessLogType = log.LogType_None
    59  	default:
    60  		config.ErrorLogLevel = clog.Severity_Warning
    61  	}
    62  	return config
    63  }