Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logger: bug with shared fields slice #202

Open
alkur-gh opened this issue Feb 27, 2024 · 0 comments
Open

logger: bug with shared fields slice #202

alkur-gh opened this issue Feb 27, 2024 · 0 comments

Comments

@alkur-gh
Copy link

Fields: make([]interface{}, 0, 6),

Because of the shared slice with capacity of 6, multiple cloned loggers write fields to the same place.

Snippet:

package main

import (
	"context"
	"fmt"

	zerolog "go.unistack.org/micro-logger-zerolog/v3"
	"go.unistack.org/micro/v3/logger"
)

func main() {
	logger.DefaultLogger = zerolog.NewLogger(
		zerolog.ReportCaller(),
		logger.WithLevel(logger.InfoLevel),
		logger.WithCallerSkipCount(5),
		//logger.WithFields(),
	)

	if err := logger.DefaultLogger.Init(); err != nil {
		panic(err)
	}

	ctx := context.Background()

	fields := func() any {
		return logger.DefaultLogger.Clone().Options().Fields
	}

	log := logger.DefaultLogger.Clone().Fields("my", "name", "is", "jojo")
	log2 := logger.DefaultLogger.Clone().Fields("who", "are")

	fmt.Printf("fields: %v (%p)\n", fields(), fields())
	fmt.Printf("log fields: %v (%p)\n", log.Options().Fields, log.Options().Fields)
	fmt.Printf("log2 fields: %v (%p)\n", log2.Options().Fields, log2.Options().Fields)

	logger.DefaultLogger.Clone().Fields("hello", "world").Trace(ctx)

	fmt.Printf("fields: %v (%p)\n", fields(), fields())
	fmt.Printf("log fields: %v (%p)\n", log.Options().Fields, log.Options().Fields)
	fmt.Printf("log2 fields: %v (%p)\n", log2.Options().Fields, log2.Options().Fields)
}
module demo

go 1.20

require (
	go.unistack.org/micro-logger-zerolog/v3 v3.8.4
	go.unistack.org/micro/v3 v3.10.39
)

require (
	github.com/mattn/go-colorable v0.1.12 // indirect
	github.com/mattn/go-isatty v0.0.14 // indirect
	github.com/pkg/errors v0.9.1 // indirect
	github.com/rs/zerolog v1.29.0 // indirect
	golang.org/x/sys v0.7.0 // indirect
)

Actual result:

fields: [] (0xc0000b4300)
log fields: [who are is jojo] (0xc0000b4300)
log2 fields: [who are] (0xc0000b4300)
fields: [] (0xc0000b4300)
log fields: [hello world is jojo] (0xc0000b4300)
log2 fields: [hello world] (0xc0000b4300)

Expected:

fields: [] (0x0)
log fields: [my name is jojo] (0xc0000d0040)
log2 fields: [who are] (0xc0000ce0c0)
fields: [] (0x0)
log fields: [my name is jojo] (0xc0000d0040)
log2 fields: [who are] (0xc0000ce0c0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant