gitpod/components/common-go/log/context_test.go
Milan Pavlik 13b3b013b6
[logging] Enable storing/extracing of logger from context (#16658)
* [logging] Enable storing/extracing of logger from context

* Fix

* Fix

* Fix

* Fix

* Fix

* Fix
2023-03-09 10:27:45 +01:00

38 lines
892 B
Go

// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.
package log
import (
"context"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
func TestExtract_GracefulWithoutEntryOnContext(t *testing.T) {
require.NotNil(t, Extract(context.Background()))
}
func TestExtract(t *testing.T) {
ctx := context.Background()
require.NotNil(t, Extract(context.Background()), "graceful without a context logger")
withContextLogger := ToContext(ctx, Log)
require.Equal(t, Log, Extract(withContextLogger))
}
func TestAddFields(t *testing.T) {
ctx := ToContext(context.Background(), Log)
fields := logrus.Fields{
"some-field": "value",
}
AddFields(ctx, fields)
entry := Extract(ctx)
require.Equal(t, entry.Data, fields)
}