logging.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 channelz
  19. import (
  20. "fmt"
  21. "google.golang.org/grpc/grpclog"
  22. )
  23. var logger = grpclog.Component("channelz")
  24. // Info logs and adds a trace event if channelz is on.
  25. func Info(l grpclog.DepthLoggerV2, id int64, args ...interface{}) {
  26. if IsOn() {
  27. AddTraceEvent(l, id, 1, &TraceEventDesc{
  28. Desc: fmt.Sprint(args...),
  29. Severity: CtINFO,
  30. })
  31. } else {
  32. l.InfoDepth(1, args...)
  33. }
  34. }
  35. // Infof logs and adds a trace event if channelz is on.
  36. func Infof(l grpclog.DepthLoggerV2, id int64, format string, args ...interface{}) {
  37. msg := fmt.Sprintf(format, args...)
  38. if IsOn() {
  39. AddTraceEvent(l, id, 1, &TraceEventDesc{
  40. Desc: msg,
  41. Severity: CtINFO,
  42. })
  43. } else {
  44. l.InfoDepth(1, msg)
  45. }
  46. }
  47. // Warning logs and adds a trace event if channelz is on.
  48. func Warning(l grpclog.DepthLoggerV2, id int64, args ...interface{}) {
  49. if IsOn() {
  50. AddTraceEvent(l, id, 1, &TraceEventDesc{
  51. Desc: fmt.Sprint(args...),
  52. Severity: CtWarning,
  53. })
  54. } else {
  55. l.WarningDepth(1, args...)
  56. }
  57. }
  58. // Warningf logs and adds a trace event if channelz is on.
  59. func Warningf(l grpclog.DepthLoggerV2, id int64, format string, args ...interface{}) {
  60. msg := fmt.Sprintf(format, args...)
  61. if IsOn() {
  62. AddTraceEvent(l, id, 1, &TraceEventDesc{
  63. Desc: msg,
  64. Severity: CtWarning,
  65. })
  66. } else {
  67. l.WarningDepth(1, msg)
  68. }
  69. }
  70. // Error logs and adds a trace event if channelz is on.
  71. func Error(l grpclog.DepthLoggerV2, id int64, args ...interface{}) {
  72. if IsOn() {
  73. AddTraceEvent(l, id, 1, &TraceEventDesc{
  74. Desc: fmt.Sprint(args...),
  75. Severity: CtError,
  76. })
  77. } else {
  78. l.ErrorDepth(1, args...)
  79. }
  80. }
  81. // Errorf logs and adds a trace event if channelz is on.
  82. func Errorf(l grpclog.DepthLoggerV2, id int64, format string, args ...interface{}) {
  83. msg := fmt.Sprintf(format, args...)
  84. if IsOn() {
  85. AddTraceEvent(l, id, 1, &TraceEventDesc{
  86. Desc: msg,
  87. Severity: CtError,
  88. })
  89. } else {
  90. l.ErrorDepth(1, msg)
  91. }
  92. }