2022-01-18 18:17:06 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2022-11-02 10:39:13 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2022-06-01 20:03:26 +00:00
|
|
|
|
2022-01-18 18:17:06 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
var log *zap.Logger
|
2023-11-09 11:40:19 +00:00
|
|
|
var messageLoggers map[string]*zap.Logger
|
2022-01-18 18:17:06 +00:00
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// Logger creates a zap.Logger with some reasonable defaults
|
2022-05-27 13:25:06 +00:00
|
|
|
func Logger() *zap.Logger {
|
2022-01-18 18:17:06 +00:00
|
|
|
if log == nil {
|
2022-11-02 10:39:13 +00:00
|
|
|
InitLogger("console", "stdout")
|
2022-01-18 18:17:06 +00:00
|
|
|
}
|
2022-05-27 13:25:06 +00:00
|
|
|
return log
|
2022-01-18 18:17:06 +00:00
|
|
|
}
|
2022-06-01 20:03:26 +00:00
|
|
|
|
2024-01-04 14:41:11 +00:00
|
|
|
// MessagesLogger returns a logger used for debug logging of sent/received messages
|
2023-11-09 11:40:19 +00:00
|
|
|
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-06-01 20:03:26 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-02-01 23:35:31 +00:00
|
|
|
if len(outputParts) > 2 || outputParts[0] != "file" {
|
|
|
|
panic("invalid output format")
|
|
|
|
}
|
2022-11-02 10:39:13 +00:00
|
|
|
cfg.File = "./waku.log"
|
|
|
|
}
|
2022-06-01 20:03:26 +00:00
|
|
|
}
|
2023-11-09 11:40:19 +00:00
|
|
|
if cfg.Level == logging.LevelError {
|
|
|
|
// Override default level setting
|
|
|
|
cfg.Level = logging.LevelInfo
|
|
|
|
}
|
2022-06-01 20:03:26 +00:00
|
|
|
|
2022-11-02 10:39:13 +00:00
|
|
|
logging.SetupLogging(cfg)
|
|
|
|
|
|
|
|
log = logging.Logger("gowaku").Desugar()
|
2022-06-01 20:03:26 +00:00
|
|
|
}
|