Igor Sirotin 679391999f
feat_: LogOnPanic linter (#5969)
* feat_: LogOnPanic linter

* fix_: add missing defer LogOnPanic

* chore_: make vendor

* fix_: tests, address pr comments

* fix_: address pr comments
2024-10-23 21:33:05 +01:00

40 lines
820 B
Go

package utils
import (
"strconv"
"fmt"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func URI(path string, line int) string {
return path + ":" + strconv.Itoa(line)
}
func ZapURI(path string, line int) zap.Field {
return zap.Field{
Type: zapcore.StringType,
Key: "uri",
String: URI(path, line),
}
}
func BuildLogger(level zapcore.Level) *zap.Logger {
// Initialize logger with colors
loggerConfig := zap.NewDevelopmentConfig()
loggerConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
loggerConfig.Level = zap.NewAtomicLevelAt(level)
loggerConfig.Development = false
loggerConfig.DisableStacktrace = true
logger, err := loggerConfig.Build()
if err != nil {
fmt.Printf("failed to initialize logger: %s", err.Error())
os.Exit(1)
}
return logger.Named("main")
}