zapgrpc_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package zapgrpc
  21. import (
  22. "fmt"
  23. "testing"
  24. "go.uber.org/zap"
  25. "go.uber.org/zap/zapcore"
  26. "go.uber.org/zap/zaptest/observer"
  27. "github.com/stretchr/testify/require"
  28. )
  29. func TestLoggerInfoExpected(t *testing.T) {
  30. checkMessages(t, zapcore.DebugLevel, nil, zapcore.InfoLevel, []string{
  31. "hello",
  32. "s1s21 2 3s34s56",
  33. "hello world",
  34. "",
  35. "foo",
  36. "foo bar",
  37. "s1 s2 1 2 3 s3 4 s5 6",
  38. "hello",
  39. "s1s21 2 3s34s56",
  40. "hello world",
  41. "",
  42. "foo",
  43. "foo bar",
  44. "s1 s2 1 2 3 s3 4 s5 6",
  45. }, func(logger *Logger) {
  46. logger.Info("hello")
  47. logger.Info("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
  48. logger.Infof("%s world", "hello")
  49. logger.Infoln()
  50. logger.Infoln("foo")
  51. logger.Infoln("foo", "bar")
  52. logger.Infoln("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
  53. logger.Print("hello")
  54. logger.Print("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
  55. logger.Printf("%s world", "hello")
  56. logger.Println()
  57. logger.Println("foo")
  58. logger.Println("foo", "bar")
  59. logger.Println("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
  60. })
  61. }
  62. func TestLoggerDebugExpected(t *testing.T) {
  63. checkMessages(t, zapcore.DebugLevel, []Option{WithDebug()}, zapcore.DebugLevel, []string{
  64. "hello",
  65. "s1s21 2 3s34s56",
  66. "hello world",
  67. "",
  68. "foo",
  69. "foo bar",
  70. "s1 s2 1 2 3 s3 4 s5 6",
  71. }, func(logger *Logger) {
  72. logger.Print("hello")
  73. logger.Print("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
  74. logger.Printf("%s world", "hello")
  75. logger.Println()
  76. logger.Println("foo")
  77. logger.Println("foo", "bar")
  78. logger.Println("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
  79. })
  80. }
  81. func TestLoggerDebugSuppressed(t *testing.T) {
  82. checkMessages(t, zapcore.InfoLevel, []Option{WithDebug()}, zapcore.DebugLevel, nil, func(logger *Logger) {
  83. logger.Print("hello")
  84. logger.Printf("%s world", "hello")
  85. logger.Println()
  86. logger.Println("foo")
  87. logger.Println("foo", "bar")
  88. })
  89. }
  90. func TestLoggerWarningExpected(t *testing.T) {
  91. checkMessages(t, zapcore.DebugLevel, nil, zapcore.WarnLevel, []string{
  92. "hello",
  93. "s1s21 2 3s34s56",
  94. "hello world",
  95. "",
  96. "foo",
  97. "foo bar",
  98. "s1 s2 1 2 3 s3 4 s5 6",
  99. }, func(logger *Logger) {
  100. logger.Warning("hello")
  101. logger.Warning("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
  102. logger.Warningf("%s world", "hello")
  103. logger.Warningln()
  104. logger.Warningln("foo")
  105. logger.Warningln("foo", "bar")
  106. logger.Warningln("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
  107. })
  108. }
  109. func TestLoggerErrorExpected(t *testing.T) {
  110. checkMessages(t, zapcore.DebugLevel, nil, zapcore.ErrorLevel, []string{
  111. "hello",
  112. "s1s21 2 3s34s56",
  113. "hello world",
  114. "",
  115. "foo",
  116. "foo bar",
  117. "s1 s2 1 2 3 s3 4 s5 6",
  118. }, func(logger *Logger) {
  119. logger.Error("hello")
  120. logger.Error("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
  121. logger.Errorf("%s world", "hello")
  122. logger.Errorln()
  123. logger.Errorln("foo")
  124. logger.Errorln("foo", "bar")
  125. logger.Errorln("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
  126. })
  127. }
  128. func TestLoggerFatalExpected(t *testing.T) {
  129. checkMessages(t, zapcore.DebugLevel, nil, zapcore.FatalLevel, []string{
  130. "hello",
  131. "s1s21 2 3s34s56",
  132. "hello world",
  133. "",
  134. "foo",
  135. "foo bar",
  136. "s1 s2 1 2 3 s3 4 s5 6",
  137. }, func(logger *Logger) {
  138. logger.Fatal("hello")
  139. logger.Fatal("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
  140. logger.Fatalf("%s world", "hello")
  141. logger.Fatalln()
  142. logger.Fatalln("foo")
  143. logger.Fatalln("foo", "bar")
  144. logger.Fatalln("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
  145. })
  146. }
  147. func TestLoggerV(t *testing.T) {
  148. tests := []struct {
  149. zapLevel zapcore.Level
  150. grpcEnabled []int
  151. grpcDisabled []int
  152. }{
  153. {
  154. zapLevel: zapcore.DebugLevel,
  155. grpcEnabled: []int{grpcLvlInfo, grpcLvlWarn, grpcLvlError, grpcLvlFatal},
  156. grpcDisabled: []int{}, // everything is enabled, nothing is disabled
  157. },
  158. {
  159. zapLevel: zapcore.InfoLevel,
  160. grpcEnabled: []int{grpcLvlInfo, grpcLvlWarn, grpcLvlError, grpcLvlFatal},
  161. grpcDisabled: []int{}, // everything is enabled, nothing is disabled
  162. },
  163. {
  164. zapLevel: zapcore.WarnLevel,
  165. grpcEnabled: []int{grpcLvlWarn, grpcLvlError, grpcLvlFatal},
  166. grpcDisabled: []int{grpcLvlInfo},
  167. },
  168. {
  169. zapLevel: zapcore.ErrorLevel,
  170. grpcEnabled: []int{grpcLvlError, grpcLvlFatal},
  171. grpcDisabled: []int{grpcLvlInfo, grpcLvlWarn},
  172. },
  173. {
  174. zapLevel: zapcore.DPanicLevel,
  175. grpcEnabled: []int{grpcLvlFatal},
  176. grpcDisabled: []int{grpcLvlInfo, grpcLvlWarn, grpcLvlError},
  177. },
  178. {
  179. zapLevel: zapcore.PanicLevel,
  180. grpcEnabled: []int{grpcLvlFatal},
  181. grpcDisabled: []int{grpcLvlInfo, grpcLvlWarn, grpcLvlError},
  182. },
  183. {
  184. zapLevel: zapcore.FatalLevel,
  185. grpcEnabled: []int{grpcLvlFatal},
  186. grpcDisabled: []int{grpcLvlInfo, grpcLvlWarn, grpcLvlError},
  187. },
  188. }
  189. for _, tst := range tests {
  190. for _, grpcLvl := range tst.grpcEnabled {
  191. t.Run(fmt.Sprintf("enabled %s %d", tst.zapLevel, grpcLvl), func(t *testing.T) {
  192. checkLevel(t, tst.zapLevel, true, func(logger *Logger) bool {
  193. return logger.V(grpcLvl)
  194. })
  195. })
  196. }
  197. for _, grpcLvl := range tst.grpcDisabled {
  198. t.Run(fmt.Sprintf("disabled %s %d", tst.zapLevel, grpcLvl), func(t *testing.T) {
  199. checkLevel(t, tst.zapLevel, false, func(logger *Logger) bool {
  200. return logger.V(grpcLvl)
  201. })
  202. })
  203. }
  204. }
  205. }
  206. func checkLevel(
  207. t testing.TB,
  208. enab zapcore.LevelEnabler,
  209. expectedBool bool,
  210. f func(*Logger) bool,
  211. ) {
  212. withLogger(enab, nil, func(logger *Logger, observedLogs *observer.ObservedLogs) {
  213. actualBool := f(logger)
  214. if expectedBool {
  215. require.True(t, actualBool)
  216. } else {
  217. require.False(t, actualBool)
  218. }
  219. })
  220. }
  221. func checkMessages(
  222. t testing.TB,
  223. enab zapcore.LevelEnabler,
  224. opts []Option,
  225. expectedLevel zapcore.Level,
  226. expectedMessages []string,
  227. f func(*Logger),
  228. ) {
  229. if expectedLevel == zapcore.FatalLevel {
  230. expectedLevel = zapcore.WarnLevel
  231. }
  232. withLogger(enab, opts, func(logger *Logger, observedLogs *observer.ObservedLogs) {
  233. f(logger)
  234. logEntries := observedLogs.All()
  235. require.Equal(t, len(expectedMessages), len(logEntries))
  236. for i, logEntry := range logEntries {
  237. require.Equal(t, expectedLevel, logEntry.Level)
  238. require.Equal(t, expectedMessages[i], logEntry.Message)
  239. }
  240. })
  241. }
  242. func withLogger(
  243. enab zapcore.LevelEnabler,
  244. opts []Option,
  245. f func(*Logger, *observer.ObservedLogs),
  246. ) {
  247. core, observedLogs := observer.New(enab)
  248. f(NewLogger(zap.New(core), append(opts, withWarn())...), observedLogs)
  249. }