代码拉取完成,页面将自动刷新
package log
import (
"fmt"
"os"
"strings"
"github.com/no-src/log/level"
"gopkg.in/yaml.v3"
)
const (
consoleLoggerType = "console"
fileLoggerType = "file"
emptyLoggerType = "empty"
)
// CreateLoggerFromConfig create a logger from config file
func CreateLoggerFromConfig(configFile string) (Logger, error) {
data, err := os.ReadFile(configFile)
if err != nil {
return nil, err
}
var conf config
err = yaml.Unmarshal(data, &conf)
if err != nil {
return nil, err
}
return createLoggers(conf.Loggers)
}
func createLoggers(configs []loggerConfig) (Logger, error) {
var loggers []Logger
for _, logConf := range configs {
logger, err := createLogger(logConf)
if err != nil {
return nil, err
}
loggers = append(loggers, logger)
}
length := len(loggers)
if length == 0 {
return NewEmptyLogger(), nil
} else if length == 1 {
return loggers[0], nil
} else {
return NewMultiLogger(loggers...), nil
}
}
func createLogger(logConf loggerConfig) (Logger, error) {
var logger Logger
loggerType := strings.ToLower(logConf.Type)
switch loggerType {
case consoleLoggerType:
lvl, err := level.ParseLevel(logConf.Level)
if err != nil {
return nil, err
}
logger = NewConsoleLogger(lvl)
case fileLoggerType:
opt, err := toFileLoggerOption(logConf)
if err != nil {
return nil, err
}
fl, err := NewFileLoggerWithOption(opt)
if err != nil {
return nil, err
}
logger = fl
case emptyLoggerType:
logger = NewEmptyLogger()
default:
return nil, fmt.Errorf("unsupported logger type: %s", logConf.Type)
}
if loggerType != emptyLoggerType && logger != nil && logConf.Sample < 1 {
logger = NewDefaultSampleLogger(logger, logConf.Sample)
}
return logger, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。