From 2d1aa89a4f88930804a0dd6a8fe419b7091d5866 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Sun, 21 Aug 2016 21:07:22 -0700 Subject: [PATCH 1/9] Initial commit --- LICENSE | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 000000000..8dada3eda --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + 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. From 1d8d22fda357dda2c7d9dc15d5dceebccd4d93f3 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Sun, 21 Aug 2016 21:41:28 -0700 Subject: [PATCH 2/9] First commit of glogr --- README.md | 8 ++++++ glogr.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 README.md create mode 100644 glogr.go diff --git a/README.md b/README.md new file mode 100644 index 000000000..3829b03f6 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# Minimal Go logging using glog + +This package implements the [logr interface](https://github.com/thockin/logr) +in terms of Google's [glog](https://godoc.org/github.com/golang/glog). This +provides a relatively minimalist API to logging in Go, backed by a well-proven +implementation. + +This is a BETA grade implementation. diff --git a/glogr.go b/glogr.go new file mode 100644 index 000000000..8287f6f25 --- /dev/null +++ b/glogr.go @@ -0,0 +1,82 @@ +// Package glogr implements github.com/thockin/logr.Logger in terms of +// github.com/golang/glog. +package glogr + +import ( + "fmt" + "runtime" + + "github.com/golang/glog" + "github.com/thockin/logr" +) + +// New returns a logr.Logger which is implemented by glog. +func New() (logr.Logger, error) { + return glogger{ + level: 0, + prefix: "", + }, nil +} + +type glogger struct { + level int + prefix string +} + +func prepend(prefix interface{}, args []interface{}) []interface{} { + return append([]interface{}{prefix}, args...) +} + +// Magic string for intermediate frames that we should ignore. +const autogeneratedFrameName = "" + +// Discover how many frames we need to climb to find the caller. This approach +// was suggested by Ian Lance Taylor of the Go team, so it *should* be safe +// enough (famous last words). +func framesToCaller() int { + // 1 is the immediate caller. 3 should be too many. + for i := 1; i < 3; i++ { + _, file, _, _ := runtime.Caller(i + 1) // +1 for this function's frame + if file != autogeneratedFrameName { + return i + } + } + return 1 // something went wrong, this is safe +} + +func (l glogger) Info(args ...interface{}) { + glog.InfoDepth(framesToCaller(), prepend(l.prefix, args)...) +} + +func (l glogger) Infof(format string, args ...interface{}) { + glog.InfoDepth(framesToCaller(), fmt.Sprintf("%s"+format, prepend(l.prefix, args)...)) +} + +func (l glogger) Enabled() bool { + return bool(glog.V(glog.Level(l.level))) +} + +func (l glogger) Error(args ...interface{}) { + glog.ErrorDepth(framesToCaller(), prepend(l.prefix, args)...) +} + +func (l glogger) Errorf(format string, args ...interface{}) { + glog.ErrorDepth(framesToCaller(), fmt.Sprintf("%s"+format, prepend(l.prefix, args)...)) +} + +func (l glogger) V(level int) logr.InfoLogger { + return glogger{ + level: level, + prefix: l.prefix, + } +} + +func (l glogger) NewWithPrefix(prefix string) logr.Logger { + return glogger{ + level: l.level, + prefix: prefix, + } +} + +var _ logr.Logger = glogger{} +var _ logr.InfoLogger = glogger{} From 7a7f3ced4f9f52e96710761820bd85ed6c400aa5 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Wed, 24 Aug 2016 21:22:32 -0700 Subject: [PATCH 3/9] Check log-level enabled --- glogr.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/glogr.go b/glogr.go index 8287f6f25..d2c8534b8 100644 --- a/glogr.go +++ b/glogr.go @@ -45,11 +45,15 @@ func framesToCaller() int { } func (l glogger) Info(args ...interface{}) { - glog.InfoDepth(framesToCaller(), prepend(l.prefix, args)...) + if l.Enabled() { + glog.InfoDepth(framesToCaller(), prepend(l.prefix, args)...) + } } func (l glogger) Infof(format string, args ...interface{}) { - glog.InfoDepth(framesToCaller(), fmt.Sprintf("%s"+format, prepend(l.prefix, args)...)) + if l.Enabled() { + glog.InfoDepth(framesToCaller(), fmt.Sprintf("%s"+format, prepend(l.prefix, args)...)) + } } func (l glogger) Enabled() bool { From 3d8bcc3cda852f458f37750a15059567c397243a Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Mon, 25 Jun 2018 16:21:27 -0700 Subject: [PATCH 4/9] Convert to structured log support --- example/main.go | 27 ++++++++++++ glogr.go | 112 ++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 116 insertions(+), 23 deletions(-) create mode 100644 example/main.go diff --git a/example/main.go b/example/main.go new file mode 100644 index 000000000..086a86e7f --- /dev/null +++ b/example/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "flag" + + "github.com/go-logr/glogr" + "github.com/golang/glog" +) + +type E struct { + str string +} + +func (e E) Error() string { + return e.str +} + +func main() { + flag.Set("v", "3") + flag.Parse() + log := glogr.New().WithName("MyName").WithValues("user", "you") + log.Info("hello", "val1", 1, "val2", map[string]int{"k": 1}) + log.V(3).Info("nice to meet you") + log.Error(nil, "uh oh", "trouble", true, "reasons", []float64{0.1, 0.11, 3.14}) + log.Error(E{"an error occurred"}, "goodbye", "code", -1) + glog.Flush() +} diff --git a/glogr.go b/glogr.go index d2c8534b8..f5ad48836 100644 --- a/glogr.go +++ b/glogr.go @@ -3,28 +3,43 @@ package glogr import ( + "bytes" + "encoding/json" "fmt" "runtime" + "sort" + "github.com/go-logr/logr" "github.com/golang/glog" - "github.com/thockin/logr" ) // New returns a logr.Logger which is implemented by glog. -func New() (logr.Logger, error) { +func New() logr.Logger { return glogger{ level: 0, prefix: "", - }, nil + values: nil, + } } type glogger struct { level int prefix string + values []interface{} +} + +func (l glogger) clone() glogger { + return glogger{ + level: l.level, + prefix: l.prefix, + values: copySlice(l.values), + } } -func prepend(prefix interface{}, args []interface{}) []interface{} { - return append([]interface{}{prefix}, args...) +func copySlice(in []interface{}) []interface{} { + out := make([]interface{}, len(in)) + copy(out, in) + return out } // Magic string for intermediate frames that we should ignore. @@ -44,15 +59,52 @@ func framesToCaller() int { return 1 // something went wrong, this is safe } -func (l glogger) Info(args ...interface{}) { - if l.Enabled() { - glog.InfoDepth(framesToCaller(), prepend(l.prefix, args)...) +type kvPair struct { + key string + val interface{} +} + +func flatten(kvList ...interface{}) string { + keys := make([]string, 0, len(kvList)) + vals := make(map[string]interface{}, len(kvList)) + for i := 0; i < len(kvList); i += 2 { + k, ok := kvList[i].(string) + if !ok { + panic(fmt.Sprintf("key is not a string: %s", pretty(kvList[i]))) + } + var v interface{} + if i+1 < len(kvList) { + v = kvList[i+1] + } + keys = append(keys, k) + vals[k] = v } + sort.Strings(keys) + buf := bytes.Buffer{} + for i, k := range keys { + v := vals[k] + if i > 0 { + buf.WriteRune(' ') + } + buf.WriteString(pretty(k)) + buf.WriteString("=") + buf.WriteString(pretty(v)) + } + return buf.String() +} + +func pretty(value interface{}) string { + jb, _ := json.Marshal(value) + return string(jb) } -func (l glogger) Infof(format string, args ...interface{}) { +func (l glogger) Info(msg string, kvList ...interface{}) { if l.Enabled() { - glog.InfoDepth(framesToCaller(), fmt.Sprintf("%s"+format, prepend(l.prefix, args)...)) + lvlStr := flatten("level", l.level) + msgStr := flatten("msg", msg) + fixedStr := flatten(l.values...) + userStr := flatten(kvList...) + glog.InfoDepth(framesToCaller(), l.prefix, " ", lvlStr, " ", msgStr, " ", fixedStr, " ", userStr) } } @@ -60,26 +112,40 @@ func (l glogger) Enabled() bool { return bool(glog.V(glog.Level(l.level))) } -func (l glogger) Error(args ...interface{}) { - glog.ErrorDepth(framesToCaller(), prepend(l.prefix, args)...) +func (l glogger) Error(err error, msg string, kvList ...interface{}) { + msgStr := flatten("msg", msg) + var loggableErr interface{} + if err != nil { + loggableErr = err.Error() + } + errStr := flatten("error", loggableErr) + fixedStr := flatten(l.values...) + userStr := flatten(kvList...) + glog.ErrorDepth(framesToCaller(), l.prefix, " ", msgStr, " ", errStr, " ", fixedStr, " ", userStr) } -func (l glogger) Errorf(format string, args ...interface{}) { - glog.ErrorDepth(framesToCaller(), fmt.Sprintf("%s"+format, prepend(l.prefix, args)...)) +func (l glogger) V(level int) logr.InfoLogger { + new := l.clone() + new.level = level + return new } -func (l glogger) V(level int) logr.InfoLogger { - return glogger{ - level: level, - prefix: l.prefix, +// WithName returns a new logr.Logger with the specified name appended. glogr +// uses '/' characters to separate name elements. Callers should not pass '/' +// in the provided name string, but this library does not actually enforce that. +func (l glogger) WithName(name string) logr.Logger { + new := l.clone() + if len(l.prefix) > 0 { + new.prefix = l.prefix + "/" } + new.prefix += name + return new } -func (l glogger) NewWithPrefix(prefix string) logr.Logger { - return glogger{ - level: l.level, - prefix: prefix, - } +func (l glogger) WithValues(kvList ...interface{}) logr.Logger { + new := l.clone() + new.values = append(new.values, kvList...) + return new } var _ logr.Logger = glogger{} From 8d8edd0973b028d50294ba7ac37c6a1fa5d711c0 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Thu, 24 Jan 2019 20:01:08 -0500 Subject: [PATCH 5/9] Switch from glog to klog Change-Id: I92207e2ca9bbd88a8780912a3aef297685c917f6 --- README.md | 6 +++--- example/main.go | 8 ++++---- glogr.go => klogr.go | 43 +++++++++++++++++++------------------------ 3 files changed, 26 insertions(+), 31 deletions(-) rename glogr.go => klogr.go (76%) diff --git a/README.md b/README.md index 3829b03f6..4f5618d7f 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -# Minimal Go logging using glog +# Minimal Go logging using klog -This package implements the [logr interface](https://github.com/thockin/logr) -in terms of Google's [glog](https://godoc.org/github.com/golang/glog). This +This package implements the [logr interface](github.com/go-logr/logr) +in terms of Kubernetes' [klog](https://github.com/kubernetes/klog). This provides a relatively minimalist API to logging in Go, backed by a well-proven implementation. diff --git a/example/main.go b/example/main.go index 086a86e7f..7615586dd 100644 --- a/example/main.go +++ b/example/main.go @@ -3,8 +3,8 @@ package main import ( "flag" - "github.com/go-logr/glogr" - "github.com/golang/glog" + "k8s.io/klog/klogr" + "k8s.io/klog" ) type E struct { @@ -18,10 +18,10 @@ func (e E) Error() string { func main() { flag.Set("v", "3") flag.Parse() - log := glogr.New().WithName("MyName").WithValues("user", "you") + log := klogr.New().WithName("MyName").WithValues("user", "you") log.Info("hello", "val1", 1, "val2", map[string]int{"k": 1}) log.V(3).Info("nice to meet you") log.Error(nil, "uh oh", "trouble", true, "reasons", []float64{0.1, 0.11, 3.14}) log.Error(E{"an error occurred"}, "goodbye", "code", -1) - glog.Flush() + klog.Flush() } diff --git a/glogr.go b/klogr.go similarity index 76% rename from glogr.go rename to klogr.go index f5ad48836..d4e7eb150 100644 --- a/glogr.go +++ b/klogr.go @@ -1,6 +1,6 @@ -// Package glogr implements github.com/thockin/logr.Logger in terms of -// github.com/golang/glog. -package glogr +// Package klogr implements github.com/go-logr/logr.Logger in terms of +// k8s.io/klog. +package klogr import ( "bytes" @@ -10,26 +10,26 @@ import ( "sort" "github.com/go-logr/logr" - "github.com/golang/glog" + "k8s.io/klog" ) // New returns a logr.Logger which is implemented by glog. func New() logr.Logger { - return glogger{ + return klogger{ level: 0, prefix: "", values: nil, } } -type glogger struct { +type klogger struct { level int prefix string values []interface{} } -func (l glogger) clone() glogger { - return glogger{ +func (l klogger) clone() klogger { + return klogger{ level: l.level, prefix: l.prefix, values: copySlice(l.values), @@ -59,11 +59,6 @@ func framesToCaller() int { return 1 // something went wrong, this is safe } -type kvPair struct { - key string - val interface{} -} - func flatten(kvList ...interface{}) string { keys := make([]string, 0, len(kvList)) vals := make(map[string]interface{}, len(kvList)) @@ -98,21 +93,21 @@ func pretty(value interface{}) string { return string(jb) } -func (l glogger) Info(msg string, kvList ...interface{}) { +func (l klogger) Info(msg string, kvList ...interface{}) { if l.Enabled() { lvlStr := flatten("level", l.level) msgStr := flatten("msg", msg) fixedStr := flatten(l.values...) userStr := flatten(kvList...) - glog.InfoDepth(framesToCaller(), l.prefix, " ", lvlStr, " ", msgStr, " ", fixedStr, " ", userStr) + klog.InfoDepth(framesToCaller(), l.prefix, " ", lvlStr, " ", msgStr, " ", fixedStr, " ", userStr) } } -func (l glogger) Enabled() bool { - return bool(glog.V(glog.Level(l.level))) +func (l klogger) Enabled() bool { + return bool(klog.V(klog.Level(l.level))) } -func (l glogger) Error(err error, msg string, kvList ...interface{}) { +func (l klogger) Error(err error, msg string, kvList ...interface{}) { msgStr := flatten("msg", msg) var loggableErr interface{} if err != nil { @@ -121,10 +116,10 @@ func (l glogger) Error(err error, msg string, kvList ...interface{}) { errStr := flatten("error", loggableErr) fixedStr := flatten(l.values...) userStr := flatten(kvList...) - glog.ErrorDepth(framesToCaller(), l.prefix, " ", msgStr, " ", errStr, " ", fixedStr, " ", userStr) + klog.ErrorDepth(framesToCaller(), l.prefix, " ", msgStr, " ", errStr, " ", fixedStr, " ", userStr) } -func (l glogger) V(level int) logr.InfoLogger { +func (l klogger) V(level int) logr.InfoLogger { new := l.clone() new.level = level return new @@ -133,7 +128,7 @@ func (l glogger) V(level int) logr.InfoLogger { // WithName returns a new logr.Logger with the specified name appended. glogr // uses '/' characters to separate name elements. Callers should not pass '/' // in the provided name string, but this library does not actually enforce that. -func (l glogger) WithName(name string) logr.Logger { +func (l klogger) WithName(name string) logr.Logger { new := l.clone() if len(l.prefix) > 0 { new.prefix = l.prefix + "/" @@ -142,11 +137,11 @@ func (l glogger) WithName(name string) logr.Logger { return new } -func (l glogger) WithValues(kvList ...interface{}) logr.Logger { +func (l klogger) WithValues(kvList ...interface{}) logr.Logger { new := l.clone() new.values = append(new.values, kvList...) return new } -var _ logr.Logger = glogger{} -var _ logr.InfoLogger = glogger{} +var _ logr.Logger = klogger{} +var _ logr.InfoLogger = klogger{} From 10149f650600827f6da422e996f4094c36df522f Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Thu, 24 Jan 2019 20:07:17 -0500 Subject: [PATCH 6/9] move files to better locations Change-Id: I3117562d1eecee175ac06443a79b717296f6149e --- {example => examples/klogr}/main.go | 0 README.md => klogr/README.md | 0 klogr.go => klogr/klogr.go | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename {example => examples/klogr}/main.go (100%) rename README.md => klogr/README.md (100%) rename klogr.go => klogr/klogr.go (100%) diff --git a/example/main.go b/examples/klogr/main.go similarity index 100% rename from example/main.go rename to examples/klogr/main.go diff --git a/README.md b/klogr/README.md similarity index 100% rename from README.md rename to klogr/README.md diff --git a/klogr.go b/klogr/klogr.go similarity index 100% rename from klogr.go rename to klogr/klogr.go From 9ab750312189c951dfad57d5c8674d8166510c14 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Thu, 24 Jan 2019 20:08:55 -0500 Subject: [PATCH 7/9] drop extra LICENSE file Change-Id: I813d542005959293327057ff3ddf81da4e454581 --- LICENSE | 201 -------------------------------------------------------- 1 file changed, 201 deletions(-) delete mode 100644 LICENSE diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 8dada3eda..000000000 --- a/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "{}" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright {yyyy} {name of copyright owner} - - 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. From 9f1c2fd0783763a0d40d2c12b9c5fa2e6db31313 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Thu, 24 Jan 2019 20:16:14 -0500 Subject: [PATCH 8/9] fix gofmt issue Change-Id: I4e2c5a7ff5b2526bafac0cd17836b3377a91339d --- examples/klogr/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/klogr/main.go b/examples/klogr/main.go index 7615586dd..ff0a83833 100644 --- a/examples/klogr/main.go +++ b/examples/klogr/main.go @@ -3,8 +3,8 @@ package main import ( "flag" - "k8s.io/klog/klogr" "k8s.io/klog" + "k8s.io/klog/klogr" ) type E struct { From 2ecd970095533717091f32f1a3c4b36eefc822df Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Thu, 24 Jan 2019 20:17:34 -0500 Subject: [PATCH 9/9] fix golint error Change-Id: Ie02835fa7e1fb01ccfe64deb30a414cabae70067 --- examples/klogr/main.go | 6 +++--- klogr/klogr.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/klogr/main.go b/examples/klogr/main.go index ff0a83833..a50264287 100644 --- a/examples/klogr/main.go +++ b/examples/klogr/main.go @@ -7,11 +7,11 @@ import ( "k8s.io/klog/klogr" ) -type E struct { +type myError struct { str string } -func (e E) Error() string { +func (e myError) Error() string { return e.str } @@ -22,6 +22,6 @@ func main() { log.Info("hello", "val1", 1, "val2", map[string]int{"k": 1}) log.V(3).Info("nice to meet you") log.Error(nil, "uh oh", "trouble", true, "reasons", []float64{0.1, 0.11, 3.14}) - log.Error(E{"an error occurred"}, "goodbye", "code", -1) + log.Error(myError{"an error occurred"}, "goodbye", "code", -1) klog.Flush() } diff --git a/klogr/klogr.go b/klogr/klogr.go index d4e7eb150..e09e82212 100644 --- a/klogr/klogr.go +++ b/klogr/klogr.go @@ -13,7 +13,7 @@ import ( "k8s.io/klog" ) -// New returns a logr.Logger which is implemented by glog. +// New returns a logr.Logger which is implemented by klog. func New() logr.Logger { return klogger{ level: 0, @@ -125,7 +125,7 @@ func (l klogger) V(level int) logr.InfoLogger { return new } -// WithName returns a new logr.Logger with the specified name appended. glogr +// WithName returns a new logr.Logger with the specified name appended. klogr // uses '/' characters to separate name elements. Callers should not pass '/' // in the provided name string, but this library does not actually enforce that. func (l klogger) WithName(name string) logr.Logger {