Compare commits
	
		
			6 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 9eaab95519 | |||
|  | 9219dc6b2a | ||
| 52607b38f1 | |||
|  | 886f046409 | ||
| 4d6d469d40 | |||
|  | 4a944274f4 | 
| @@ -1,5 +1,5 @@ | |||||||
| # Micro | # Micro | ||||||
|  |  | ||||||
| [](https://opensource.org/licenses/Apache-2.0) | [](https://opensource.org/licenses/Apache-2.0) | ||||||
| [](https://pkg.go.dev/go.unistack.org/micro/v4?tab=overview) | [](https://pkg.go.dev/go.unistack.org/micro/v4?tab=overview) | ||||||
| [](https://git.unistack.org/unistack-org/micro/actions?query=workflow%3Abuild+branch%3Av4+event%3Apush) | [](https://git.unistack.org/unistack-org/micro/actions?query=workflow%3Abuild+branch%3Av4+event%3Apush) | ||||||
|   | |||||||
| @@ -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" | ||||||
| @@ -231,11 +229,12 @@ func (s *slogLogger) Error(ctx context.Context, msg string, attrs ...interface{} | |||||||
|  |  | ||||||
| func (s *slogLogger) Fatal(ctx context.Context, msg string, attrs ...interface{}) { | func (s *slogLogger) Fatal(ctx context.Context, msg string, attrs ...interface{}) { | ||||||
| 	s.printLog(ctx, logger.FatalLevel, msg, attrs...) | 	s.printLog(ctx, logger.FatalLevel, msg, attrs...) | ||||||
|  | 	for _, fn := range s.opts.FatalFinalizers { | ||||||
|  | 		fn(ctx) | ||||||
|  | 	} | ||||||
| 	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) |  | ||||||
| 	os.Exit(1) |  | ||||||
| } | } | ||||||
|  |  | ||||||
| func (s *slogLogger) Warn(ctx context.Context, msg string, attrs ...interface{}) { | func (s *slogLogger) Warn(ctx context.Context, msg string, attrs ...interface{}) { | ||||||
|   | |||||||
| @@ -469,3 +469,25 @@ func Test_WithContextAttrFunc(t *testing.T) { | |||||||
|  |  | ||||||
| 	// t.Logf("xxx %s", buf.Bytes()) | 	// t.Logf("xxx %s", buf.Bytes()) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TestFatalFinalizers(t *testing.T) { | ||||||
|  | 	ctx := context.TODO() | ||||||
|  | 	buf := bytes.NewBuffer(nil) | ||||||
|  | 	l := NewLogger( | ||||||
|  | 		logger.WithLevel(logger.TraceLevel), | ||||||
|  | 		logger.WithOutput(buf), | ||||||
|  | 	) | ||||||
|  | 	if err := l.Init( | ||||||
|  | 		logger.WithFatalFinalizers(func(ctx context.Context) { | ||||||
|  | 			l.Info(ctx, "fatal finalizer") | ||||||
|  | 		})); err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 	l.Fatal(ctx, "info_msg1") | ||||||
|  | 	if !bytes.Contains(buf.Bytes(), []byte("fatal finalizer")) { | ||||||
|  | 		t.Fatalf("logger dont have fatal message, buf %s", buf.Bytes()) | ||||||
|  | 	} | ||||||
|  | 	if !bytes.Contains(buf.Bytes(), []byte("info_msg1")) { | ||||||
|  | 		t.Fatalf("logger dont have info_msg1 message, buf %s", buf.Bytes()) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|   | |||||||
| @@ -82,7 +82,11 @@ type FloatCounter interface { | |||||||
|  |  | ||||||
| // Gauge is a float64 gauge | // Gauge is a float64 gauge | ||||||
| type Gauge interface { | type Gauge interface { | ||||||
|  | 	Add(float64) | ||||||
| 	Get() float64 | 	Get() float64 | ||||||
|  | 	Set(float64) | ||||||
|  | 	Dec() | ||||||
|  | 	Inc() | ||||||
| } | } | ||||||
|  |  | ||||||
| // Histogram is a histogram for non-negative values with automatically created buckets | // Histogram is a histogram for non-negative values with automatically created buckets | ||||||
|   | |||||||
| @@ -136,6 +136,18 @@ type noopGauge struct { | |||||||
| 	labels []string | 	labels []string | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (r *noopGauge) Add(float64) { | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r *noopGauge) Set(float64) { | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r *noopGauge) Inc() { | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r *noopGauge) Dec() { | ||||||
|  | } | ||||||
|  |  | ||||||
| func (r *noopGauge) Get() float64 { | func (r *noopGauge) Get() float64 { | ||||||
| 	return 0 | 	return 0 | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user