2018-04-22 16:50:34 +00:00
|
|
|
package logutils
|
|
|
|
|
|
|
|
import (
|
2024-11-04 08:09:28 +00:00
|
|
|
"io"
|
2018-04-22 16:50:34 +00:00
|
|
|
"os"
|
|
|
|
|
2023-02-01 23:40:32 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
|
2024-11-04 08:09:28 +00:00
|
|
|
"go.uber.org/zap/zapcore"
|
2018-04-22 16:50:34 +00:00
|
|
|
)
|
|
|
|
|
2021-08-18 12:44:10 +00:00
|
|
|
type LogSettings struct {
|
2024-09-13 15:08:20 +00:00
|
|
|
Enabled bool `json:"Enabled"`
|
|
|
|
MobileSystem bool `json:"MobileSystem"`
|
|
|
|
Level string `json:"Level"`
|
|
|
|
File string `json:"File"`
|
|
|
|
MaxSize int `json:"MaxSize"`
|
|
|
|
MaxBackups int `json:"MaxBackups"`
|
|
|
|
CompressRotated bool `json:"CompressRotated"`
|
2024-11-04 08:09:28 +00:00
|
|
|
Colorized bool `json:"Colorized"` // FIXME: doesn't take effect
|
2021-08-18 12:44:10 +00:00
|
|
|
}
|
|
|
|
|
2024-11-04 08:09:28 +00:00
|
|
|
func OverrideRootLoggerWithConfig(settings LogSettings) error {
|
|
|
|
return overrideCoreWithConfig(ZapLogger().Core().(*Core), settings)
|
2019-03-05 09:03:06 +00:00
|
|
|
}
|
|
|
|
|
2024-11-04 08:09:28 +00:00
|
|
|
func overrideCoreWithConfig(core *Core, settings LogSettings) error {
|
2021-08-18 12:44:10 +00:00
|
|
|
if !settings.Enabled {
|
2024-11-04 08:09:28 +00:00
|
|
|
core.UpdateSyncer(zapcore.AddSync(io.Discard))
|
2019-03-05 09:03:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-01 13:37:13 +00:00
|
|
|
|
2024-11-04 08:09:28 +00:00
|
|
|
if settings.Level == "" {
|
|
|
|
settings.Level = "info"
|
|
|
|
}
|
|
|
|
level, err := lvlFromString(settings.Level)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
core.SetLevel(level)
|
2019-03-01 13:37:13 +00:00
|
|
|
|
2024-11-04 08:09:28 +00:00
|
|
|
if settings.MobileSystem {
|
|
|
|
core.UpdateSyncer(zapcore.Lock(os.Stdout))
|
2018-04-26 16:28:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-11-04 08:09:28 +00:00
|
|
|
|
|
|
|
if settings.File != "" {
|
|
|
|
if settings.MaxBackups == 0 {
|
2023-05-11 18:36:01 +00:00
|
|
|
// Setting MaxBackups to 0 causes all log files to be kept. Even setting MaxAge to > 0 doesn't fix it
|
|
|
|
// Docs: https://pkg.go.dev/gopkg.in/natefinch/lumberjack.v2@v2.0.0#readme-cleaning-up-old-log-files
|
2024-11-04 08:09:28 +00:00
|
|
|
settings.MaxBackups = 1
|
2023-05-11 18:36:01 +00:00
|
|
|
}
|
2024-11-04 08:09:28 +00:00
|
|
|
core.UpdateSyncer(ZapSyncerWithRotation(FileOptions{
|
|
|
|
Filename: settings.File,
|
|
|
|
MaxSize: settings.MaxSize,
|
|
|
|
MaxBackups: settings.MaxBackups,
|
|
|
|
Compress: settings.CompressRotated,
|
|
|
|
}))
|
2018-04-22 16:50:34 +00:00
|
|
|
} else {
|
2024-11-04 08:09:28 +00:00
|
|
|
core.UpdateSyncer(zapcore.Lock(os.Stderr))
|
2018-04-22 16:50:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-04 08:09:28 +00:00
|
|
|
// FIXME: remove go-libp2p logging altogether
|
2023-02-01 23:40:32 +00:00
|
|
|
// go-libp2p logger
|
2024-11-04 08:09:28 +00:00
|
|
|
{
|
|
|
|
lvl, err := logging.LevelFromString(settings.Level)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logging.SetAllLoggers(lvl)
|
2023-02-01 23:40:32 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 16:50:34 +00:00
|
|
|
return nil
|
|
|
|
}
|