diff --git a/Gopkg.lock b/Gopkg.lock index 1b716b82b6..bb9c5ceaff 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -182,6 +182,14 @@ revision = "9316a62528ac99aaecb4e47eadd6dc8aa6533d58" version = "v0.3.5" +[[projects]] + branch = "master" + digest = "1:67af90f6f6551eed6e14b16833c68ff9e6a3d1e11ea296df70693b24744c24d1" + name = "github.com/joonix/log" + packages = ["."] + pruneopts = "" + revision = "d2d3f2f4a80658c67a0bc44021dcac30f3017c06" + [[projects]] digest = "1:b79fc583e4dc7055ed86742e22164ac41bf8c0940722dbcb600f1a3ace1a8cb5" name = "github.com/json-iterator/go" @@ -838,6 +846,7 @@ "github.com/grpc-ecosystem/grpc-gateway/runtime", "github.com/grpc-ecosystem/grpc-gateway/utilities", "github.com/heptiolabs/healthcheck", + "github.com/joonix/log", "github.com/mattbaird/jsonpatch", "github.com/pkg/errors", "github.com/sirupsen/logrus", diff --git a/Gopkg.toml b/Gopkg.toml index 01ff20b60d..7af1a8a1d5 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -78,4 +78,8 @@ [[override]] name = "github.com/grpc-ecosystem/grpc-gateway" - version = "1.5.1" \ No newline at end of file + version = "1.5.1" + +[[constraint]] + branch = "master" + name = "github.com/joonix/log" diff --git a/pkg/util/runtime/runtime.go b/pkg/util/runtime/runtime.go index 8e44ecc04f..1b0c1ef819 100644 --- a/pkg/util/runtime/runtime.go +++ b/pkg/util/runtime/runtime.go @@ -19,6 +19,7 @@ package runtime import ( "fmt" + joonix "github.com/joonix/log" "github.com/pkg/errors" "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/util/runtime" @@ -33,12 +34,7 @@ type stackTracer interface { // replace the standard glog error logger, with a logrus one func init() { - - logrus.SetFormatter(&logrus.JSONFormatter{ - FieldMap: logrus.FieldMap{ - logrus.FieldKeyLevel: "severity", - }, - }) + logrus.SetFormatter(&joonix.FluentdFormatter{}) runtime.ErrorHandlers[0] = func(err error) { if stackTrace, ok := err.(stackTracer); ok { diff --git a/vendor/github.com/joonix/log/LICENSE b/vendor/github.com/joonix/log/LICENSE new file mode 100644 index 0000000000..87a53c5328 --- /dev/null +++ b/vendor/github.com/joonix/log/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 JoonixSE + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/joonix/log/README.md b/vendor/github.com/joonix/log/README.md new file mode 100644 index 0000000000..ad44e18768 --- /dev/null +++ b/vendor/github.com/joonix/log/README.md @@ -0,0 +1,36 @@ +# Log + +## FluentdFormatter + +Useful addition to logrus, allowing it to format log entries that can be parsed by Kubernetes +and Google Container Engine. + +Example: + +```go +package main + +import ( + "os" + "fmt" + "flag" + + log "github.com/sirupsen/logrus" + joonix "github.com/joonix/log" +) + +func main() { + lvl := flag.String("level", log.DebugLevel.String(), "log level") + flag.Parse() + + level, err := log.ParseLevel(*lvl) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + log.SetLevel(level) + log.SetFormatter(&joonix.FluentdFormatter{}) + + log.Debug("hello world!") +} +``` diff --git a/vendor/github.com/joonix/log/fluentd.go b/vendor/github.com/joonix/log/fluentd.go new file mode 100644 index 0000000000..05100b33ec --- /dev/null +++ b/vendor/github.com/joonix/log/fluentd.go @@ -0,0 +1,60 @@ +package log + +import ( + "encoding/json" + "fmt" + "time" + + "github.com/sirupsen/logrus" +) + +// FluentdFormatter is similar to logrus.JSONFormatter but with log level that are recongnized +// by kubernetes fluentd. +type FluentdFormatter struct { + TimestampFormat string +} + +// Format the log entry. Implements logrus.Formatter. +func (f *FluentdFormatter) Format(entry *logrus.Entry) ([]byte, error) { + data := make(logrus.Fields, len(entry.Data)+3) + for k, v := range entry.Data { + switch v := v.(type) { + case error: + // Otherwise errors are ignored by `encoding/json` + // https://github.com/Sirupsen/logrus/issues/137 + data[k] = v.Error() + default: + data[k] = v + } + } + prefixFieldClashes(data) + + timestampFormat := f.TimestampFormat + if timestampFormat == "" { + timestampFormat = time.RFC3339Nano + } + + data["time"] = entry.Time.Format(timestampFormat) + data["message"] = entry.Message + data["severity"] = entry.Level.String() + + serialized, err := json.Marshal(data) + if err != nil { + return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err) + } + return append(serialized, '\n'), nil +} + +func prefixFieldClashes(data logrus.Fields) { + if t, ok := data["time"]; ok { + data["fields.time"] = t + } + + if m, ok := data["msg"]; ok { + data["fields.msg"] = m + } + + if l, ok := data["level"]; ok { + data["fields.level"] = l + } +}