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
|
||||
// DedupKeys deduplicate keys in log output
|
||||
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
|
||||
@@ -65,6 +71,7 @@ func NewOptions(opts ...Option) Options {
|
||||
AddSource: true,
|
||||
TimeFunc: time.Now,
|
||||
Meter: meter.DefaultMeter,
|
||||
FatalFinalizers: []func(context.Context){DefaultFatalFinalizer},
|
||||
}
|
||||
|
||||
WithMicroKeys()(&options)
|
||||
@@ -76,6 +83,13 @@ func NewOptions(opts ...Option) 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
|
||||
func WithContextAttrFuncs(fncs ...ContextAttrFunc) Option {
|
||||
return func(o *Options) {
|
||||
|
||||
@@ -4,14 +4,12 @@ import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"go.unistack.org/micro/v4/logger"
|
||||
"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 {
|
||||
closer.Close()
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
os.Exit(1)
|
||||
for _, fn := range s.opts.FatalFinalizers {
|
||||
fn(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *slogLogger) Warn(ctx context.Context, msg string, attrs ...interface{}) {
|
||||
|
||||
Reference in New Issue
Block a user