loggerv2.go 7.3 KB

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