go-waku/waku/v2/utils/logger.go

60 lines
1.1 KiB
Go
Raw Normal View History

package utils
import (
2022-11-02 10:39:13 +00:00
"strings"
logging "github.com/ipfs/go-log/v2"
"go.uber.org/zap"
)
var log *zap.Logger
// Logger creates a zap.Logger with some reasonable defaults
2022-05-27 13:25:06 +00:00
func Logger() *zap.Logger {
if log == nil {
2022-11-02 10:39:13 +00:00
InitLogger("console", "stdout")
}
2022-05-27 13:25:06 +00:00
return log
}
2022-07-25 15:28:17 +00:00
// InitLogger initializes a global logger using an specific encoding
2022-11-02 10:39:13 +00:00
func InitLogger(encoding string, output string) {
cfg := logging.GetConfig()
if encoding == "json" {
cfg.Format = logging.JSONOutput
} else if encoding == "nocolor" {
cfg.Format = logging.PlaintextOutput
} else {
cfg.Format = logging.ColorizedOutput
}
2022-11-02 10:39:13 +00:00
if output == "stdout" || output == "" {
cfg.Stdout = true
cfg.Stderr = false
} else {
if encoding == "console" {
cfg.Format = logging.PlaintextOutput
}
cfg.Stdout = false
cfg.Stderr = false
outputParts := strings.Split(output, ":")
if len(outputParts) == 2 {
cfg.File = outputParts[1]
} else {
if len(outputParts) > 2 || outputParts[0] != "file" {
panic("invalid output format")
}
2022-11-02 10:39:13 +00:00
cfg.File = "./waku.log"
}
}
2022-11-02 10:39:13 +00:00
logging.SetupLogging(cfg)
log = logging.Logger("gowaku").Desugar()
logging.SetAllLoggers(logging.LevelInfo)
}