small fixups
Some checks failed
pr / test (pull_request) Failing after 1m6s
lint / lint (pull_request) Successful in 10m45s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-10-12 12:36:08 +03:00
parent ced277ff57
commit 8a9243636e
7 changed files with 31 additions and 48 deletions

View File

@ -2,7 +2,6 @@ package broker
import (
"context"
"fmt"
"sync"
"go.unistack.org/micro/v3/broker"
@ -207,7 +206,7 @@ func (m *memoryBroker) publish(ctx context.Context, msgs []*broker.Message, opts
}
} else if sub.opts.AutoAck {
if err = ms.Ack(); err != nil {
m.opts.Logger.Error(m.opts.Context, fmt.Sprintf("ack failed: %v", err), err)
m.opts.Logger.Error(m.opts.Context, "broker ack error", err)
}
}
// single processing
@ -218,11 +217,11 @@ func (m *memoryBroker) publish(ctx context.Context, msgs []*broker.Message, opts
if eh != nil {
_ = eh(p)
} else if m.opts.Logger.V(logger.ErrorLevel) {
m.opts.Logger.Error(m.opts.Context, err.Error())
m.opts.Logger.Error(m.opts.Context, "broker handler error", err)
}
} else if sub.opts.AutoAck {
if err = p.Ack(); err != nil {
m.opts.Logger.Error(m.opts.Context, fmt.Sprintf("ack failed: %v", err), err)
m.opts.Logger.Error(m.opts.Context, "broker ack error", err)
}
}
}

View File

@ -139,7 +139,7 @@ var (
return nil
}
if err := fn(ctx, c); err != nil {
c.Options().Logger.Error(ctx, fmt.Sprintf("%s BeforeLoad err: %v", c.String(), err), err)
c.Options().Logger.Error(ctx, fmt.Sprintf("%s BeforeLoad error", c.String()), err)
if !c.Options().AllowFail {
return err
}
@ -154,7 +154,7 @@ var (
return nil
}
if err := fn(ctx, c); err != nil {
c.Options().Logger.Error(ctx, fmt.Sprintf("%s AfterLoad err: %v", c.String(), err), err)
c.Options().Logger.Error(ctx, fmt.Sprintf("%s AfterLoad error", c.String()), err)
if !c.Options().AllowFail {
return err
}
@ -169,7 +169,7 @@ var (
return nil
}
if err := fn(ctx, c); err != nil {
c.Options().Logger.Error(ctx, fmt.Sprintf("%s BeforeSave err: %v", c.String(), err), err)
c.Options().Logger.Error(ctx, fmt.Sprintf("%s BeforeSave error", c.String()), err)
if !c.Options().AllowFail {
return err
}
@ -184,7 +184,7 @@ var (
return nil
}
if err := fn(ctx, c); err != nil {
c.Options().Logger.Error(ctx, fmt.Sprintf("%s AfterSave err: %v", c.String(), err), err)
c.Options().Logger.Error(ctx, fmt.Sprintf("%s AfterSave error", c.String()), err)
if !c.Options().AllowFail {
return err
}
@ -199,7 +199,7 @@ var (
return nil
}
if err := fn(ctx, c); err != nil {
c.Options().Logger.Error(ctx, fmt.Sprintf("%s BeforeInit err: %v", c.String(), err), err)
c.Options().Logger.Error(ctx, fmt.Sprintf("%s BeforeInit error", c.String()), err)
if !c.Options().AllowFail {
return err
}
@ -214,7 +214,7 @@ var (
return nil
}
if err := fn(ctx, c); err != nil {
c.Options().Logger.Error(ctx, fmt.Sprintf("%s AfterInit err: %v", c.String(), err), err)
c.Options().Logger.Error(ctx, fmt.Sprintf("%s AfterInit error", c.String(), err), err)
if !c.Options().AllowFail {
return err
}

View File

@ -188,7 +188,7 @@ func (w *microWorkflow) Execute(ctx context.Context, req *Message, opts ...Execu
steps, err := w.getSteps(options.Start, options.Reverse)
if err != nil {
if werr := workflowStore.Write(w.opts.Context, "status", &codec.Frame{Data: []byte(StatusPending.String())}); werr != nil {
w.opts.Logger.Error(w.opts.Context, fmt.Sprintf("store error: %v", werr), werr)
w.opts.Logger.Error(w.opts.Context, "store write error", werr)
}
return "", err
}
@ -212,7 +212,7 @@ func (w *microWorkflow) Execute(ctx context.Context, req *Message, opts ...Execu
done := make(chan struct{})
if werr := workflowStore.Write(w.opts.Context, "status", &codec.Frame{Data: []byte(StatusRunning.String())}); werr != nil {
w.opts.Logger.Error(w.opts.Context, fmt.Sprintf("store error: %v", werr), werr)
w.opts.Logger.Error(w.opts.Context, "store write error", werr)
return eid, werr
}
for idx := range steps {
@ -257,21 +257,21 @@ func (w *microWorkflow) Execute(ctx context.Context, req *Message, opts ...Execu
if serr != nil {
step.SetStatus(StatusFailure)
if werr := stepStore.Write(ctx, step.ID()+w.opts.Store.Options().Separator+"rsp", serr); werr != nil && w.opts.Logger.V(logger.ErrorLevel) {
w.opts.Logger.Error(ctx, fmt.Sprintf("store write error: %v", werr), werr)
w.opts.Logger.Error(ctx, "store write error", werr)
}
if werr := stepStore.Write(ctx, step.ID()+w.opts.Store.Options().Separator+"status", &codec.Frame{Data: []byte(StatusFailure.String())}); werr != nil && w.opts.Logger.V(logger.ErrorLevel) {
w.opts.Logger.Error(ctx, fmt.Sprintf("store write error: %v", werr), werr)
w.opts.Logger.Error(ctx, "store write error", werr)
}
cherr <- serr
return
}
if werr := stepStore.Write(ctx, step.ID()+w.opts.Store.Options().Separator+"rsp", rsp); werr != nil {
w.opts.Logger.Error(ctx, fmt.Sprintf("store write error: %v", werr), werr)
w.opts.Logger.Error(ctx, "store write error", werr)
cherr <- werr
return
}
if werr := stepStore.Write(ctx, step.ID()+w.opts.Store.Options().Separator+"status", &codec.Frame{Data: []byte(StatusSuccess.String())}); werr != nil {
w.opts.Logger.Error(ctx, fmt.Sprintf("store write error: %v", werr), werr)
w.opts.Logger.Error(ctx, "store write error", werr)
cherr <- werr
return
}
@ -290,16 +290,16 @@ func (w *microWorkflow) Execute(ctx context.Context, req *Message, opts ...Execu
if serr != nil {
cstep.SetStatus(StatusFailure)
if werr := stepStore.Write(ctx, cstep.ID()+w.opts.Store.Options().Separator+"rsp", serr); werr != nil && w.opts.Logger.V(logger.ErrorLevel) {
w.opts.Logger.Error(ctx, fmt.Sprintf("store write error: %v", werr), werr)
w.opts.Logger.Error(ctx, "store write error", werr)
}
if werr := stepStore.Write(ctx, cstep.ID()+w.opts.Store.Options().Separator+"status", &codec.Frame{Data: []byte(StatusFailure.String())}); werr != nil && w.opts.Logger.V(logger.ErrorLevel) {
w.opts.Logger.Error(ctx, fmt.Sprintf("store write error: %v", werr), werr)
w.opts.Logger.Error(ctx, "store write error", werr)
}
cherr <- serr
return
}
if werr := stepStore.Write(ctx, cstep.ID()+w.opts.Store.Options().Separator+"rsp", rsp); werr != nil {
w.opts.Logger.Error(ctx, fmt.Sprintf("store write error: %v", werr), werr)
w.opts.Logger.Error(ctx, "store write error", werr)
cherr <- werr
return
}
@ -333,15 +333,15 @@ func (w *microWorkflow) Execute(ctx context.Context, req *Message, opts ...Execu
switch {
case nctx.Err() != nil:
if werr := workflowStore.Write(w.opts.Context, "status", &codec.Frame{Data: []byte(StatusAborted.String())}); werr != nil {
w.opts.Logger.Error(w.opts.Context, fmt.Sprintf("store error: %v", werr), werr)
w.opts.Logger.Error(w.opts.Context, "store write error", werr)
}
case err == nil:
if werr := workflowStore.Write(w.opts.Context, "status", &codec.Frame{Data: []byte(StatusSuccess.String())}); werr != nil {
w.opts.Logger.Error(w.opts.Context, fmt.Sprintf("store error: %v", werr), werr)
w.opts.Logger.Error(w.opts.Context, "store write error", werr)
}
case err != nil:
if werr := workflowStore.Write(w.opts.Context, "status", &codec.Frame{Data: []byte(StatusFailure.String())}); werr != nil {
w.opts.Logger.Error(w.opts.Context, fmt.Sprintf("store error: %v", werr), werr)
w.opts.Logger.Error(w.opts.Context, "store write error", werr)
}
}

View File

@ -14,8 +14,6 @@ var (
DefaultLogger Logger = NewLogger()
// DefaultLevel used by logger
DefaultLevel = InfoLevel
// defaultCallerSkipCount used by logger
defaultCallerSkipCount = 3
)
// Logger is a generic logging interface

View File

@ -4,12 +4,17 @@ import (
"context"
)
const (
defaultCallerSkipCount = 2
)
type noopLogger struct {
opts Options
}
func NewLogger(opts ...Option) Logger {
options := NewOptions(opts...)
options.CallerSkipCount = defaultCallerSkipCount
return &noopLogger{opts: options}
}
@ -71,24 +76,3 @@ func (l *noopLogger) Warn(ctx context.Context, msg string, attrs ...interface{})
func (l *noopLogger) Fatal(ctx context.Context, msg string, attrs ...interface{}) {
}
func (l *noopLogger) Logf(ctx context.Context, lvl Level, msg string, attrs ...interface{}) {
}
func (l *noopLogger) Infof(ctx context.Context, msg string, attrs ...interface{}) {
}
func (l *noopLogger) Debugf(ctx context.Context, msg string, attrs ...interface{}) {
}
func (l *noopLogger) Errorf(ctx context.Context, msg string, attrs ...interface{}) {
}
func (l *noopLogger) Tracef(ctx context.Context, msg string, attrs ...interface{}) {
}
func (l *noopLogger) Warnf(ctx context.Context, msg string, attrs ...interface{}) {
}
func (l *noopLogger) Fatalf(ctx context.Context, msg string, attrs ...interface{}) {
}

View File

@ -54,7 +54,6 @@ type Options struct {
// NewOptions creates new options struct
func NewOptions(opts ...Option) Options {
options := Options{
CallerSkipCount: defaultCallerSkipCount,
Level: DefaultLevel,
Fields: make([]interface{}, 0, 6),
Out: os.Stderr,

View File

@ -16,8 +16,9 @@ import (
const (
badKey = "!BADKEY"
// defaultCallerSkipCount used by logger
defaultCallerSkipCount = 3
)
const emptyMSg = "!EMPTYMSG"
var reTrace = regexp.MustCompile(`.*/slog/logger\.go.*\n`)
@ -192,11 +193,12 @@ func (s *slogLogger) String() string {
}
func (s *slogLogger) printLog(ctx context.Context, lvl logger.Level, msg string, attrs ...interface{}) {
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", lvl.String()).Inc()
if !s.V(lvl) {
return
}
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", lvl.String()).Inc()
attrs = prepareAttributes(attrs)
for _, fn := range s.opts.ContextAttrFuncs {
@ -235,6 +237,7 @@ func NewLogger(opts ...logger.Option) logger.Logger {
s := &slogLogger{
opts: logger.NewOptions(opts...),
}
s.opts.CallerSkipCount = defaultCallerSkipCount
return s
}