loggerv2.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. if _, ok := l.(*componentData); ok {
  66. panic("cannot use component logger as grpclog logger")
  67. }
  68. grpclog.Logger = l
  69. grpclog.DepthLogger, _ = l.(grpclog.DepthLoggerV2)
  70. }
  71. const (
  72. // infoLog indicates Info severity.
  73. infoLog int = iota
  74. // warningLog indicates Warning severity.
  75. warningLog
  76. // errorLog indicates Error severity.
  77. errorLog
  78. // fatalLog indicates Fatal severity.
  79. fatalLog
  80. )
  81. // severityName contains the string representation of each severity.
  82. var severityName = []string{
  83. infoLog: "INFO",
  84. warningLog: "WARNING",
  85. errorLog: "ERROR",
  86. fatalLog: "FATAL",
  87. }
  88. // loggerT is the default logger used by grpclog.
  89. type loggerT struct {
  90. m []*log.Logger
  91. v int
  92. }
  93. // NewLoggerV2 creates a loggerV2 with the provided writers.
  94. // Fatal logs will be written to errorW, warningW, infoW, followed by exit(1).
  95. // Error logs will be written to errorW, warningW and infoW.
  96. // Warning logs will be written to warningW and infoW.
  97. // Info logs will be written to infoW.
  98. func NewLoggerV2(infoW, warningW, errorW io.Writer) LoggerV2 {
  99. return NewLoggerV2WithVerbosity(infoW, warningW, errorW, 0)
  100. }
  101. // NewLoggerV2WithVerbosity creates a loggerV2 with the provided writers and
  102. // verbosity level.
  103. func NewLoggerV2WithVerbosity(infoW, warningW, errorW io.Writer, v int) LoggerV2 {
  104. var m []*log.Logger
  105. m = append(m, log.New(infoW, severityName[infoLog]+": ", log.LstdFlags))
  106. m = append(m, log.New(io.MultiWriter(infoW, warningW), severityName[warningLog]+": ", log.LstdFlags))
  107. ew := io.MultiWriter(infoW, warningW, errorW) // ew will be used for error and fatal.
  108. m = append(m, log.New(ew, severityName[errorLog]+": ", log.LstdFlags))
  109. m = append(m, log.New(ew, severityName[fatalLog]+": ", log.LstdFlags))
  110. return &loggerT{m: m, v: v}
  111. }
  112. // newLoggerV2 creates a loggerV2 to be used as default logger.
  113. // All logs are written to stderr.
  114. func newLoggerV2() LoggerV2 {
  115. errorW := ioutil.Discard
  116. warningW := ioutil.Discard
  117. infoW := ioutil.Discard
  118. logLevel := os.Getenv("GRPC_GO_LOG_SEVERITY_LEVEL")
  119. switch logLevel {
  120. case "", "ERROR", "error": // If env is unset, set level to ERROR.
  121. errorW = os.Stderr
  122. case "WARNING", "warning":
  123. warningW = os.Stderr
  124. case "INFO", "info":
  125. infoW = os.Stderr
  126. }
  127. var v int
  128. vLevel := os.Getenv("GRPC_GO_LOG_VERBOSITY_LEVEL")
  129. if vl, err := strconv.Atoi(vLevel); err == nil {
  130. v = vl
  131. }
  132. return NewLoggerV2WithVerbosity(infoW, warningW, errorW, v)
  133. }
  134. func (g *loggerT) Info(args ...interface{}) {
  135. g.m[infoLog].Print(args...)
  136. }
  137. func (g *loggerT) Infoln(args ...interface{}) {
  138. g.m[infoLog].Println(args...)
  139. }
  140. func (g *loggerT) Infof(format string, args ...interface{}) {
  141. g.m[infoLog].Printf(format, args...)
  142. }
  143. func (g *loggerT) Warning(args ...interface{}) {
  144. g.m[warningLog].Print(args...)
  145. }
  146. func (g *loggerT) Warningln(args ...interface{}) {
  147. g.m[warningLog].Println(args...)
  148. }
  149. func (g *loggerT) Warningf(format string, args ...interface{}) {
  150. g.m[warningLog].Printf(format, args...)
  151. }
  152. func (g *loggerT) Error(args ...interface{}) {
  153. g.m[errorLog].Print(args...)
  154. }
  155. func (g *loggerT) Errorln(args ...interface{}) {
  156. g.m[errorLog].Println(args...)
  157. }
  158. func (g *loggerT) Errorf(format string, args ...interface{}) {
  159. g.m[errorLog].Printf(format, args...)
  160. }
  161. func (g *loggerT) Fatal(args ...interface{}) {
  162. g.m[fatalLog].Fatal(args...)
  163. // No need to call os.Exit() again because log.Logger.Fatal() calls os.Exit().
  164. }
  165. func (g *loggerT) Fatalln(args ...interface{}) {
  166. g.m[fatalLog].Fatalln(args...)
  167. // No need to call os.Exit() again because log.Logger.Fatal() calls os.Exit().
  168. }
  169. func (g *loggerT) Fatalf(format string, args ...interface{}) {
  170. g.m[fatalLog].Fatalf(format, args...)
  171. // No need to call os.Exit() again because log.Logger.Fatal() calls os.Exit().
  172. }
  173. func (g *loggerT) V(l int) bool {
  174. return l <= g.v
  175. }
  176. // DepthLoggerV2 logs at a specified call frame. If a LoggerV2 also implements
  177. // DepthLoggerV2, the below functions will be called with the appropriate stack
  178. // depth set for trivial functions the logger may ignore.
  179. //
  180. // Experimental
  181. //
  182. // Notice: This type is EXPERIMENTAL and may be changed or removed in a
  183. // later release.
  184. type DepthLoggerV2 interface {
  185. LoggerV2
  186. // InfoDepth logs to INFO log at the specified depth. Arguments are handled in the manner of fmt.Print.
  187. InfoDepth(depth int, args ...interface{})
  188. // WarningDepth logs to WARNING log at the specified depth. Arguments are handled in the manner of fmt.Print.
  189. WarningDepth(depth int, args ...interface{})
  190. // ErrorDetph logs to ERROR log at the specified depth. Arguments are handled in the manner of fmt.Print.
  191. ErrorDepth(depth int, args ...interface{})
  192. // FatalDepth logs to FATAL log at the specified depth. Arguments are handled in the manner of fmt.Print.
  193. FatalDepth(depth int, args ...interface{})
  194. }