Skip to content

Commit 1b2740c

Browse files
committed
address comments.
Signed-off-by: morvencao <lcao@redhat.com>
1 parent b5dcf56 commit 1b2740c

File tree

6 files changed

+37
-46
lines changed

6 files changed

+37
-46
lines changed

cmd/maestro/environments/framework.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"k8s.io/client-go/rest"
1818
"k8s.io/client-go/tools/clientcmd"
1919

20+
envtypes "github.com/openshift-online/maestro/cmd/maestro/environments/types"
2021
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic"
2122
)
2223

@@ -27,16 +28,13 @@ func init() {
2728
// Create the configuration
2829
environment.Config = config.NewApplicationConfig()
2930
environment.ApplicationConfig = ApplicationConfig{config.NewApplicationConfig()}
30-
environment.Name = GetEnvironmentStrFromEnv()
31+
environment.Name = envtypes.GetEnvironmentStrFromEnv()
3132

3233
environments = map[string]EnvironmentImpl{
33-
DevelopmentEnv: &devEnvImpl{environment},
34-
TestingEnv: &testingEnvImpl{environment},
35-
ProductionEnv: &productionEnvImpl{environment},
34+
envtypes.DevelopmentEnv: &devEnvImpl{environment},
35+
envtypes.TestingEnv: &testingEnvImpl{environment},
36+
envtypes.ProductionEnv: &productionEnvImpl{environment},
3637
}
37-
38-
// initialize the logger
39-
logger.InitLogger(environment.Name)
4038
})
4139
}
4240

@@ -59,14 +57,6 @@ type EnvironmentImpl interface {
5957
VisitClients(c *Clients) error
6058
}
6159

62-
func GetEnvironmentStrFromEnv() string {
63-
envStr, specified := os.LookupEnv(EnvironmentStringKey)
64-
if !specified || envStr == "" {
65-
envStr = EnvironmentDefault
66-
}
67-
return envStr
68-
}
69-
7060
func Environment() *Env {
7161
return environment
7262
}
@@ -269,7 +259,7 @@ func (e *Env) InitializeSentry() error {
269259
}
270260

271261
func (e *Env) Teardown() {
272-
if e.Name != TestingEnv {
262+
if e.Name != envtypes.TestingEnv {
273263
if err := e.Database.SessionFactory.Close(); err != nil {
274264
log.Fatalf("Unable to close db connection: %s", err.Error())
275265
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package types
2+
3+
import "os"
4+
5+
const (
6+
TestingEnv string = "testing"
7+
DevelopmentEnv string = "development"
8+
ProductionEnv string = "production"
9+
10+
EnvironmentStringKey string = "MAESTRO_ENV"
11+
EnvironmentDefault string = DevelopmentEnv
12+
)
13+
14+
func GetEnvironmentStrFromEnv() string {
15+
envStr, specified := os.LookupEnv(EnvironmentStringKey)
16+
if !specified || envStr == "" {
17+
envStr = EnvironmentDefault
18+
}
19+
return envStr
20+
}

cmd/maestro/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func main() {
4747
viper.SetDefault(varLogLevel, "info")
4848
if err := viper.ReadInConfig(); err != nil {
4949
if _, ok := err.(*os.PathError); ok {
50-
log.Infof("no config file '%s' not found", logConfigFile)
50+
log.Infof("no config file '%s'", logConfigFile)
5151
} else {
5252
log.Errorf("failed to read the config file '%s': %v", logConfigFile, err)
5353
}

pkg/dispatcher/hash_dispatcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (d *HashDispatcher) updateConsumerSet() error {
196196

197197
_ = d.consumerSet.Append(toAddConsumers...)
198198
d.consumerSet.RemoveAll(toRemoveConsumers...)
199-
log.Infof("Consumers set for current instance: %s", d.consumerSet.String())
199+
log.Debugf("Consumers set for current instance: %s", d.consumerSet.String())
200200

201201
return nil
202202
}

pkg/logger/zap.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package logger
22

33
import (
4-
"sync"
5-
4+
envtypes "github.com/openshift-online/maestro/cmd/maestro/environments/types"
65
"go.uber.org/zap"
76
"go.uber.org/zap/zapcore"
87
)
@@ -12,49 +11,29 @@ import (
1211
var (
1312
zapLogLevel zap.AtomicLevel
1413
zapLogger *zap.SugaredLogger
15-
once sync.Once
1614
)
1715

18-
// InitLogger initializes the logger with the given environment.
19-
// Must be called before using the logger.
20-
func InitLogger(env string) {
21-
once.Do(func() {
16+
// GetLogger returns the singleton logger instance, initializing it
17+
// with the given environment if necessary.
18+
func GetLogger() *zap.SugaredLogger {
19+
if zapLogger == nil {
2220
zapLogLevel = zap.NewAtomicLevel()
2321
zapConfig := zap.NewDevelopmentConfig()
2422
zapConfig.DisableStacktrace = true
2523
zapConfig.Encoding = "console"
26-
if env == "development" {
24+
if envtypes.GetEnvironmentStrFromEnv() == "development" {
2725
zapLogLevel.SetLevel(zapcore.DebugLevel)
2826
} else {
2927
zapLogLevel.SetLevel(zapcore.InfoLevel)
3028
}
31-
zapConfig.Level = zapLogLevel
32-
zlog, err := zapConfig.Build()
33-
if err != nil {
34-
panic(err)
35-
}
36-
37-
zapLogger = zlog.Sugar()
38-
})
39-
}
40-
41-
// GetLogger returns the singleton logger instance.
42-
func GetLogger() *zap.SugaredLogger {
43-
if zapLogger == nil {
44-
zapLogLevel = zap.NewAtomicLevel()
45-
zapConfig := zap.NewDevelopmentConfig()
46-
zapConfig.DisableStacktrace = true
47-
zapConfig.Encoding = "console"
4829
zapLogLevel.SetLevel(zapcore.InfoLevel)
4930
zapConfig.Level = zapLogLevel
5031
zlog, err := zapConfig.Build()
5132
if err != nil {
5233
panic(err)
5334
}
54-
5535
zapLogger = zlog.Sugar()
5636
}
57-
5837
return zapLogger
5938
}
6039

test/helper.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636
"github.com/segmentio/ksuid"
3737
"github.com/spf13/pflag"
3838

39+
envtypes "github.com/openshift-online/maestro/cmd/maestro/environments/types"
40+
3941
amv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
4042

4143
"github.com/openshift-online/maestro/cmd/maestro/environments"
@@ -102,7 +104,7 @@ func NewHelper(t *testing.T) *Helper {
102104

103105
env := helper.Env()
104106
// Manually set environment name, ignoring environment variables
105-
env.Name = environments.TestingEnv
107+
env.Name = envtypes.TestingEnv
106108
err = env.AddFlags(pflag.CommandLine)
107109
if err != nil {
108110
log.Fatalf("Unable to add environment flags: %s", err.Error())

0 commit comments

Comments
 (0)