logger: add Fatal finalizers
* closes #222 Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -52,6 +52,12 @@ type Options struct {
|
|||||||
AddStacktrace bool
|
AddStacktrace bool
|
||||||
// DedupKeys deduplicate keys in log output
|
// DedupKeys deduplicate keys in log output
|
||||||
DedupKeys bool
|
DedupKeys bool
|
||||||
|
// FatalFinalizers runs in order in [logger.Fatal] method
|
||||||
|
FatalFinalizers []func(context.Context)
|
||||||
|
}
|
||||||
|
|
||||||
|
var DefaultFatalFinalizer = func(ctx context.Context) {
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewOptions creates new options struct
|
// NewOptions creates new options struct
|
||||||
@@ -65,6 +71,7 @@ func NewOptions(opts ...Option) Options {
|
|||||||
AddSource: true,
|
AddSource: true,
|
||||||
TimeFunc: time.Now,
|
TimeFunc: time.Now,
|
||||||
Meter: meter.DefaultMeter,
|
Meter: meter.DefaultMeter,
|
||||||
|
FatalFinalizers: []func(context.Context){DefaultFatalFinalizer},
|
||||||
}
|
}
|
||||||
|
|
||||||
WithMicroKeys()(&options)
|
WithMicroKeys()(&options)
|
||||||
@@ -76,6 +83,13 @@ func NewOptions(opts ...Option) Options {
|
|||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithFatalFinalizers set logger.Fatal finalizers
|
||||||
|
func WithFatalFinalizers(fncs []func(context.Context)) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.FatalFinalizers = fncs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithContextAttrFuncs appends default funcs for the context attrs filler
|
// WithContextAttrFuncs appends default funcs for the context attrs filler
|
||||||
func WithContextAttrFuncs(fncs ...ContextAttrFunc) Option {
|
func WithContextAttrFuncs(fncs ...ContextAttrFunc) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
|
|||||||
@@ -4,14 +4,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
|
||||||
|
|
||||||
"go.unistack.org/micro/v4/logger"
|
"go.unistack.org/micro/v4/logger"
|
||||||
"go.unistack.org/micro/v4/semconv"
|
"go.unistack.org/micro/v4/semconv"
|
||||||
@@ -234,8 +232,9 @@ func (s *slogLogger) Fatal(ctx context.Context, msg string, attrs ...interface{}
|
|||||||
if closer, ok := s.opts.Out.(io.Closer); ok {
|
if closer, ok := s.opts.Out.(io.Closer); ok {
|
||||||
closer.Close()
|
closer.Close()
|
||||||
}
|
}
|
||||||
time.Sleep(1 * time.Second)
|
for _, fn := range s.opts.FatalFinalizers {
|
||||||
os.Exit(1)
|
fn(ctx)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *slogLogger) Warn(ctx context.Context, msg string, attrs ...interface{}) {
|
func (s *slogLogger) Warn(ctx context.Context, msg string, attrs ...interface{}) {
|
||||||
|
|||||||
Reference in New Issue
Block a user