Skip to content

Commit

Permalink
feat: util/log support to set writer and add log-to-stderr flag for k…
Browse files Browse the repository at this point in the history
…usion compile
  • Loading branch information
howieyuen committed Jun 28, 2022
1 parent fd58099 commit ec64064
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 2 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1351,8 +1351,6 @@ kusionstack.io/kcl-plugin v0.4.1-alpha2 h1:m43JJhpJSjl/Q3FqghJf5dRIwdXsrJeKxrHhg
kusionstack.io/kcl-plugin v0.4.1-alpha2/go.mod h1:VgB7qXVbDGWFOh/qb/yXf75+UrliP5EPXOQUDqBCdAQ=
kusionstack.io/kclvm-go v0.4.2-alpha.9 h1:c4mDJFlKuP3CbGTtEkFEQidxcsJpGHNCDBtv7eexlH8=
kusionstack.io/kclvm-go v0.4.2-alpha.9/go.mod h1:LKGVud6Ch0dLLACwPqZQyU4E+NkLAXzveJFQWVb5Pbk=
kusionstack.io/kclvm-go v0.4.2-alpha4 h1:HL8dElxruBHcPbPl3MsfoojFhfeHDH/GHeUryuekrho=
kusionstack.io/kclvm-go v0.4.2-alpha4/go.mod h1:LKGVud6Ch0dLLACwPqZQyU4E+NkLAXzveJFQWVb5Pbk=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
2 changes: 2 additions & 0 deletions pkg/kusionctl/cmd/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func NewCmdCompile() *cobra.Command {
i18n.T("Specify the override option"))
cmd.Flags().StringSliceVarP(&o.Overrides, "overrides", "O", []string{},
i18n.T("Specify the configuration override path and value"))
cmd.Flags().BoolVarP(&o.LogToStderr, "log-to-stderr", "s", false,
i18n.T("log to standard error instead of files"))

return cmd
}
5 changes: 5 additions & 0 deletions pkg/kusionctl/cmd/compile/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"

"kusionstack.io/kusion/pkg/compile"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/projectstack"
)

Expand All @@ -20,6 +21,7 @@ type CompileOptions struct {
DisableNone bool
OverrideAST bool
Overrides []string
LogToStderr bool
}

func NewCompileOptions() *CompileOptions {
Expand All @@ -32,6 +34,9 @@ func NewCompileOptions() *CompileOptions {
func (o *CompileOptions) Complete(args []string) {
o.Filenames = args
o.PreSet(projectstack.IsStack)
if o.LogToStderr {
log.SetOutput(os.Stderr)
}
}

func (o *CompileOptions) Validate() error {
Expand Down
5 changes: 5 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"fmt"
"io"
"os"
"strings"
)
Expand Down Expand Up @@ -98,3 +99,7 @@ func GetLogger() Logger {
func With(args ...interface{}) Logger {
return log.With(args...)
}

func SetOutput(out io.Writer) {
log.SetOutput(out)
}
2 changes: 2 additions & 0 deletions pkg/log/log_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package log

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_zapLogger(t *testing.T) {
SetOutput(os.Stderr)
t.Run("log debug", func(t *testing.T) {
Debug("foo")
})
Expand Down
3 changes: 3 additions & 0 deletions pkg/log/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package log

import "io"

type Level uint8

const (
Expand All @@ -26,4 +28,5 @@ type Logger interface {
SetLevel(level Level)
GetLogDir() LogDir
With(args ...interface{}) Logger
SetOutput(out io.Writer)
}
55 changes: 55 additions & 0 deletions pkg/log/zap.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package log

import (
"fmt"
"io"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -28,6 +30,7 @@ type zapLogger struct {
debugLevel zap.AtomicLevel
defaultLevel zap.AtomicLevel
errorLevel zap.AtomicLevel
out io.Writer
}

func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
Expand Down Expand Up @@ -72,50 +75,98 @@ func newZapLogger() (Logger, error) {
}

func (l *zapLogger) Debug(args ...interface{}) {
if l.out != nil {
l.out.Write([]byte(fmt.Sprint(args...)))
return
}
l.sugaredLogger.Debug(args...)
}

func (l *zapLogger) Debugf(format string, args ...interface{}) {
if l.out != nil {
l.out.Write([]byte(fmt.Sprintf(format, args...)))
return
}
l.sugaredLogger.Debugf(format, args...)
}

func (l *zapLogger) Infof(format string, args ...interface{}) {
if l.out != nil {
l.out.Write([]byte(fmt.Sprintf(format, args...)))
return
}
l.sugaredLogger.Infof(format, args...)
}

func (l *zapLogger) Info(args ...interface{}) {
if l.out != nil {
l.out.Write([]byte(fmt.Sprint(args...)))
return
}
l.sugaredLogger.Info(args...)
}

func (l *zapLogger) Warnf(format string, args ...interface{}) {
if l.out != nil {
l.out.Write([]byte(fmt.Sprintf(format, args...)))
return
}
l.sugaredLogger.Warnf(format, args...)
}

func (l *zapLogger) Warn(args ...interface{}) {
if l.out != nil {
l.out.Write([]byte(fmt.Sprint(args...)))
return
}
l.sugaredLogger.Warn(args...)
}

func (l *zapLogger) Errorf(format string, args ...interface{}) {
if l.out != nil {
l.out.Write([]byte(fmt.Sprintf(format, args...)))
return
}
l.sugaredLogger.Errorf(format, args...)
}

func (l *zapLogger) Error(args ...interface{}) {
if l.out != nil {
l.out.Write([]byte(fmt.Sprint(args...)))
return
}
l.sugaredLogger.Error(args...)
}

func (l *zapLogger) Panicf(format string, args ...interface{}) {
if l.out != nil {
l.out.Write([]byte(fmt.Sprintf(format, args...)))
return
}
l.sugaredLogger.Panicf(format, args...)
}

func (l *zapLogger) Panic(args ...interface{}) {
if l.out != nil {
l.out.Write([]byte(fmt.Sprint(args...)))
return
}
l.sugaredLogger.Panic(args...)
}

func (l *zapLogger) Fatalf(format string, args ...interface{}) {
if l.out != nil {
l.out.Write([]byte(fmt.Sprintf(format, args...)))
return
}
l.sugaredLogger.Fatalf(format, args...)
}

func (l *zapLogger) Fatal(args ...interface{}) {
if l.out != nil {
l.out.Write([]byte(fmt.Sprint(args...)))
return
}
l.sugaredLogger.Fatal(args...)
}

Expand All @@ -140,6 +191,10 @@ func (l *zapLogger) With(args ...interface{}) Logger {
return &zapLogger{sugaredLogger: curLogger, defaultLevel: l.defaultLevel, debugLevel: l.debugLevel, errorLevel: l.errorLevel}
}

func (l *zapLogger) SetOutput(output io.Writer) {
l.out = output
}

func getZapLevel(level Level) zapcore.Level {
switch level {
case INFO:
Expand Down

0 comments on commit ec64064

Please sign in to comment.