0
0
Fork 0
mirror of https://github.com/crazy-max/diun.git synced 2025-01-12 11:38:11 +00:00
crazy-max_diun/internal/grpc/logger/logger.go
CrazyMax 1115234010
Add CLI to interact with Diun through gRPC (#382)
Add simple CLI to interact with Diun through gRPC
Create image and notif proto services
Compile and validate protos through a dedicated Dockerfile and bake target
Implement proto definitions
Move server as `serve` command
New commands `image` and `notif`
Refactor command line usage doc
Better CLI error handling
Tools build constraint to manage tools deps through go modules
Add upgrade notes

Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
2021-05-26 18:18:10 +02:00

94 lines
1.9 KiB
Go

package logger
import (
"fmt"
"github.com/rs/zerolog"
"google.golang.org/grpc/grpclog"
)
func SetGrpcLogger(logger zerolog.Logger) {
grpclog.SetLoggerV2(wrap(logger))
}
func wrap(l zerolog.Logger) *bridge {
return &bridge{l}
}
type bridge struct {
zerolog.Logger
}
func (b *bridge) Info(args ...interface{}) {
b.Logger.Info().Msg(fmt.Sprint(args...))
}
func (b *bridge) Infoln(args ...interface{}) {
b.Logger.Info().Msg(fmt.Sprint(args...))
}
func (b *bridge) Infof(format string, args ...interface{}) {
b.Logger.Info().Msgf(format, args...)
}
func (b *bridge) Warning(args ...interface{}) {
b.Logger.Warn().Msg(fmt.Sprint(args...))
}
func (b *bridge) Warningln(args ...interface{}) {
b.Logger.Warn().Msg(fmt.Sprint(args...))
}
func (b *bridge) Warningf(format string, args ...interface{}) {
b.Logger.Warn().Msgf(format, args...)
}
func (b *bridge) Error(args ...interface{}) {
b.Logger.Error().Msg(fmt.Sprint(args...))
}
func (b *bridge) Errorln(args ...interface{}) {
b.Logger.Error().Msg(fmt.Sprint(args...))
}
func (b *bridge) Errorf(format string, args ...interface{}) {
b.Logger.Error().Msgf(format, args...)
}
func (b *bridge) Fatal(args ...interface{}) {
b.Logger.Fatal().Msg(fmt.Sprint(args...))
}
func (b *bridge) Fatalln(args ...interface{}) {
b.Logger.Fatal().Msg(fmt.Sprint(args...))
}
func (b *bridge) Fatalf(format string, args ...interface{}) {
b.Logger.Fatal().Msgf(format, args...)
}
func (b *bridge) V(verbosity int) bool {
// verbosity values:
// 0 = info
// 1 = warning
// 2 = error
// 3 = fatal
switch b.GetLevel() {
case zerolog.PanicLevel:
return verbosity > 3
case zerolog.FatalLevel:
return verbosity == 3
case zerolog.ErrorLevel:
return verbosity == 2
case zerolog.WarnLevel:
return verbosity == 1
case zerolog.InfoLevel:
return verbosity == 0
case zerolog.DebugLevel:
return true
case zerolog.TraceLevel:
return true
default:
return false
}
}