logger.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package alibaba
  13. import (
  14. "github.com/go-logr/logr"
  15. "github.com/hashicorp/go-retryablehttp"
  16. ctrl "sigs.k8s.io/controller-runtime"
  17. )
  18. var log = newLogger()
  19. type logLevel int
  20. const (
  21. logLevelWarn logLevel = iota
  22. logLevelInfo logLevel = iota
  23. logLevelDebug logLevel = iota
  24. )
  25. type logger struct {
  26. logr.Logger
  27. }
  28. func (l logLevel) Level() int {
  29. return int(l)
  30. }
  31. func newLogger() *logger {
  32. return &logger{
  33. Logger: ctrl.Log.WithName("provider").WithName("alibaba").WithName("kms"),
  34. }
  35. }
  36. var _ retryablehttp.LeveledLogger = (*logger)(nil)
  37. var _ retryablehttp.Logger = (*logger)(nil)
  38. func (l *logger) WithField(key string, value interface{}) *logger {
  39. return l.WithFields(key, value)
  40. }
  41. func (l *logger) WithError(err error) *logger {
  42. return l.WithFields("error", err)
  43. }
  44. func (l *logger) WithFields(keysAndValues ...interface{}) *logger {
  45. newLogger := *l
  46. newLogger.Logger = l.Logger.WithValues(keysAndValues...)
  47. return &newLogger
  48. }
  49. func (l *logger) Error(msg string, keysAndValues ...interface{}) {
  50. l.Logger.Error(nil, msg, keysAndValues...)
  51. }
  52. func (l *logger) Info(msg string, keysAndValues ...interface{}) {
  53. l.Logger.V(logLevelInfo.Level()).Info(msg, keysAndValues...)
  54. }
  55. func (l *logger) Debug(msg string, keysAndValues ...interface{}) {
  56. l.Logger.V(logLevelDebug.Level()).Info(msg, keysAndValues...)
  57. }
  58. func (l *logger) Warn(msg string, keysAndValues ...interface{}) {
  59. l.Logger.V(logLevelWarn.Level()).Info(msg, keysAndValues...)
  60. }
  61. func (l *logger) Printf(msg string, keysAndValues ...interface{}) {
  62. l.Logger.Info(msg, keysAndValues...)
  63. }