Skip to content

Commit

Permalink
Pull klog bridge out to log util
Browse files Browse the repository at this point in the history
Signed-off-by: Clayton Walker <[email protected]>
  • Loading branch information
Sineaggi committed Apr 10, 2023
1 parent 2b431f2 commit 3d11f83
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
7 changes: 3 additions & 4 deletions cmd/kubectl-argo-rollouts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package main
import (
"os"

"github.com/bombsimon/logrusr/v2"
logutil "github.com/argoproj/argo-rollouts/utils/log"

"github.com/sirupsen/logrus"

"k8s.io/cli-runtime/pkg/genericclioptions"
Expand All @@ -18,9 +19,7 @@ import (

func main() {
klog.InitFlags(nil)
logrusLog := logrus.New()
logger := logrusr.New(logrusLog)
klog.SetLogger(logger)
logutil.SetKLogLogger(logrus.New())
streams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
o := options.NewArgoRolloutsOptions(streams)
root := cmd.NewCmdArgoRollouts(o)
Expand Down
3 changes: 3 additions & 0 deletions cmd/rollouts-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"time"

"github.com/sirupsen/logrus"

"github.com/argoproj/pkg/kubeclientmetrics"
smiclientset "github.com/servicemeshinterface/smi-sdk-go/pkg/gen/client/split/clientset/versioned"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -83,6 +85,7 @@ func newCommand() *cobra.Command {
if logFormat != "" {
log.SetFormatter(createFormatter(logFormat))
}
logutil.SetKLogLogger(logrus.New())
logutil.SetKLogLevel(klogLevel)
log.WithField("version", version.GetVersion()).Info("Argo Rollouts starting")

Expand Down
8 changes: 5 additions & 3 deletions utils/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ const (
NamespaceKey = "namespace"
)

// SetKLogLogger set the klog logger for the k8s go-client
func SetKLogLogger(logger *logrus.Logger) {
klog.SetLogger(logrusr.New(logger))
}

// SetKLogLevel set the klog level for the k8s go-client
func SetKLogLevel(klogLevel int) {
klog.InitFlags(nil)
logrusLog := logrus.New()
logger := logrusr.New(logrusLog)
klog.SetLogger(logger)
_ = flag.Set("logtostderr", "true")
_ = flag.Set("v", strconv.Itoa(klogLevel))
}
Expand Down
13 changes: 13 additions & 0 deletions utils/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"testing"

"k8s.io/klog/v2"

log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -274,3 +276,14 @@ func TestWithVersionFields(t *testing.T) {
assert.True(t, strings.Contains(logMessage, "generation=2"))
assert.True(t, strings.Contains(logMessage, "resourceVersion=123"))
}

func TestKLogLogger(t *testing.T) {
buf := bytes.NewBufferString("")
logger := log.New()
logger.SetOutput(buf)
SetKLogLogger(logger)
defer klog.ClearLogger()
klog.Info("Logging from klog")
logMessage := buf.String()
assert.Contains(t, logMessage, "Logging from klog")
}

0 comments on commit 3d11f83

Please sign in to comment.