reformat error funcs, improved logger

This commit is contained in:
Adphi 2021-11-18 20:21:30 +01:00
parent 43357bc790
commit 8f40c6fc53
3 changed files with 210 additions and 8 deletions

View File

@ -77,6 +77,9 @@ func Aborted(err error) error {
func AbortedF(msg string, args ...interface{}) error {
return status.Errorf(codes.Aborted, msg, args...)
}
func AbortedD(err error, details ...proto.Message) error {
return statusErr(codes.Aborted, err, details...)
}
func OutOfRange(err error) error {
return status.Error(codes.OutOfRange, err.Error())
}
@ -143,3 +146,88 @@ func makeDetails(m ...proto.Message) []*anypb.Any {
}
return out
}
func InvalidArgumentf(msg string, args ...interface{}) error {
return status.Errorf(codes.InvalidArgument, msg, args...)
}
func InvalidArgumentd(err error, details ...proto.Message) error {
return statusErr(codes.InvalidArgument, err, details...)
}
func DeadlineExceededf(msg string, args ...interface{}) error {
return status.Errorf(codes.DeadlineExceeded, msg, args...)
}
func DeadlineExceededd(err error, details ...proto.Message) error {
return statusErr(codes.DeadlineExceeded, err, details...)
}
func NotFoundf(msg string, args ...interface{}) error {
return status.Errorf(codes.NotFound, msg, args...)
}
func NotFoundd(err error, details ...proto.Message) error {
return statusErr(codes.NotFound, err, details...)
}
func AlreadyExistsf(msg string, args ...interface{}) error {
return status.Errorf(codes.AlreadyExists, msg, args...)
}
func AlreadyExistsd(err error, details ...proto.Message) error {
return statusErr(codes.AlreadyExists, err, details...)
}
func PermissionDeniedf(msg string, args ...interface{}) error {
return status.Errorf(codes.PermissionDenied, msg, args...)
}
func PermissionDeniedd(err error, details ...proto.Message) error {
return statusErr(codes.PermissionDenied, err, details...)
}
func ResourceExhaustedf(msg string, args ...interface{}) error {
return status.Errorf(codes.ResourceExhausted, msg, args...)
}
func ResourceExhaustedd(err error, details ...proto.Message) error {
return statusErr(codes.ResourceExhausted, err, details...)
}
func FailedPreconditionf(msg string, args ...interface{}) error {
return status.Errorf(codes.FailedPrecondition, msg, args...)
}
func FailedPreconditiond(err error, details ...proto.Message) error {
return statusErr(codes.FailedPrecondition, err, details...)
}
func Abortedf(msg string, args ...interface{}) error {
return status.Errorf(codes.Aborted, msg, args...)
}
func Abortedd(err error, details ...proto.Message) error {
return statusErr(codes.Aborted, err, details...)
}
func OutOfRangef(msg string, args ...interface{}) error {
return status.Errorf(codes.OutOfRange, msg, args...)
}
func OutOfRanged(err error, details ...proto.Message) error {
return statusErr(codes.OutOfRange, err, details...)
}
func Unimplementedf(msg string, args ...interface{}) error {
return status.Errorf(codes.Unimplemented, msg, args...)
}
func Unimplementedd(err error, details ...proto.Message) error {
return statusErr(codes.Unimplemented, err, details...)
}
func Internalf(msg string, args ...interface{}) error {
return status.Errorf(codes.Internal, msg, args...)
}
func Internald(err error, details ...proto.Message) error {
return statusErr(codes.Internal, err, details...)
}
func Unavailablef(msg string, args ...interface{}) error {
return status.Errorf(codes.Unavailable, msg, args...)
}
func Unavailabled(err error, details ...proto.Message) error {
return statusErr(codes.Unavailable, err, details...)
}
func DataLossf(msg string, args ...interface{}) error {
return status.Errorf(codes.DataLoss, msg, args...)
}
func DataLossd(err error, details ...proto.Message) error {
return statusErr(codes.DataLoss, err, details...)
}
func Unauthenticatedf(msg string, args ...interface{}) error {
return status.Errorf(codes.Unauthenticated, msg, args...)
}
func Unauthenticatedd(err error, details ...proto.Message) error {
return statusErr(codes.Unauthenticated, err, details...)
}

View File

@ -15,7 +15,7 @@ var (
type log struct{}
func init() {
defaultLogger = &logger{FieldLogger: logrus.New()}
defaultLogger = &logger{fl: logrus.New()}
}
func SetDefault(logger Logger) {

View File

@ -2,16 +2,21 @@ package logger
import (
"fmt"
"io"
"github.com/sirupsen/logrus"
)
var (
StandardLogger Logger = &logger{FieldLogger: logrus.StandardLogger()}
standardLogger Logger = &logger{fl: logrus.StandardLogger()}
)
func StandardLogger() Logger {
return standardLogger
}
func New() Logger {
return &logger{FieldLogger: logrus.New()}
return &logger{fl: logrus.New()}
}
type Logger interface {
@ -19,6 +24,8 @@ type Logger interface {
WithFields(kv ...interface{}) Logger
WithError(err error) Logger
WriterLevel(level logrus.Level) *io.PipeWriter
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Printf(format string, args ...interface{})
@ -48,20 +55,127 @@ type Logger interface {
}
type logger struct {
logrus.FieldLogger
fl logrus.FieldLogger
}
func (l *logger) Debugf(format string, args ...interface{}) {
l.fl.Debugf(format, args...)
}
func (l *logger) Infof(format string, args ...interface{}) {
l.fl.Infof(format, args...)
}
func (l *logger) Printf(format string, args ...interface{}) {
l.fl.Printf(format, args...)
}
func (l *logger) Warnf(format string, args ...interface{}) {
l.fl.Warnf(format, args...)
}
func (l *logger) Warningf(format string, args ...interface{}) {
l.fl.Warningf(format, args...)
}
func (l *logger) Errorf(format string, args ...interface{}) {
l.fl.Errorf(format, args...)
}
func (l *logger) Fatalf(format string, args ...interface{}) {
l.fl.Fatalf(format, args...)
}
func (l *logger) Panicf(format string, args ...interface{}) {
l.fl.Panicf(format, args...)
}
func (l *logger) Debug(args ...interface{}) {
l.fl.Debug(args...)
}
func (l *logger) Info(args ...interface{}) {
l.fl.Info(args...)
}
func (l *logger) Print(args ...interface{}) {
l.fl.Print(args...)
}
func (l *logger) Warn(args ...interface{}) {
l.fl.Warn(args...)
}
func (l *logger) Warning(args ...interface{}) {
l.fl.Warning(args...)
}
func (l *logger) Error(args ...interface{}) {
l.fl.Error(args...)
}
func (l *logger) Fatal(args ...interface{}) {
l.fl.Fatal(args...)
}
func (l *logger) Panic(args ...interface{}) {
l.fl.Panic(args...)
}
func (l *logger) Debugln(args ...interface{}) {
l.fl.Debugln(args...)
}
func (l *logger) Infoln(args ...interface{}) {
l.fl.Infoln(args...)
}
func (l *logger) Println(args ...interface{}) {
l.fl.Println(args...)
}
func (l *logger) Warnln(args ...interface{}) {
l.fl.Warnln(args...)
}
func (l *logger) Warningln(args ...interface{}) {
l.fl.Warningln(args...)
}
func (l *logger) Errorln(args ...interface{}) {
l.fl.Errorln(args...)
}
func (l *logger) Fatalln(args ...interface{}) {
l.fl.Fatalln(args...)
}
func (l *logger) Panicln(args ...interface{}) {
l.fl.Panicln(args...)
}
func (l *logger) WriterLevel(level logrus.Level) *io.PipeWriter {
switch t := l.fl.(type) {
case *logrus.Logger:
return t.WriterLevel(level)
case *logrus.Entry:
return t.WriterLevel(level)
}
panic(fmt.Sprintf("unsupporter logger type %T", l.fl))
}
func (l *logger) WithField(key string, value interface{}) Logger {
return &logger{FieldLogger: l.FieldLogger.WithField(key, value)}
return &logger{fl: l.fl.WithField(key, value)}
}
func (l *logger) WithFields(kv ...interface{}) Logger {
log := &logger{fl: l.fl}
for i := 0; i < len(kv); i += 2 {
l.FieldLogger = l.FieldLogger.WithField(fmt.Sprintf("%v", kv[i]), kv[i+1])
log = &logger{fl: log.fl.WithField(fmt.Sprintf("%v", kv[i]), kv[i+1])}
}
return l
return log
}
func (l *logger) WithError(err error) Logger {
return &logger{FieldLogger: l.FieldLogger.WithError(err)}
return &logger{fl: l.fl.WithError(err)}
}