refactor: put logger as its own package with a custom config for the log level, allow it to be configurable with a default, add an init func for it, and have the config init use a temp logger
This commit is contained in:
parent
ba44b58897
commit
0281936511
|
@ -5,6 +5,7 @@ import (
|
|||
"git.lumeweb.com/LumeWeb/portal/api"
|
||||
"git.lumeweb.com/LumeWeb/portal/config"
|
||||
"git.lumeweb.com/LumeWeb/portal/interfaces"
|
||||
"git.lumeweb.com/LumeWeb/portal/logger"
|
||||
"git.lumeweb.com/LumeWeb/portal/protocols"
|
||||
"git.lumeweb.com/LumeWeb/portal/storage"
|
||||
"github.com/spf13/viper"
|
||||
|
@ -28,12 +29,10 @@ type PortalImpl struct {
|
|||
}
|
||||
|
||||
func NewPortal() interfaces.Portal {
|
||||
logger, _ := zap.NewDevelopment()
|
||||
|
||||
portal := &PortalImpl{
|
||||
apiRegistry: api.NewRegistry(),
|
||||
protocolRegistry: protocols.NewProtocolRegistry(),
|
||||
logger: logger,
|
||||
storage: nil,
|
||||
}
|
||||
|
||||
|
@ -82,7 +81,7 @@ func (p *PortalImpl) Identity() ed25519.PrivateKey {
|
|||
func (p *PortalImpl) getInitFuncs() []func() error {
|
||||
return []func() error{
|
||||
func() error {
|
||||
return config.Init(p.Logger())
|
||||
return config.Init()
|
||||
},
|
||||
func() error {
|
||||
var seed [32]byte
|
||||
|
@ -122,6 +121,12 @@ func (p *PortalImpl) getInitFuncs() []func() error {
|
|||
|
||||
return nil
|
||||
},
|
||||
|
||||
func() error {
|
||||
p.logger = logger.Get(p.Config())
|
||||
|
||||
return nil
|
||||
},
|
||||
func() error {
|
||||
return protocols.Init(p.protocolRegistry)
|
||||
},
|
||||
|
|
|
@ -14,7 +14,8 @@ var (
|
|||
}
|
||||
)
|
||||
|
||||
func Init(logger *zap.Logger) error {
|
||||
func Init() error {
|
||||
logger, _ := zap.NewDevelopment()
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("yaml")
|
||||
|
||||
|
@ -45,4 +46,5 @@ func Init(logger *zap.Logger) error {
|
|||
|
||||
func defaults() {
|
||||
viper.SetDefault("core.post-upload-limit", 1024*1024*1000)
|
||||
viper.SetDefault("core.log.level", "info")
|
||||
}
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
logger *zap.Logger
|
||||
)
|
||||
|
||||
func Get(viper *viper.Viper) *zap.Logger {
|
||||
if logger == nil {
|
||||
|
||||
// Create a new atomic level
|
||||
atomicLevel := zap.NewAtomicLevel()
|
||||
|
||||
// Set initial log level, for example, info level
|
||||
atomicLevel.SetLevel(mapLogLevel(viper.GetString("core.log.level")))
|
||||
|
||||
// Create the logger with the atomic level
|
||||
logger = zap.New(zapcore.NewCore(
|
||||
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
|
||||
zapcore.Lock(os.Stdout),
|
||||
atomicLevel,
|
||||
))
|
||||
}
|
||||
|
||||
return logger
|
||||
}
|
||||
|
||||
func mapLogLevel(level string) zapcore.Level {
|
||||
switch level {
|
||||
case "debug":
|
||||
return zapcore.DebugLevel
|
||||
case "info":
|
||||
return zapcore.InfoLevel
|
||||
case "warn":
|
||||
return zapcore.WarnLevel
|
||||
default:
|
||||
return zapcore.ErrorLevel
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue