Skip to content

Commit

Permalink
add klog
Browse files Browse the repository at this point in the history
  • Loading branch information
KnicKnic committed Oct 18, 2019
1 parent 72789e0 commit a31150f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
11 changes: 6 additions & 5 deletions examples/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"fmt"
"strings"

"github.com/KnicKnic/go-powershell/pkg/logger"
"github.com/KnicKnic/go-powershell/pkg/logger/kloghelper"
"github.com/KnicKnic/go-powershell/pkg/powershell"
"github.com/golang/glog"
"k8s.io/klog"
)

type callbackTest struct{}
Expand Down Expand Up @@ -50,11 +50,11 @@ func PrintAndExecuteCommand(runspace powershell.Runspace, command string, useLoc

// Example on how to use powershell wrappers
func Example() {
runspace := powershell.CreateRunspace(logger.Glog{VerboseLevel: 1, DebugLevel: 2}, callbackTest{})
runspace := powershell.CreateRunspace(kloghelper.Klog{VerboseLevel: 1, DebugLevel: 2}, callbackTest{})
defer runspace.Close()

if len(commandFlags) == 0 {
glog.Exit("Did not specify a \"-command\" to run")
klog.Exit("Did not specify a \"-command\" to run")
}
for i := 0; i < len(commandFlags); i++ {
command := strings.ReplaceAll(commandFlags[i], "\\", "\\\\")
Expand All @@ -78,9 +78,10 @@ var commandFlags arrayCommandFlags
var useLocalScope = flag.Bool("useLocalScope", false, "True if should execute scripts in the local scope")

func main() {
klog.InitFlags(nil)
flag.Var(&commandFlags, "command", "Command to run in powershell")
flag.Set("logtostderr", "true")
flag.Parse()
Example()
glog.Flush()
klog.Flush()
}
2 changes: 1 addition & 1 deletion pkg/logger/glog.go → pkg/logger/gloghelper/glog.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package logger
package gloghelper

import (
"github.com/golang/glog"
Expand Down
76 changes: 76 additions & 0 deletions pkg/logger/kloghelper/klog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package kloghelper

import (
"k8s.io/klog"
)

// Klog a type that can be used to log messages to klog
type Klog struct {
ErrorLevel klog.Level
WarningLevel klog.Level
InformationLevel klog.Level
VerboseLevel klog.Level
DebugLevel klog.Level
}

func (log Klog) Warning(arg string) {
if klog.V(log.WarningLevel) {
klog.Warning(arg)
}
}
func (log Klog) Information(arg string) {
if klog.V(log.InformationLevel) {
klog.Info(arg)
}
}
func (log Klog) Verbose(arg string) {
if klog.V(log.VerboseLevel) {
klog.Info(arg)
}
}
func (log Klog) Debug(arg string) {
if klog.V(log.DebugLevel) {
klog.Info(arg)
}
}
func (log Klog) Error(arg string) {
if klog.V(log.ErrorLevel) {
klog.Error(arg)
}
}
func (log Klog) Write(arg string) {
if klog.V(log.InformationLevel) {
klog.Info(arg)
}
}

func (log Klog) Warningln(arg string) {
if klog.V(log.WarningLevel) {
klog.Warningln(arg)
}
}
func (log Klog) Informationln(arg string) {
if klog.V(log.InformationLevel) {
klog.Infoln(arg)
}
}
func (log Klog) Verboseln(arg string) {
if klog.V(log.VerboseLevel) {
klog.Infoln(arg)
}
}
func (log Klog) Debugln(arg string) {
if klog.V(log.DebugLevel) {
klog.Infoln(arg)
}
}
func (log Klog) Errorln(arg string) {
if klog.V(log.ErrorLevel) {
klog.Errorln(arg)
}
}
func (log Klog) Writeln(arg string) {
if klog.V(log.InformationLevel) {
klog.Infoln(arg)
}
}

0 comments on commit a31150f

Please sign in to comment.