Browse Source

添加loglevel选项

yjp 9 months ago
parent
commit
59916caf5a
2 changed files with 30 additions and 2 deletions
  1. 17 2
      client/client.go
  2. 13 0
      client/options.go

+ 17 - 2
client/client.go

@@ -6,11 +6,14 @@ import (
 	"github.com/argoproj/argo-workflows/v3/pkg/apiclient/workflow"
 	"github.com/argoproj/argo-workflows/v3/pkg/apiclient/workflowtemplate"
 	"github.com/pkg/errors"
+	log "github.com/sirupsen/logrus"
 	"k8s.io/client-go/tools/clientcmd"
 	"os"
 	"sync"
 )
 
+var LogLevel string
+
 var watchWorkflowTokenMap sync.Map
 
 type Client struct {
@@ -20,8 +23,20 @@ type Client struct {
 	workflowService         workflow.WorkflowServiceClient
 }
 
-func NewClient(kubeConfigEnv string) (*Client, error) {
-	err := os.Setenv("KUBECONFIG", kubeConfigEnv)
+func NewClient(kubeConfigEnv string, opts ...Option) (*Client, error) {
+	options := new(Options)
+	for _, opt := range opts {
+		opt(options)
+	}
+
+	level, err := log.ParseLevel(options.logLevel)
+	if err != nil {
+		level = log.ErrorLevel
+	}
+
+	log.SetLevel(level)
+
+	err = os.Setenv("KUBECONFIG", kubeConfigEnv)
 	if err != nil {
 		return nil, errors.New(err.Error())
 	}

+ 13 - 0
client/options.go

@@ -0,0 +1,13 @@
+package client
+
+type Option func(options *Options)
+
+type Options struct {
+	logLevel string
+}
+
+func WithLogLevel(logLevel string) Option {
+	return func(options *Options) {
+		options.logLevel = logLevel
+	}
+}