Fix overriding previous logger in context

This commit is contained in:
Deluan 2022-12-14 11:50:16 -05:00
parent 6c4a0be6ff
commit 6489dd4478
3 changed files with 6 additions and 3 deletions

View File

@ -38,7 +38,7 @@ func (p *players) Register(ctx context.Context, id, client, userAgent, ip string
if err != nil || id == "" {
plr, err = p.ds.Player(ctx).FindMatch(userName, client, userAgent)
if err == nil {
log.Debug("Found matching player", "id", plr.ID, "client", client, "username", userName, "type", userAgent)
log.Debug(ctx, "Found matching player", "id", plr.ID, "client", client, "username", userName, "type", userAgent)
} else {
plr = &model.Player{
ID: uuid.NewString(),

View File

@ -127,7 +127,11 @@ func NewContext(ctx context.Context, keyValuePairs ...interface{}) context.Conte
ctx = context.Background()
}
logger := addFields(createNewLogger(), keyValuePairs)
logger, ok := ctx.Value(loggerCtxKey).(*logrus.Entry)
if !ok {
logger = createNewLogger()
}
logger = addFields(logger, keyValuePairs)
ctx = context.WithValue(ctx, loggerCtxKey, logger)
return ctx

View File

@ -100,7 +100,6 @@ func authenticate(ds model.DataStore) func(next http.Handler) http.Handler {
ctx = log.NewContext(r.Context(), "username", username)
ctx = request.WithUser(ctx, *usr)
ctx = request.WithUsername(ctx, usr.UserName)
r = r.WithContext(ctx)
next.ServeHTTP(w, r)