2022-01-18 14:17:06 -04:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2022-11-02 06:39:13 -04:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2022-06-01 16:03:26 -04:00
|
|
|
|
2022-01-18 14:17:06 -04:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2023-09-11 10:24:05 -04:00
|
|
|
var log *zap.Logger
|
2022-01-18 14:17:06 -04:00
|
|
|
|
2022-04-25 23:31:26 +04:00
|
|
|
// Logger creates a zap.Logger with some reasonable defaults
|
2022-05-27 09:25:06 -04:00
|
|
|
func Logger() *zap.Logger {
|
2022-01-18 14:17:06 -04:00
|
|
|
if log == nil {
|
2022-11-02 06:39:13 -04:00
|
|
|
InitLogger("console", "stdout")
|
2022-01-18 14:17:06 -04:00
|
|
|
}
|
2022-05-27 09:25:06 -04:00
|
|
|
return log
|
2022-01-18 14:17:06 -04:00
|
|
|
}
|
2022-06-01 16:03:26 -04:00
|
|
|
|
2022-07-25 11:28:17 -04:00
|
|
|
// InitLogger initializes a global logger using an specific encoding
|
2022-11-02 06:39:13 -04: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 16:03:26 -04:00
|
|
|
}
|
|
|
|
|
2022-11-02 06:39:13 -04: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 19:35:31 -04:00
|
|
|
if len(outputParts) > 2 || outputParts[0] != "file" {
|
|
|
|
panic("invalid output format")
|
|
|
|
}
|
2022-11-02 06:39:13 -04:00
|
|
|
cfg.File = "./waku.log"
|
|
|
|
}
|
2022-06-01 16:03:26 -04:00
|
|
|
}
|
|
|
|
|
2022-11-02 06:39:13 -04:00
|
|
|
logging.SetupLogging(cfg)
|
|
|
|
|
|
|
|
log = logging.Logger("gowaku").Desugar()
|
2023-02-01 19:35:31 -04:00
|
|
|
|
|
|
|
logging.SetAllLoggers(logging.LevelInfo)
|
2022-06-01 16:03:26 -04:00
|
|
|
}
|