logging.go 2.5 KB

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