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

Adding File Name and Line Number to Logs #72

Merged
merged 23 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/go-logr/logr"
cfgv1alpha2 "github.com/openservicemesh/osm/pkg/apis/config/v1alpha2"
policyv1alpha1 "github.com/openservicemesh/osm/pkg/apis/policy/v1alpha1"
ubzap "go.uber.org/zap"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -43,8 +44,11 @@ func init() {
}

func getLogger(opts ...zap.Opts) logr.Logger {
// use raw opts to add caller info to logs
rawOpts := zap.RawZapOpts(ubzap.AddCaller())

// zap is the default recommended logger for controller-runtime when wanting json structured output
return zap.New(opts...)
return zap.New(append(opts, rawOpts)...)
}

func registerSchemes(s *runtime.Scheme) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"encoding/json"
"errors"
"strings"
"testing"

"github.com/Azure/aks-app-routing-operator/pkg/config"
Expand Down Expand Up @@ -37,6 +38,7 @@ func TestLogger(t *testing.T) {
}

assert.True(t, json.Valid(line), "line is not valid json", string(line))
assert.True(t, strings.Contains(string(line), "\"caller\":\"controller/controller_test.go"))
checked++
}

Expand Down