component.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. *
  3. * Copyright 2020 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package grpclog
  19. import (
  20. "fmt"
  21. "google.golang.org/grpc/internal/grpclog"
  22. )
  23. // componentData records the settings for a component.
  24. type componentData struct {
  25. name string
  26. }
  27. var cache = map[string]*componentData{}
  28. func (c *componentData) InfoDepth(depth int, args ...interface{}) {
  29. args = append([]interface{}{"[" + string(c.name) + "]"}, args...)
  30. grpclog.InfoDepth(depth+1, args...)
  31. }
  32. func (c *componentData) WarningDepth(depth int, args ...interface{}) {
  33. args = append([]interface{}{"[" + string(c.name) + "]"}, args...)
  34. grpclog.WarningDepth(depth+1, args...)
  35. }
  36. func (c *componentData) ErrorDepth(depth int, args ...interface{}) {
  37. args = append([]interface{}{"[" + string(c.name) + "]"}, args...)
  38. grpclog.ErrorDepth(depth+1, args...)
  39. }
  40. func (c *componentData) FatalDepth(depth int, args ...interface{}) {
  41. args = append([]interface{}{"[" + string(c.name) + "]"}, args...)
  42. grpclog.FatalDepth(depth+1, args...)
  43. }
  44. func (c *componentData) Info(args ...interface{}) {
  45. c.InfoDepth(1, args...)
  46. }
  47. func (c *componentData) Warning(args ...interface{}) {
  48. c.WarningDepth(1, args...)
  49. }
  50. func (c *componentData) Error(args ...interface{}) {
  51. c.ErrorDepth(1, args...)
  52. }
  53. func (c *componentData) Fatal(args ...interface{}) {
  54. c.FatalDepth(1, args...)
  55. }
  56. func (c *componentData) Infof(format string, args ...interface{}) {
  57. c.InfoDepth(1, fmt.Sprintf(format, args...))
  58. }
  59. func (c *componentData) Warningf(format string, args ...interface{}) {
  60. c.WarningDepth(1, fmt.Sprintf(format, args...))
  61. }
  62. func (c *componentData) Errorf(format string, args ...interface{}) {
  63. c.ErrorDepth(1, fmt.Sprintf(format, args...))
  64. }
  65. func (c *componentData) Fatalf(format string, args ...interface{}) {
  66. c.FatalDepth(1, fmt.Sprintf(format, args...))
  67. }
  68. func (c *componentData) Infoln(args ...interface{}) {
  69. c.InfoDepth(1, args...)
  70. }
  71. func (c *componentData) Warningln(args ...interface{}) {
  72. c.WarningDepth(1, args...)
  73. }
  74. func (c *componentData) Errorln(args ...interface{}) {
  75. c.ErrorDepth(1, args...)
  76. }
  77. func (c *componentData) Fatalln(args ...interface{}) {
  78. c.FatalDepth(1, args...)
  79. }
  80. func (c *componentData) V(l int) bool {
  81. return grpclog.Logger.V(l)
  82. }
  83. // Component creates a new component and returns it for logging. If a component
  84. // with the name already exists, nothing will be created and it will be
  85. // returned. SetLoggerV2 will panic if it is called with a logger created by
  86. // Component.
  87. func Component(componentName string) DepthLoggerV2 {
  88. if cData, ok := cache[componentName]; ok {
  89. return cData
  90. }
  91. c := &componentData{componentName}
  92. cache[componentName] = c
  93. return c
  94. }