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:
Derrick Hammer 2024-01-15 10:02:58 -05:00
parent ba44b58897
commit 0281936511
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 56 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import (
"git.lumeweb.com/LumeWeb/portal/api" "git.lumeweb.com/LumeWeb/portal/api"
"git.lumeweb.com/LumeWeb/portal/config" "git.lumeweb.com/LumeWeb/portal/config"
"git.lumeweb.com/LumeWeb/portal/interfaces" "git.lumeweb.com/LumeWeb/portal/interfaces"
"git.lumeweb.com/LumeWeb/portal/logger"
"git.lumeweb.com/LumeWeb/portal/protocols" "git.lumeweb.com/LumeWeb/portal/protocols"
"git.lumeweb.com/LumeWeb/portal/storage" "git.lumeweb.com/LumeWeb/portal/storage"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -28,12 +29,10 @@ type PortalImpl struct {
} }
func NewPortal() interfaces.Portal { func NewPortal() interfaces.Portal {
logger, _ := zap.NewDevelopment()
portal := &PortalImpl{ portal := &PortalImpl{
apiRegistry: api.NewRegistry(), apiRegistry: api.NewRegistry(),
protocolRegistry: protocols.NewProtocolRegistry(), protocolRegistry: protocols.NewProtocolRegistry(),
logger: logger,
storage: nil, storage: nil,
} }
@ -82,7 +81,7 @@ func (p *PortalImpl) Identity() ed25519.PrivateKey {
func (p *PortalImpl) getInitFuncs() []func() error { func (p *PortalImpl) getInitFuncs() []func() error {
return []func() error{ return []func() error{
func() error { func() error {
return config.Init(p.Logger()) return config.Init()
}, },
func() error { func() error {
var seed [32]byte var seed [32]byte
@ -122,6 +121,12 @@ func (p *PortalImpl) getInitFuncs() []func() error {
return nil return nil
}, },
func() error {
p.logger = logger.Get(p.Config())
return nil
},
func() error { func() error {
return protocols.Init(p.protocolRegistry) return protocols.Init(p.protocolRegistry)
}, },

View File

@ -14,7 +14,8 @@ var (
} }
) )
func Init(logger *zap.Logger) error { func Init() error {
logger, _ := zap.NewDevelopment()
viper.SetConfigName("config") viper.SetConfigName("config")
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
@ -45,4 +46,5 @@ func Init(logger *zap.Logger) error {
func defaults() { func defaults() {
viper.SetDefault("core.post-upload-limit", 1024*1024*1000) viper.SetDefault("core.post-upload-limit", 1024*1024*1000)
viper.SetDefault("core.log.level", "info")
} }

45
logger/logger.go Normal file
View File

@ -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
}
}