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

77 lines
1.6 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
2023-11-09 11:40:19 +00:00
var messageLoggers map[string]*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
}
2023-11-09 11:40:19 +00:00
// MessagesLogger returns a logger used for debug logging of receivent/sent messages
func MessagesLogger(prefix string) *zap.Logger {
if messageLoggers == nil {
messageLoggers = make(map[string]*zap.Logger)
}
logger := messageLoggers[prefix]
if logger == nil {
logger = logging.Logger(prefix + ".messages").Desugar()
messageLoggers[prefix] = logger
}
return logger
}
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"
}
}
2023-11-09 11:40:19 +00:00
if cfg.Level == logging.LevelError {
// Override default level setting
cfg.Level = logging.LevelInfo
}
2022-11-02 10:39:13 +00:00
logging.SetupLogging(cfg)
log = logging.Logger("gowaku").Desugar()
}