update slog/logger #351
@ -2,7 +2,6 @@ package broker
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/broker"
|
"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 {
|
} else if sub.opts.AutoAck {
|
||||||
if err = ms.Ack(); err != nil {
|
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
|
// single processing
|
||||||
@ -218,11 +217,11 @@ func (m *memoryBroker) publish(ctx context.Context, msgs []*broker.Message, opts
|
|||||||
if eh != nil {
|
if eh != nil {
|
||||||
_ = eh(p)
|
_ = eh(p)
|
||||||
} else if m.opts.Logger.V(logger.ErrorLevel) {
|
} 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 {
|
} else if sub.opts.AutoAck {
|
||||||
if err = p.Ack(); err != nil {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,7 +139,7 @@ var (
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err := fn(ctx, c); err != 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 {
|
if !c.Options().AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -154,7 +154,7 @@ var (
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err := fn(ctx, c); err != 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 {
|
if !c.Options().AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -169,7 +169,7 @@ var (
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err := fn(ctx, c); err != 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 {
|
if !c.Options().AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -184,7 +184,7 @@ var (
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err := fn(ctx, c); err != 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 {
|
if !c.Options().AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -199,7 +199,7 @@ var (
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err := fn(ctx, c); err != 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 {
|
if !c.Options().AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -214,7 +214,7 @@ var (
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err := fn(ctx, c); err != 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 {
|
if !c.Options().AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -188,7 +188,7 @@ func (w *microWorkflow) Execute(ctx context.Context, req *Message, opts ...Execu
|
|||||||
steps, err := w.getSteps(options.Start, options.Reverse)
|
steps, err := w.getSteps(options.Start, options.Reverse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if werr := workflowStore.Write(w.opts.Context, "status", &codec.Frame{Data: []byte(StatusPending.String())}); werr != 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
|
return "", err
|
||||||
}
|
}
|
||||||
@ -212,7 +212,7 @@ func (w *microWorkflow) Execute(ctx context.Context, req *Message, opts ...Execu
|
|||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
|
|
||||||
if werr := workflowStore.Write(w.opts.Context, "status", &codec.Frame{Data: []byte(StatusRunning.String())}); werr != nil {
|
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
|
return eid, werr
|
||||||
}
|
}
|
||||||
for idx := range steps {
|
for idx := range steps {
|
||||||
@ -257,21 +257,21 @@ func (w *microWorkflow) Execute(ctx context.Context, req *Message, opts ...Execu
|
|||||||
if serr != nil {
|
if serr != nil {
|
||||||
step.SetStatus(StatusFailure)
|
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) {
|
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) {
|
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
|
cherr <- serr
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if werr := stepStore.Write(ctx, step.ID()+w.opts.Store.Options().Separator+"rsp", rsp); werr != nil {
|
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
|
cherr <- werr
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if werr := stepStore.Write(ctx, step.ID()+w.opts.Store.Options().Separator+"status", &codec.Frame{Data: []byte(StatusSuccess.String())}); werr != nil {
|
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
|
cherr <- werr
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -290,16 +290,16 @@ func (w *microWorkflow) Execute(ctx context.Context, req *Message, opts ...Execu
|
|||||||
if serr != nil {
|
if serr != nil {
|
||||||
cstep.SetStatus(StatusFailure)
|
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) {
|
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) {
|
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
|
cherr <- serr
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if werr := stepStore.Write(ctx, cstep.ID()+w.opts.Store.Options().Separator+"rsp", rsp); werr != nil {
|
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
|
cherr <- werr
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -333,15 +333,15 @@ func (w *microWorkflow) Execute(ctx context.Context, req *Message, opts ...Execu
|
|||||||
switch {
|
switch {
|
||||||
case nctx.Err() != nil:
|
case nctx.Err() != nil:
|
||||||
if werr := workflowStore.Write(w.opts.Context, "status", &codec.Frame{Data: []byte(StatusAborted.String())}); werr != 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:
|
case err == nil:
|
||||||
if werr := workflowStore.Write(w.opts.Context, "status", &codec.Frame{Data: []byte(StatusSuccess.String())}); werr != 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:
|
case err != nil:
|
||||||
if werr := workflowStore.Write(w.opts.Context, "status", &codec.Frame{Data: []byte(StatusFailure.String())}); werr != 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,8 +14,6 @@ var (
|
|||||||
DefaultLogger Logger = NewLogger()
|
DefaultLogger Logger = NewLogger()
|
||||||
// DefaultLevel used by logger
|
// DefaultLevel used by logger
|
||||||
DefaultLevel = InfoLevel
|
DefaultLevel = InfoLevel
|
||||||
// defaultCallerSkipCount used by logger
|
|
||||||
defaultCallerSkipCount = 3
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Logger is a generic logging interface
|
// Logger is a generic logging interface
|
||||||
|
@ -4,12 +4,17 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultCallerSkipCount = 2
|
||||||
|
)
|
||||||
|
|
||||||
type noopLogger struct {
|
type noopLogger struct {
|
||||||
opts Options
|
opts Options
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLogger(opts ...Option) Logger {
|
func NewLogger(opts ...Option) Logger {
|
||||||
options := NewOptions(opts...)
|
options := NewOptions(opts...)
|
||||||
|
options.CallerSkipCount = defaultCallerSkipCount
|
||||||
return &noopLogger{opts: options}
|
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) 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{}) {
|
|
||||||
}
|
|
||||||
|
@ -54,7 +54,6 @@ type Options struct {
|
|||||||
// NewOptions creates new options struct
|
// NewOptions creates new options struct
|
||||||
func NewOptions(opts ...Option) Options {
|
func NewOptions(opts ...Option) Options {
|
||||||
options := Options{
|
options := Options{
|
||||||
CallerSkipCount: defaultCallerSkipCount,
|
|
||||||
Level: DefaultLevel,
|
Level: DefaultLevel,
|
||||||
Fields: make([]interface{}, 0, 6),
|
Fields: make([]interface{}, 0, 6),
|
||||||
Out: os.Stderr,
|
Out: os.Stderr,
|
||||||
|
@ -16,8 +16,9 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
badKey = "!BADKEY"
|
badKey = "!BADKEY"
|
||||||
|
// defaultCallerSkipCount used by logger
|
||||||
|
defaultCallerSkipCount = 3
|
||||||
)
|
)
|
||||||
const emptyMSg = "!EMPTYMSG"
|
|
||||||
|
|
||||||
var reTrace = regexp.MustCompile(`.*/slog/logger\.go.*\n`)
|
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{}) {
|
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) {
|
if !s.V(lvl) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", lvl.String()).Inc()
|
||||||
|
|
||||||
attrs = prepareAttributes(attrs)
|
attrs = prepareAttributes(attrs)
|
||||||
|
|
||||||
for _, fn := range s.opts.ContextAttrFuncs {
|
for _, fn := range s.opts.ContextAttrFuncs {
|
||||||
@ -235,6 +237,7 @@ func NewLogger(opts ...logger.Option) logger.Logger {
|
|||||||
s := &slogLogger{
|
s := &slogLogger{
|
||||||
opts: logger.NewOptions(opts...),
|
opts: logger.NewOptions(opts...),
|
||||||
}
|
}
|
||||||
|
s.opts.CallerSkipCount = defaultCallerSkipCount
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user