diff --git a/go.mod b/go.mod index 858b56a0..af3947bf 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( k8s.io/apimachinery v0.0.0 k8s.io/client-go v0.0.0 k8s.io/code-generator v0.0.0-20190612205613-18da4a14b22b - k8s.io/klog/v2 v2.2.0 + github.com/practo/klog/v2 v2.2.1 ) replace ( diff --git a/go.sum b/go.sum index c1f25a12..075bae47 100644 --- a/go.sum +++ b/go.sum @@ -173,6 +173,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA= +github.com/practo/klog/v2 v2.2.1 h1:yV/SavG73KHSmCVwtDap1RGe+2jd86e1eugT+Mv2/FA= +github.com/practo/klog/v2 v2.2.1/go.mod h1:WMkpfPwTxLgSLookpI4UUv2zL7tMltKACK/FHB1zLV0= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM= github.com/prometheus/client_golang v0.9.3 h1:9iH4JKXLzFbOAdtqv/a+j8aewx2Y8lAjAydhbaScPF8= diff --git a/vendor/k8s.io/klog/v2/.gitignore b/vendor/github.com/practo/klog/v2/.gitignore similarity index 100% rename from vendor/k8s.io/klog/v2/.gitignore rename to vendor/github.com/practo/klog/v2/.gitignore diff --git a/vendor/k8s.io/klog/v2/CONTRIBUTING.md b/vendor/github.com/practo/klog/v2/CONTRIBUTING.md similarity index 100% rename from vendor/k8s.io/klog/v2/CONTRIBUTING.md rename to vendor/github.com/practo/klog/v2/CONTRIBUTING.md diff --git a/vendor/k8s.io/klog/v2/LICENSE b/vendor/github.com/practo/klog/v2/LICENSE similarity index 100% rename from vendor/k8s.io/klog/v2/LICENSE rename to vendor/github.com/practo/klog/v2/LICENSE diff --git a/vendor/k8s.io/klog/v2/OWNERS b/vendor/github.com/practo/klog/v2/OWNERS similarity index 100% rename from vendor/k8s.io/klog/v2/OWNERS rename to vendor/github.com/practo/klog/v2/OWNERS diff --git a/vendor/k8s.io/klog/v2/README.md b/vendor/github.com/practo/klog/v2/README.md similarity index 100% rename from vendor/k8s.io/klog/v2/README.md rename to vendor/github.com/practo/klog/v2/README.md diff --git a/vendor/k8s.io/klog/v2/RELEASE.md b/vendor/github.com/practo/klog/v2/RELEASE.md similarity index 100% rename from vendor/k8s.io/klog/v2/RELEASE.md rename to vendor/github.com/practo/klog/v2/RELEASE.md diff --git a/vendor/k8s.io/klog/v2/SECURITY_CONTACTS b/vendor/github.com/practo/klog/v2/SECURITY_CONTACTS similarity index 100% rename from vendor/k8s.io/klog/v2/SECURITY_CONTACTS rename to vendor/github.com/practo/klog/v2/SECURITY_CONTACTS diff --git a/vendor/k8s.io/klog/v2/code-of-conduct.md b/vendor/github.com/practo/klog/v2/code-of-conduct.md similarity index 100% rename from vendor/k8s.io/klog/v2/code-of-conduct.md rename to vendor/github.com/practo/klog/v2/code-of-conduct.md diff --git a/vendor/k8s.io/klog/v2/go.mod b/vendor/github.com/practo/klog/v2/go.mod similarity index 59% rename from vendor/k8s.io/klog/v2/go.mod rename to vendor/github.com/practo/klog/v2/go.mod index e396e31c..cfafd69e 100644 --- a/vendor/k8s.io/klog/v2/go.mod +++ b/vendor/github.com/practo/klog/v2/go.mod @@ -1,4 +1,4 @@ -module k8s.io/klog/v2 +module github.com/practo/klog/v2 go 1.13 diff --git a/vendor/k8s.io/klog/v2/go.sum b/vendor/github.com/practo/klog/v2/go.sum similarity index 100% rename from vendor/k8s.io/klog/v2/go.sum rename to vendor/github.com/practo/klog/v2/go.sum diff --git a/vendor/github.com/practo/klog/v2/hooks.go b/vendor/github.com/practo/klog/v2/hooks.go new file mode 100644 index 00000000..d0c921bc --- /dev/null +++ b/vendor/github.com/practo/klog/v2/hooks.go @@ -0,0 +1,99 @@ +// Go support for leveled logs, analogous to https://code.google.com/p/google-glog/ +// +// Copyright 2013 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package klog + +import ( + "fmt" +) + +var ( + InfoSeverityLevel = severityName[infoLog] + WarningSeverityLevel = severityName[warningLog] + ErrorSeverityLevel = severityName[errorLog] + FatalSeverityLevel = severityName[fatalLog] +) + +// Hook to be fired when logging to a severity +type Hook interface { + // SeverityLevel returns "INFO", "WARNING", "ERROR" or "FATAL" + // Hook will be fired in all the cases when severity is greater than + // or equal to the severity level + SeverityLevel() string + + // Fire implements the actual hook task that needs to be triggered + Fire(s string, args ...interface{}) error +} + +func GetSeverityNames() []string { + var severityNames []string + for _, s := range severityName { + severityNames = append(severityNames, s) + } + + return severityNames +} + +func IsSeverityLevelSupported(severityLevel string) error { + severityNames := GetSeverityNames() + for _, s := range severityNames { + if s == severityLevel { + return nil + } + } + + return fmt.Errorf( + "not supported severity level: %s, supported severity levels are: %v", + severityLevel, + severityNames, + ) +} + +type Hooks map[string][]Hook + +func (hooks Hooks) Add(hook Hook) error { + severityLevel := hook.SeverityLevel() + err := IsSeverityLevelSupported(severityLevel) + if err != nil { + return err + } + hooks[severityLevel] = append(hooks[severityLevel], hook) + + return nil +} + +func (hooks Hooks) Fire(s severity, args ...interface{}) error { + for severityLevel, severityHooks := range hooks { + level, ok := severityByName(severityLevel) + if !ok { + return fmt.Errorf( + "error getting severity name for: %s", severityLevel, + ) + } + + if s < level { + continue + } + + for _, hook := range severityHooks { + if err := hook.Fire(severityName[s], args); err != nil { + return err + } + } + } + + return nil +} diff --git a/vendor/k8s.io/klog/v2/klog.go b/vendor/github.com/practo/klog/v2/klog.go similarity index 98% rename from vendor/k8s.io/klog/v2/klog.go rename to vendor/github.com/practo/klog/v2/klog.go index ae2b8613..929eb9b9 100644 --- a/vendor/k8s.io/klog/v2/klog.go +++ b/vendor/github.com/practo/klog/v2/klog.go @@ -413,6 +413,7 @@ func init() { logging.skipHeaders = false logging.addDirHeader = false logging.skipLogHeaders = false + logging.hooks = Hooks{} go logging.flushDaemon() } @@ -443,6 +444,10 @@ func Flush() { logging.lockAndFlushAll() } +func AddHook(hook Hook) { + logging.addHook(hook) +} + // loggingT collects all the global state of the logging setup. type loggingT struct { // Boolean flags. Not handled atomically because the flag.Value interface @@ -505,6 +510,9 @@ type loggingT struct { // If set, all output will be redirected unconditionally to the provided logr.Logger logr logr.Logger + + // hooks to fire based on severity + hooks Hooks } // buffer holds a byte Buffer for reuse. The zero value is ready for use. @@ -697,6 +705,7 @@ func (l *loggingT) println(s severity, logr logr.Logger, args ...interface{}) { } fmt.Fprintln(buf, args...) l.output(s, logr, buf, file, line, false) + l.fireHooks(s) } func (l *loggingT) print(s severity, logr logr.Logger, args ...interface{}) { @@ -716,6 +725,7 @@ func (l *loggingT) printDepth(s severity, logr logr.Logger, depth int, args ...i buf.WriteByte('\n') } l.output(s, logr, buf, file, line, false) + l.fireHooks(s) } func (l *loggingT) printf(s severity, logr logr.Logger, format string, args ...interface{}) { @@ -731,6 +741,7 @@ func (l *loggingT) printf(s severity, logr logr.Logger, format string, args ...i buf.WriteByte('\n') } l.output(s, logr, buf, file, line, false) + l.fireHooks(s) } // printWithFileLine behaves like print but uses the provided file and line number. If @@ -749,6 +760,7 @@ func (l *loggingT) printWithFileLine(s severity, logr logr.Logger, file string, buf.WriteByte('\n') } l.output(s, logr, buf, file, line, alsoToStderr) + l.fireHooks(s) } // if loggr is specified, will call loggr.Error, otherwise output with logging module. @@ -1022,6 +1034,23 @@ func (l *loggingT) exit(err error) { os.Exit(2) } +// addHook adds a Hook to logging +func (l *loggingT) addHook(hook Hook) { + l.mu.Lock() + defer l.mu.Unlock() + err := l.hooks.Add(hook) + if err != nil { + l.exit(err) + } +} + +func (l *loggingT) fireHooks(s severity) { + err := l.hooks.Fire(s) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err) + } +} + // syncBuffer joins a bufio.Writer to its underlying file, providing access to the // file's Sync method and providing a wrapper for the Write method that provides log // file rotation. There are conflicting methods, so the file cannot be embedded. diff --git a/vendor/k8s.io/klog/v2/klog_file.go b/vendor/github.com/practo/klog/v2/klog_file.go similarity index 100% rename from vendor/k8s.io/klog/v2/klog_file.go rename to vendor/github.com/practo/klog/v2/klog_file.go diff --git a/vendor/modules.txt b/vendor/modules.txt index cd5144d5..b711167d 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -113,6 +113,9 @@ github.com/modern-go/concurrent github.com/modern-go/reflect2 # github.com/pelletier/go-toml v1.2.0 github.com/pelletier/go-toml +# github.com/practo/klog/v2 v2.2.1 +## explicit +github.com/practo/klog/v2 # github.com/prometheus/client_golang v0.9.3 ## explicit github.com/prometheus/client_golang/prometheus @@ -502,9 +505,6 @@ k8s.io/gengo/parser k8s.io/gengo/types # k8s.io/klog v0.3.1 k8s.io/klog -# k8s.io/klog/v2 v2.2.0 -## explicit -k8s.io/klog/v2 # k8s.io/kube-openapi v0.0.0-20190228160746-b3a7cee44a30 k8s.io/kube-openapi/pkg/util/proto # k8s.io/utils v0.0.0-20190221042446-c2654d5206da