Skip to content

Commit 4e12060

Browse files
authored
Merge pull request agones-dev#103 from googleprivate/feature/logging-origin
Add an `source` to all log statements
2 parents 4409235 + d99b932 commit 4e12060

9 files changed

Lines changed: 146 additions & 125 deletions

File tree

cmd/controller/main.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
package main
1717

1818
import (
19-
"strings"
20-
"time"
2119
"os"
2220
"path/filepath"
21+
"strings"
22+
"time"
2323

2424
"agones.dev/agones/pkg"
2525
"agones.dev/agones/pkg/client/clientset/versioned"
@@ -28,7 +28,6 @@ import (
2828
"agones.dev/agones/pkg/util/runtime"
2929
"agones.dev/agones/pkg/util/signals"
3030
"agones.dev/agones/pkg/util/webhooks"
31-
"github.com/sirupsen/logrus"
3231
"github.com/spf13/pflag"
3332
"github.com/spf13/viper"
3433
extclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
@@ -46,15 +45,15 @@ const (
4645
keyFileFlag = "key-file"
4746
)
4847

49-
func init() {
50-
logrus.SetFormatter(&logrus.JSONFormatter{})
51-
}
48+
var (
49+
logger = runtime.NewLoggerWithSource("main")
50+
)
5251

5352
// main starts the operator for the gameserver CRD
5453
func main() {
5554
exec, err := os.Executable()
5655
if err != nil {
57-
logrus.WithError(err).Fatal("Could not get executable path")
56+
logger.WithError(err).Fatal("Could not get executable path")
5857
}
5958

6059
base := filepath.Dir(exec)
@@ -87,7 +86,7 @@ func main() {
8786
keyFile := viper.GetString(keyFileFlag)
8887
certFile := viper.GetString(certFileFlag)
8988

90-
logrus.WithField(sidecarFlag, sidecarImage).
89+
logger.WithField(sidecarFlag, sidecarImage).
9190
WithField("minPort", minPort).
9291
WithField("maxPort", maxPort).
9392
WithField(keyFileFlag, keyFile).
@@ -96,29 +95,29 @@ func main() {
9695
WithField("Version", pkg.Version).Info("starting gameServer operator...")
9796

9897
if minPort <= 0 || maxPort <= 0 {
99-
logrus.Fatal("Min Port and Max Port values are required.")
98+
logger.Fatal("Min Port and Max Port values are required.")
10099
} else if maxPort < minPort {
101-
logrus.Fatal("Max Port cannot be set less that the Min Port")
100+
logger.Fatal("Max Port cannot be set less that the Min Port")
102101
}
103102

104103
config, err := rest.InClusterConfig()
105104
if err != nil {
106-
logrus.WithError(err).Fatal("Could not create in cluster config")
105+
logger.WithError(err).Fatal("Could not create in cluster config")
107106
}
108107

109108
kubeClient, err := kubernetes.NewForConfig(config)
110109
if err != nil {
111-
logrus.WithError(err).Fatal("Could not create the kubernetes clientset")
110+
logger.WithError(err).Fatal("Could not create the kubernetes clientset")
112111
}
113112

114113
extClient, err := extclientset.NewForConfig(config)
115114
if err != nil {
116-
logrus.WithError(err).Fatal("Could not create the api extension clientset")
115+
logger.WithError(err).Fatal("Could not create the api extension clientset")
117116
}
118117

119118
agonesClient, err := versioned.NewForConfig(config)
120119
if err != nil {
121-
logrus.WithError(err).Fatal("Could not create the agones api clientset")
120+
logger.WithError(err).Fatal("Could not create the agones api clientset")
122121
}
123122

124123
agonesInformerFactory := externalversions.NewSharedInformerFactory(agonesClient, 30*time.Second)
@@ -134,14 +133,14 @@ func main() {
134133

135134
go func() {
136135
if err := wh.Run(stop); err != nil { // nolint: vetshadow
137-
logrus.WithError(err).Fatal("could not run webhook server")
136+
logger.WithError(err).Fatal("could not run webhook server")
138137
}
139138
}()
140139

141140
err = c.Run(2, stop)
142141
if err != nil {
143-
logrus.WithError(err).Fatal("Could not run gameserver controller")
142+
logger.WithError(err).Fatal("Could not run gameserver controller")
144143
}
145144

146-
logrus.Info("Shut down gameserver controller")
145+
logger.Info("Shut down gameserver controller")
147146
}

cmd/sdk-server/main.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"agones.dev/agones/pkg/gameservers"
2727
"agones.dev/agones/pkg/sdk"
2828
"agones.dev/agones/pkg/util/runtime"
29-
"github.com/sirupsen/logrus"
3029
"github.com/spf13/pflag"
3130
"github.com/spf13/viper"
3231
"golang.org/x/net/context"
@@ -51,9 +50,9 @@ const (
5150
healthFailureThresholdFlag = "health-failure-threshold"
5251
)
5352

54-
func init() {
55-
logrus.SetFormatter(&logrus.JSONFormatter{})
56-
}
53+
var (
54+
logger = runtime.NewLoggerWithSource("main")
55+
)
5756

5857
func main() {
5958
viper.SetDefault(localFlag, false)
@@ -92,15 +91,15 @@ func main() {
9291
healthInitialDelay := time.Duration(viper.GetInt64(healthInitialDelayFlag)) * time.Second
9392
healthFailureThreshold := viper.GetInt64(healthFailureThresholdFlag)
9493

95-
logrus.WithField(localFlag, isLocal).WithField("version", pkg.Version).
94+
logger.WithField(localFlag, isLocal).WithField("version", pkg.Version).
9695
WithField("port", port).WithField(addressFlag, address).
9796
WithField(healthDisabledFlag, healthDisabled).WithField(healthTimeoutFlag, healthTimeout).
9897
WithField(healthFailureThresholdFlag, healthFailureThreshold).
9998
WithField(healthInitialDelayFlag, healthInitialDelay).Info("Starting sdk sidecar")
10099

101100
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, port))
102101
if err != nil {
103-
logrus.WithField("port", port).WithField("address", address).Fatalf("Could not listen on port")
102+
logger.WithField("port", port).WithField("address", address).Fatalf("Could not listen on port")
104103
}
105104
grpcServer := grpc.NewServer()
106105

@@ -109,24 +108,24 @@ func main() {
109108
} else {
110109
config, err := rest.InClusterConfig()
111110
if err != nil {
112-
logrus.WithError(err).Fatal("Could not create in cluster config")
111+
logger.WithError(err).Fatal("Could not create in cluster config")
113112
}
114113

115114
kubeClient, err := kubernetes.NewForConfig(config)
116115
if err != nil {
117-
logrus.WithError(err).Fatal("Could not create the kubernetes clientset")
116+
logger.WithError(err).Fatal("Could not create the kubernetes clientset")
118117
}
119118

120119
agonesClient, err := versioned.NewForConfig(config)
121120
if err != nil {
122-
logrus.WithError(err).Fatalf("Could not create the agones api clientset")
121+
logger.WithError(err).Fatalf("Could not create the agones api clientset")
123122
}
124123

125124
var s *gameservers.SDKServer
126125
s, err = gameservers.NewSDKServer(viper.GetString(gameServerNameEnv), viper.GetString(podNamespaceEnv),
127126
healthDisabled, healthTimeout, healthFailureThreshold, healthInitialDelay, kubeClient, agonesClient)
128127
if err != nil {
129-
logrus.WithError(err).Fatalf("Could not start sidecar")
128+
logger.WithError(err).Fatalf("Could not start sidecar")
130129
}
131130
ctx, cancel := context.WithCancel(context.Background())
132131
defer cancel()
@@ -137,6 +136,6 @@ func main() {
137136

138137
err = grpcServer.Serve(lis)
139138
if err != nil {
140-
logrus.WithError(err).Error("Could not serve grpc server")
139+
logger.WithError(err).Error("Could not serve grpc server")
141140
}
142141
}

pkg/apis/stable/v1alpha1/types_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"testing"
1919

2020
"agones.dev/agones/pkg/apis/stable"
21-
"github.com/sirupsen/logrus"
2221
"github.com/stretchr/testify/assert"
2322
corev1 "k8s.io/api/core/v1"
2423
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -218,7 +217,6 @@ func TestGameServerPod(t *testing.T) {
218217
assert.Equal(t, fixture.Spec.HostPort, pod.Spec.Containers[0].Ports[0].HostPort)
219218
assert.Equal(t, fixture.Spec.ContainerPort, pod.Spec.Containers[0].Ports[0].ContainerPort)
220219
assert.Equal(t, corev1.Protocol("UDP"), pod.Spec.Containers[0].Ports[0].Protocol)
221-
logrus.SetFormatter(&logrus.JSONFormatter{})
222220
assert.True(t, metav1.IsControlledBy(pod, fixture))
223221

224222
sidecar := corev1.Container{Name: "sidecar", Image: "container/sidecar"}

0 commit comments

Comments
 (0)