Skip to content

Commit c2b1f07

Browse files
committed
fix: logger moved to root; returned zap
1 parent 8ec1f52 commit c2b1f07

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

cmd/gateway.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
openmfpcontext "github.com/openmfp/golang-commons/context"
1111
"github.com/openmfp/golang-commons/sentry"
1212
"github.com/spf13/cobra"
13+
ctrl "sigs.k8s.io/controller-runtime"
1314
restCfg "sigs.k8s.io/controller-runtime/pkg/client/config"
15+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
1416

1517
"github.com/openmfp/golang-commons/logger"
1618

@@ -44,6 +46,8 @@ var gatewayCmd = &cobra.Command{
4446
defer openmfpcontext.Recover(log)
4547
}
4648

49+
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
50+
4751
// Get Kubernetes restCfg
4852
restCfg, err := restCfg.GetConfig()
4953
if err != nil {

cmd/listener.go

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,21 @@ import (
1616
ctrl "sigs.k8s.io/controller-runtime"
1717
"sigs.k8s.io/controller-runtime/pkg/client"
1818
"sigs.k8s.io/controller-runtime/pkg/healthz"
19+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
1920
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
2021
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
2122
"sigs.k8s.io/controller-runtime/pkg/webhook"
2223

23-
"github.com/openmfp/golang-commons/logger"
2424
"github.com/openmfp/kubernetes-graphql-gateway/listener/discoveryclient"
2525
"github.com/openmfp/kubernetes-graphql-gateway/listener/kcp"
2626
)
2727

2828
var (
2929
scheme = runtime.NewScheme()
30-
setupLog *logger.Logger
3130
webhookServer webhook.Server
3231
metricsServerOptions metricsserver.Options
3332
)
3433

35-
func init() {
36-
var err error
37-
setupLog, err = setupLogger("info")
38-
if err != nil {
39-
panic("failed to initialize setup logger: " + err.Error())
40-
}
41-
}
42-
4334
var listenCmd = &cobra.Command{
4435
Use: "listener",
4536
Example: "KUBECONFIG=<path to kubeconfig file> go run . listener",
@@ -50,8 +41,13 @@ var listenCmd = &cobra.Command{
5041
utilruntime.Must(kcptenancy.AddToScheme(scheme))
5142
utilruntime.Must(apiextensionsv1.AddToScheme(scheme))
5243

44+
opts := zap.Options{
45+
Development: true,
46+
}
47+
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
48+
5349
disableHTTP2 := func(c *tls.Config) {
54-
setupLog.Info().Msg("disabling http/2")
50+
log.Info().Msg("disabling http/2")
5551
c.NextProtos = []string{"http/1.1"}
5652
}
5753

@@ -77,11 +73,6 @@ var listenCmd = &cobra.Command{
7773
Run: func(cmd *cobra.Command, args []string) {
7874
ctx := ctrl.SetupSignalHandler()
7975
restCfg := ctrl.GetConfigOrDie()
80-
log, err := setupLogger(defaultCfg.Log.Level)
81-
if err != nil {
82-
setupLog.Error().Err(err).Msg("unable to setup logger")
83-
os.Exit(1)
84-
}
8576

8677
mgrOpts := ctrl.Options{
8778
Scheme: scheme,
@@ -96,21 +87,21 @@ var listenCmd = &cobra.Command{
9687
Scheme: scheme,
9788
})
9889
if err != nil {
99-
setupLog.Error().Err(err).Msg("failed to create client from config")
90+
log.Error().Err(err).Msg("failed to create client from config")
10091
os.Exit(1)
10192
}
10293

10394
mf := kcp.NewManagerFactory(log, appCfg)
10495

10596
mgr, err := mf.NewManager(ctx, restCfg, mgrOpts, clt)
10697
if err != nil {
107-
setupLog.Error().Err(err).Msg("unable to start manager")
98+
log.Error().Err(err).Msg("unable to start manager")
10899
os.Exit(1)
109100
}
110101

111102
discoveryInterface, err := discovery.NewDiscoveryClientForConfig(restCfg)
112103
if err != nil {
113-
setupLog.Error().Err(err).Msg("failed to create discovery client")
104+
log.Error().Err(err).Msg("failed to create discovery client")
114105
os.Exit(1)
115106
}
116107

@@ -122,29 +113,28 @@ var listenCmd = &cobra.Command{
122113
}
123114

124115
reconciler, err := kcp.NewReconciler(appCfg, reconcilerOpts, restCfg, discoveryInterface, kcp.PreReconcile, discoveryclient.NewFactory, log)
125-
126116
if err != nil {
127-
setupLog.Error().Err(err).Msg("unable to instantiate reconciler")
117+
log.Error().Err(err).Msg("unable to instantiate reconciler")
128118
os.Exit(1)
129119
}
130120

131121
if err := reconciler.SetupWithManager(mgr); err != nil {
132-
setupLog.Error().Err(err).Msg("unable to create controller")
122+
log.Error().Err(err).Msg("unable to create controller")
133123
os.Exit(1)
134124
}
135125

136126
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
137-
setupLog.Error().Err(err).Msg("unable to set up health check")
127+
log.Error().Err(err).Msg("unable to set up health check")
138128
os.Exit(1)
139129
}
140130
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
141-
setupLog.Error().Err(err).Msg("unable to set up ready check")
131+
log.Error().Err(err).Msg("unable to set up ready check")
142132
os.Exit(1)
143133
}
144134

145-
setupLog.Info().Msg("starting manager")
135+
log.Info().Msg("starting manager")
146136
if err := mgr.Start(ctx); err != nil {
147-
setupLog.Error().Err(err).Msg("problem running manager")
137+
log.Error().Err(err).Msg("problem running manager")
148138
os.Exit(1)
149139
}
150140
},

cmd/root.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import (
55
"github.com/spf13/viper"
66

77
openmfpconfig "github.com/openmfp/golang-commons/config"
8+
"github.com/openmfp/golang-commons/logger"
89
"github.com/openmfp/kubernetes-graphql-gateway/common/config"
910
)
1011

1112
var (
1213
appCfg config.Config
1314
defaultCfg *openmfpconfig.CommonServiceConfig
1415
v *viper.Viper
16+
log *logger.Logger
1517
)
1618

1719
var rootCmd = &cobra.Command{
@@ -28,6 +30,12 @@ func init() {
2830
panic(err)
2931
}
3032

33+
// Initialize logger after config is loaded
34+
log, err = setupLogger(defaultCfg.Log.Level)
35+
if err != nil {
36+
panic("failed to initialize logger: " + err.Error())
37+
}
38+
3139
cobra.OnInitialize(initConfig)
3240

3341
err = openmfpconfig.BindConfigToFlags(v, gatewayCmd, &appCfg)

0 commit comments

Comments
 (0)