Skip to content

Commit 0dbc31d

Browse files
committed
Requested changes to logger package
Signed-off-by: Yuri Sa <[email protected]>
1 parent c802880 commit 0dbc31d

13 files changed

+112
-73
lines changed

internal/capability/audit.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/opdev/opcap/internal/operator"
8+
"github.com/opdev/opcap/internal/logger"
89
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
910

1011
operatorv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
@@ -57,7 +58,7 @@ func newCapAudit(c operator.Client, subscription operator.SubscriptionData, audi
5758

5859
ocpVersion, err := c.GetOpenShiftVersion()
5960
if err != nil {
60-
logger.Debugw("Couldn't get OpenShift version for testing", "Err:", err)
61+
logger.Debug("Couldn't get OpenShift version for testing", "Err:", err)
6162
return capAudit{}, err
6263
}
6364

internal/capability/auditor.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"reflect"
55

66
"github.com/opdev/opcap/internal/operator"
7+
"github.com/opdev/opcap/internal/logger"
78
)
89

910
// capAuditor implements Auditor
@@ -37,7 +38,7 @@ func (capAuditor *CapAuditor) buildWorkQueueByCatalog() error {
3738
// Getting subscription data form the package manifests available in the selected catalog
3839
subscriptions, err := c.GetSubscriptionData(capAuditor.CatalogSource, capAuditor.CatalogSourceNamespace, capAuditor.Packages)
3940
if err != nil {
40-
logger.Errorf("Error while getting bundles from CatalogSource %s: %w", capAuditor.CatalogSource, err)
41+
logger.Error("Error while getting bundles from CatalogSource %s: %w", capAuditor.CatalogSource, err)
4142
return err
4243
}
4344

@@ -50,7 +51,7 @@ func (capAuditor *CapAuditor) buildWorkQueueByCatalog() error {
5051

5152
capAudit, err := newCapAudit(c, subscription, capAuditor.AuditPlan)
5253
if err != nil {
53-
logger.Debugf("Couldn't build capAudit for subscription %s", "Err:", err)
54+
logger.Debug("Couldn't build capAudit for subscription %s", "Err:", err)
5455
return err
5556
}
5657

@@ -65,7 +66,7 @@ func (capAuditor *CapAuditor) buildWorkQueueByCatalog() error {
6566
func (capAuditor *CapAuditor) RunAudits() error {
6667
err := capAuditor.buildWorkQueueByCatalog()
6768
if err != nil {
68-
logger.Debugf("Unable to build workqueue err := %s", err.Error())
69+
logger.Debug("Unable to build workqueue err := %s", err.Error())
6970
return err
7071
}
7172

internal/capability/operand_cleanup.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
"github.com/opdev/opcap/internal/operator"
7+
"github.com/opdev/opcap/internal/logger"
78

89
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
910

@@ -14,7 +15,7 @@ import (
1415

1516
// OperandCleanup removes the operand from the OCP cluster in the ca.namespace
1617
func (ca *capAudit) OperandCleanUp() error {
17-
logger.Debugw("cleaningUp operand for operator", "package", ca.subscription.Package, "channel", ca.subscription.Channel, "installmode",
18+
logger.Debug("cleaningUp operand for operator", "package", ca.subscription.Package, "channel", ca.subscription.Channel, "installmode",
1819
ca.subscription.InstallModeType)
1920

2021
if len(ca.customResources) > 0 {
@@ -57,7 +58,7 @@ func (ca *capAudit) OperandCleanUp() error {
5758
// delete the resource using the dynamic client
5859
err = client.Resource(gvr).Namespace(ca.namespace).Delete(context.TODO(), name, v1.DeleteOptions{})
5960
if err != nil {
60-
logger.Debugf("failed operandCleanUp: %s package: %s error: %s\n", Resource, ca.subscription.Package, err.Error())
61+
logger.Debug("failed operandCleanUp: %s package: %s error: %s\n", Resource, ca.subscription.Package, err.Error())
6162
return err
6263
}
6364
}

internal/capability/operand_install.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"time"
88

99
"github.com/opdev/opcap/internal/operator"
10+
"github.com/opdev/opcap/internal/logger"
11+
1012

1113
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1214

@@ -51,7 +53,7 @@ func (ca *capAudit) getAlmExamples() error {
5153

5254
// OperandInstall installs the operand from the ALMExamples in the ca.namespace
5355
func (ca *capAudit) OperandInstall() error {
54-
logger.Debugw("installing operand for operator", "package", ca.subscription.Package, "channel", ca.subscription.Channel, "installmode", ca.subscription.InstallModeType)
56+
logger.Debug("installing operand for operator", "package", ca.subscription.Package, "channel", ca.subscription.Channel, "installmode", ca.subscription.InstallModeType)
5557

5658
ca.getAlmExamples()
5759

internal/capability/operator_cleanup.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,34 @@ import (
44
"context"
55

66
"github.com/opdev/opcap/internal/operator"
7+
"github.com/opdev/opcap/internal/logger"
78
)
89

910
func (ca *capAudit) OperatorCleanUp() error {
1011

1112
// delete subscription
1213
if err := ca.client.DeleteSubscription(context.Background(), ca.subscription.Name, ca.namespace); err != nil {
13-
logger.Debugf("Error while deleting Subscription: %w", err)
14+
logger.Debug("Error while deleting Subscription: %w", err)
1415
return err
1516
}
1617

1718
// delete operator group
1819
if err := ca.client.DeleteOperatorGroup(context.Background(), ca.operatorGroupData.Name, ca.namespace); err != nil {
19-
logger.Debugf("Error while deleting OperatorGroup: %w", err)
20+
logger.Debug("Error while deleting OperatorGroup: %w", err)
2021
return err
2122
}
2223

2324
// delete target namespaces
2425
for _, ns := range ca.operatorGroupData.TargetNamespaces {
2526
if err := operator.DeleteNamespace(context.Background(), ns); err != nil {
26-
logger.Debugf("Error deleting target namespace %s", ns)
27+
logger.Debug("Error deleting target namespace %s", ns)
2728
return err
2829
}
2930
}
3031

3132
// delete operator's own namespace
3233
if err := operator.DeleteNamespace(context.Background(), ca.namespace); err != nil {
33-
logger.Debugf("Error deleting operator's own namespace %s", ca.namespace)
34+
logger.Debug("Error deleting operator's own namespace %s", ca.namespace)
3435
return err
3536
}
3637
return nil

internal/capability/operator_install.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ import (
44
"context"
55
"os"
66

7-
log "github.com/opdev/opcap/internal/logger"
7+
"github.com/opdev/opcap/internal/logger"
88
"github.com/opdev/opcap/internal/operator"
99
)
1010

11-
var logger = log.Sugar
12-
1311
func (ca *capAudit) OperatorInstall() error {
14-
logger.Debugw("installing package", "package", ca.subscription.Package, "channel", ca.subscription.Channel, "installmode", ca.subscription.InstallModeType)
12+
logger.Debug("installing package", "package", ca.subscription.Package, "channel", ca.subscription.Channel, "installmode", ca.subscription.InstallModeType)
1513

1614
// create operator's own namespace
1715
if _, err := operator.CreateNamespace(context.Background(), ca.namespace); err != nil {
@@ -30,7 +28,7 @@ func (ca *capAudit) OperatorInstall() error {
3028

3129
// create subscription for operator package/channel
3230
if _, err := ca.client.CreateSubscription(context.Background(), ca.subscription, ca.namespace); err != nil {
33-
logger.Debugf("Error creating subscriptions: %w", err)
31+
logger.Debug("Error creating subscriptions: %w", err)
3432
return err
3533
}
3634

internal/logger/logger.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package logger
2+
3+
import (
4+
"encoding/json"
5+
"go.uber.org/zap"
6+
"log"
7+
"os"
8+
)
9+
10+
var (
11+
envLog = os.Getenv("OPCAP_LOG_LEVEL")
12+
cfg zap.Config
13+
)
14+
15+
type LogJSON struct {
16+
Level string `json:"level"`
17+
Encoding string `json:"encoding"`
18+
OutputPaths []string `json:"outputPaths"`
19+
EncoderConfig *EncoderCfg `json:"encoderConfig"`
20+
}
21+
22+
type EncoderCfg struct {
23+
MessageKey string `json:"messageKey"`
24+
LevelKey string `json:"levelKey"`
25+
LevelEncoder string `json:"levelEncoder"`
26+
}
27+
28+
var logger *zap.Logger
29+
var sugarLogger *zap.SugaredLogger
30+
31+
func InitLogger() /* *zap.SugaredLogger */ {
32+
data := &LogJSON{
33+
Level: envLog,
34+
Encoding: "json",
35+
OutputPaths: []string{"stdout"},
36+
EncoderConfig: &EncoderCfg{
37+
MessageKey: "message",
38+
LevelKey: "level",
39+
LevelEncoder: "lowercase",
40+
},
41+
}
42+
dataJSON, err := json.Marshal(data)
43+
if err != nil {
44+
log.Fatalf("failed to build zap logger: %v", err)
45+
}
46+
47+
if err := json.Unmarshal([]byte(dataJSON), &cfg); err != nil {
48+
panic(err)
49+
}
50+
logger, err = cfg.Build()
51+
if err != nil {
52+
panic(err)
53+
}
54+
sugarLogger = logger.Sugar()
55+
}
56+
57+
func Info(message string, fields ...interface{}) {
58+
sugarLogger.Info(message, fields)
59+
}
60+
func Debug(message string, fields ...interface{}) {
61+
sugarLogger.Debug(message, fields)
62+
}
63+
func Error(message string, fields ...interface{}) {
64+
sugarLogger.Error(message, fields)
65+
}
66+
func Fatal(message string, fields ...interface{}) {
67+
sugarLogger.Fatal(message, fields)
68+
}
69+
func Panic(message string, fields ...interface{}) {
70+
sugarLogger.Panic(message, fields)
71+
}

internal/logger/zap.go

-37
This file was deleted.

internal/operator/client.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"k8s.io/apimachinery/pkg/runtime"
1111

12-
log "github.com/opdev/opcap/internal/logger"
12+
"github.com/opdev/opcap/internal/logger"
1313
configv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
1414
operatorv1 "github.com/operator-framework/api/pkg/operators/v1"
1515
operatorv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
@@ -24,9 +24,6 @@ import (
2424
"k8s.io/client-go/rest"
2525
"k8s.io/client-go/tools/clientcmd"
2626
)
27-
28-
var logger = log.Sugar
29-
3027
type Client interface {
3128
CreateOperatorGroup(ctx context.Context, data OperatorGroupData, namespace string) (*operatorv1.OperatorGroup, error)
3229
DeleteOperatorGroup(ctx context.Context, name string, namespace string) error
@@ -64,7 +61,7 @@ func NewOpCapClient() (Client, error) {
6461

6562
client, err := runtimeClient.New(kubeConfig(), runtimeClient.Options{Scheme: scheme})
6663
if err != nil {
67-
logger.Errorf("could not get subscription client")
64+
logger.Error("could not get subscription client")
6865
return nil, err
6966
}
7067

internal/operator/csv.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"fmt"
66
"time"
7-
7+
"github.com/opdev/opcap/internal/logger"
88
operatorv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
99
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1010
"k8s.io/apimachinery/pkg/util/wait"

internal/operator/namespace.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package operator
33
import (
44
"context"
55

6+
"github.com/opdev/opcap/internal/logger"
7+
68
corev1 "k8s.io/api/core/v1"
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
810
)
@@ -21,10 +23,10 @@ func CreateNamespace(ctx context.Context, name string) (*corev1.Namespace, error
2123
}
2224
_, err = operatorClient.CoreV1().Namespaces().Create(ctx, &nsSpec, metav1.CreateOptions{})
2325
if err != nil {
24-
logger.Errorf("error while creating Namespace %s: %w", name, err)
26+
logger.Error("error while creating Namespace %s: %w", name, err)
2527
return nil, err
2628
}
27-
logger.Debugf("Namespace Created: %s", name)
29+
logger.Debug("Namespace Created: %s", name)
2830
return &nsSpec, nil
2931
}
3032

@@ -34,12 +36,12 @@ func DeleteNamespace(ctx context.Context, name string) error {
3436
return err
3537
}
3638

37-
logger.Debugf("Delete namespace: %s", name)
39+
logger.Debug("Delete namespace: %s", name)
3840
err = operatorClient.CoreV1().Namespaces().Delete(ctx, name, metav1.DeleteOptions{})
3941
if err != nil {
40-
logger.Errorf("error while deleting Namespace %s: %w", name, err)
42+
logger.Error("error while deleting Namespace %s: %w", name, err)
4143
return err
4244
}
43-
logger.Debugf("Namespace Deleted: %s", name)
45+
logger.Debug("Namespace Deleted: %s", name)
4446
return nil
4547
}

0 commit comments

Comments
 (0)