loggerv2.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. *
  3. * Copyright 2017 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. "io"
  21. "io/ioutil"
  22. "log"
  23. "os"
  24. "strconv"
  25. )
  26. // LoggerV2 does underlying logging work for grpclog.
  27. type LoggerV2 interface {
  28. // Info logs to INFO log. Arguments are handled in the manner of fmt.Print.
  29. Info(args ...interface{})
  30. // Infoln logs to INFO log. Arguments are handled in the manner of fmt.Println.
  31. Infoln(args ...interface{})
  32. // Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
  33. Infof(format string, args ...interface{})
  34. // Warning logs to WARNING log. Arguments are handled in the manner of fmt.Print.
  35. Warning(args ...interface{})
  36. // Warningln logs to WARNING log. Arguments are handled in the manner of fmt.Println.
  37. Warningln(args ...interface{})
  38. // Warningf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
  39. Warningf(format string, args ...interface{})
  40. // Error logs to ERROR log. Arguments are handled in the manner of fmt.Print.
  41. Error(args ...interface{})
  42. // Errorln logs to ERROR log. Arguments are handled in the manner of fmt.Println.
  43. Errorln(args ...interface{})
  44. // Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
  45. Errorf(format string, args ...interface{})
  46. // Fatal logs to ERROR log. Arguments are handled in the manner of fmt.Print.
  47. // gRPC ensures that all Fatal logs will exit with os.Exit(1).
  48. // Implementations may also call os.Exit() with a non-zero exit code.
  49. Fatal(args ...interface{})
  50. // Fatalln logs to ERROR log. Arguments are handled in the manner of fmt.Println.
  51. // gRPC ensures that all Fatal logs will exit with os.Exit(1).
  52. // Implementations may also call os.Exit() with a non-zero exit code.
  53. Fatalln(args ...interface{})
  54. // Fatalf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
  55. // gRPC ensures that all Fatal logs will exit with os.Exit(1).
  56. // Implementations may also call os.Exit() with a non-zero exit code.
  57. Fatalf(format string, args ...interface{})
  58. // V reports whether verbosity level l is at least the requested verbose level.
  59. V(l int) bool
  60. }
  61. // SetLoggerV2 sets logger that is used in grpc to a V2 logger.
  62. // Not mutex-protected, should be called before any gRPC functions.
  63. func SetLoggerV2(l LoggerV2) {
  64. logger = l
  65. }
  66. const (
  67. // infoLog indicates Info severity.
  68. infoLog int = iota
  69. // warningLog indicates Warning severity.
  70. warningLog
  71. // errorLog indicates Error severity.
  72. errorLog
  73. // fatalLog indicates Fatal severity.
  74. fatalLog
  75. )
  76. // severityName contains the string representation of each severity.
  77. var severityName = []string{
  78. infoLog: "INFO",
  79. warningLog: "WARNING",
  80. errorLog: "ERROR",
  81. fatalLog: "FATAL",
  82. }
  83. // loggerT is the default logger used by grpclog.
  84. type loggerT struct {
  85. m []*log.Logger
  86. v int
  87. }
  88. // NewLoggerV2 creates a loggerV2 with the provided writers.
  89. // Fatal logs will be written to errorW, warningW, infoW, followed by exit(1).
  90. // Error logs will be written to errorW, warningW and infoW.
  91. // Warning logs will be written to warningW and infoW.
  92. // Info logs will be written to infoW.
  93. func NewLoggerV2(infoW, warningW, errorW io.Writer) LoggerV2 {
  94. return NewLoggerV2WithVerbosity(infoW, warningW, errorW, 0)
  95. }
  96. // NewLoggerV2WithVerbosity creates a loggerV2 with the provided writers and
  97. // verbosity level.
  98. func NewLoggerV2WithVerbosity(infoW, warningW, errorW io.Writer, v int) LoggerV2 {
  99. var m []*log.Logger
  100. m = append(m, log.New(infoW, severityName[infoLog]+": ", log.LstdFlags))
  101. m = append(m, log.New(io.MultiWriter(infoW, warningW), severityName[warningLog]+": ", log.LstdFlags))
  102. ew := io.MultiWriter(infoW, warningW, errorW) // ew will be used for error and fatal.
  103. m = append(m, log.New(ew, severityName[errorLog]+": ", log.LstdFlags))
  104. m = append(m, log.New(ew, severityName[fatalLog]+": ", log.LstdFlags))
  105. return &loggerT{m: m, v: v}
  106. }
  107. // newLoggerV2 creates a loggerV2 to be used as default logger.
  108. // All logs are written to stderr.
  109. func newLoggerV2() LoggerV2 {
  110. errorW := ioutil.Discard
  111. warningW := ioutil.Discard
  112. infoW := ioutil.Discard
  113. logLevel := os.Getenv("GRPC_GO_LOG_SEVERITY_LEVEL")
  114. switch logLevel {
  115. case "", "ERROR", "error": // If env is unset, set level to ERROR.
  116. errorW = os.Stderr
  117. case "WARNING", "warning":
  118. warningW = os.Stderr
  119. case "INFO", "info":
  120. infoW = os.Stderr
  121. }
  122. var v int
  123. vLevel := os.Getenv("GRPC_GO_LOG_VERBOSITY_LEVEL")
  124. if vl, err := strconv.Atoi(vLevel); err == nil {
  125. v = vl
  126. }
  127. return NewLoggerV2WithVerbosity(infoW, warningW, errorW, v)
  128. }
  129. func (g *loggerT) Info(args ...interface{}) {
  130. g.m[infoLog].Print(args...)
  131. }
  132. func (g *loggerT) Infoln(args ...interface{}) {
  133. g.m[infoLog].Println(args...)
  134. }
  135. func (g *loggerT) Infof(format string, args ...interface{}) {
  136. g.m[infoLog].Printf(format, args...)
  137. }
  138. func (g *loggerT) Warning(args ...interface{}) {
  139. g.m[warningLog].Print(args...)
  140. }
  141. func (g *loggerT) Warningln(args ...interface{}) {
  142. g.m[warningLog].Println(args...)
  143. }
  144. func (g *loggerT) Warningf(format string, args ...interface{}) {
  145. g.m[warningLog].Printf(format, args...)
  146. }
  147. func (g *loggerT) Error(args ...interface{}) {
  148. g.m[errorLog].Print(args...)
  149. }
  150. func (g *loggerT) Errorln(args ...interface{}) {
  151. g.m[errorLog].Println(args...)
  152. }
  153. func (g *loggerT) Errorf(format string, args ...interface{}) {
  154. g.m[errorLog].Printf(format, args...)
  155. }
  156. func (g *loggerT) Fatal(args ...interface{}) {
  157. g.m[fatalLog].Fatal(args...)
  158. // No need to call os.Exit() again because log.Logger.Fatal() calls os.Exit().
  159. }
  160. func (g *loggerT) Fatalln(args ...interface{}) {
  161. g.m[fatalLog].Fatalln(args...)
  162. // No need to call os.Exit() again because log.Logger.Fatal() calls os.Exit().
  163. }
  164. func (g *loggerT) Fatalf(format string, args ...interface{}) {
  165. g.m[fatalLog].Fatalf(format, args...)
  166. // No need to call os.Exit() again because log.Logger.Fatal() calls os.Exit().
  167. }
  168. func (g *loggerT) V(l int) bool {
  169. return l <= g.v
  170. }