logger.go 2.0 KB

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