config_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 zap
  21. import (
  22. "io/ioutil"
  23. "os"
  24. "testing"
  25. "github.com/stretchr/testify/assert"
  26. "github.com/stretchr/testify/require"
  27. "go.uber.org/atomic"
  28. "go.uber.org/zap/zapcore"
  29. )
  30. func TestConfig(t *testing.T) {
  31. tests := []struct {
  32. desc string
  33. cfg Config
  34. expectN int64
  35. expectRe string
  36. }{
  37. {
  38. desc: "production",
  39. cfg: NewProductionConfig(),
  40. expectN: 2 + 100 + 1, // 2 from initial logs, 100 initial sampled logs, 1 from off-by-one in sampler
  41. expectRe: `{"level":"info","caller":"zap/config_test.go:\d+","msg":"info","k":"v","z":"zz"}` + "\n" +
  42. `{"level":"warn","caller":"zap/config_test.go:\d+","msg":"warn","k":"v","z":"zz"}` + "\n",
  43. },
  44. {
  45. desc: "development",
  46. cfg: NewDevelopmentConfig(),
  47. expectN: 3 + 200, // 3 initial logs, all 200 subsequent logs
  48. expectRe: "DEBUG\tzap/config_test.go:" + `\d+` + "\tdebug\t" + `{"k": "v", "z": "zz"}` + "\n" +
  49. "INFO\tzap/config_test.go:" + `\d+` + "\tinfo\t" + `{"k": "v", "z": "zz"}` + "\n" +
  50. "WARN\tzap/config_test.go:" + `\d+` + "\twarn\t" + `{"k": "v", "z": "zz"}` + "\n" +
  51. `testing.\w+`,
  52. },
  53. }
  54. for _, tt := range tests {
  55. t.Run(tt.desc, func(t *testing.T) {
  56. temp, err := ioutil.TempFile("", "zap-prod-config-test")
  57. require.NoError(t, err, "Failed to create temp file.")
  58. defer os.Remove(temp.Name())
  59. tt.cfg.OutputPaths = []string{temp.Name()}
  60. tt.cfg.EncoderConfig.TimeKey = "" // no timestamps in tests
  61. tt.cfg.InitialFields = map[string]interface{}{"z": "zz", "k": "v"}
  62. hook, count := makeCountingHook()
  63. logger, err := tt.cfg.Build(Hooks(hook))
  64. require.NoError(t, err, "Unexpected error constructing logger.")
  65. logger.Debug("debug")
  66. logger.Info("info")
  67. logger.Warn("warn")
  68. byteContents, err := ioutil.ReadAll(temp)
  69. require.NoError(t, err, "Couldn't read log contents from temp file.")
  70. logs := string(byteContents)
  71. assert.Regexp(t, tt.expectRe, logs, "Unexpected log output.")
  72. for i := 0; i < 200; i++ {
  73. logger.Info("sampling")
  74. }
  75. assert.Equal(t, tt.expectN, count.Load(), "Hook called an unexpected number of times.")
  76. })
  77. }
  78. }
  79. func TestConfigWithInvalidPaths(t *testing.T) {
  80. tests := []struct {
  81. desc string
  82. output string
  83. errOutput string
  84. }{
  85. {"output directory doesn't exist", "/tmp/not-there/foo.log", "stderr"},
  86. {"error output directory doesn't exist", "stdout", "/tmp/not-there/foo-errors.log"},
  87. {"neither output directory exists", "/tmp/not-there/foo.log", "/tmp/not-there/foo-errors.log"},
  88. }
  89. for _, tt := range tests {
  90. t.Run(tt.desc, func(t *testing.T) {
  91. cfg := NewProductionConfig()
  92. cfg.OutputPaths = []string{tt.output}
  93. cfg.ErrorOutputPaths = []string{tt.errOutput}
  94. _, err := cfg.Build()
  95. assert.Error(t, err, "Expected an error opening a non-existent directory.")
  96. })
  97. }
  98. }
  99. func TestConfigWithMissingAttributes(t *testing.T) {
  100. tests := []struct {
  101. desc string
  102. cfg Config
  103. expectErr string
  104. }{
  105. {
  106. desc: "missing level",
  107. cfg: Config{
  108. Encoding: "json",
  109. },
  110. expectErr: "missing Level",
  111. },
  112. {
  113. desc: "missing encoder time in encoder config",
  114. cfg: Config{
  115. Level: NewAtomicLevelAt(zapcore.InfoLevel),
  116. Encoding: "json",
  117. EncoderConfig: zapcore.EncoderConfig{
  118. MessageKey: "msg",
  119. TimeKey: "ts",
  120. },
  121. },
  122. expectErr: "missing EncodeTime in EncoderConfig",
  123. },
  124. }
  125. for _, tt := range tests {
  126. t.Run(tt.desc, func(t *testing.T) {
  127. cfg := tt.cfg
  128. _, err := cfg.Build()
  129. require.Error(t, err)
  130. assert.Equal(t, tt.expectErr, err.Error())
  131. })
  132. }
  133. }
  134. func makeSamplerCountingHook() (h func(zapcore.Entry, zapcore.SamplingDecision),
  135. dropped, sampled *atomic.Int64) {
  136. dropped = new(atomic.Int64)
  137. sampled = new(atomic.Int64)
  138. h = func(_ zapcore.Entry, dec zapcore.SamplingDecision) {
  139. if dec&zapcore.LogDropped > 0 {
  140. dropped.Inc()
  141. } else if dec&zapcore.LogSampled > 0 {
  142. sampled.Inc()
  143. }
  144. }
  145. return h, dropped, sampled
  146. }
  147. func TestConfigWithSamplingHook(t *testing.T) {
  148. shook, dcount, scount := makeSamplerCountingHook()
  149. cfg := Config{
  150. Level: NewAtomicLevelAt(InfoLevel),
  151. Development: false,
  152. Sampling: &SamplingConfig{
  153. Initial: 100,
  154. Thereafter: 100,
  155. Hook: shook,
  156. },
  157. Encoding: "json",
  158. EncoderConfig: NewProductionEncoderConfig(),
  159. OutputPaths: []string{"stderr"},
  160. ErrorOutputPaths: []string{"stderr"},
  161. }
  162. expectRe := `{"level":"info","caller":"zap/config_test.go:\d+","msg":"info","k":"v","z":"zz"}` + "\n" +
  163. `{"level":"warn","caller":"zap/config_test.go:\d+","msg":"warn","k":"v","z":"zz"}` + "\n"
  164. expectDropped := 99 // 200 - 100 initial - 1 thereafter
  165. expectSampled := 103 // 2 from initial + 100 + 1 thereafter
  166. temp, err := ioutil.TempFile("", "zap-prod-config-test")
  167. require.NoError(t, err, "Failed to create temp file.")
  168. defer func() {
  169. err := os.Remove(temp.Name())
  170. if err != nil {
  171. return
  172. }
  173. }()
  174. cfg.OutputPaths = []string{temp.Name()}
  175. cfg.EncoderConfig.TimeKey = "" // no timestamps in tests
  176. cfg.InitialFields = map[string]interface{}{"z": "zz", "k": "v"}
  177. logger, err := cfg.Build()
  178. require.NoError(t, err, "Unexpected error constructing logger.")
  179. logger.Debug("debug")
  180. logger.Info("info")
  181. logger.Warn("warn")
  182. byteContents, err := ioutil.ReadAll(temp)
  183. require.NoError(t, err, "Couldn't read log contents from temp file.")
  184. logs := string(byteContents)
  185. assert.Regexp(t, expectRe, logs, "Unexpected log output.")
  186. for i := 0; i < 200; i++ {
  187. logger.Info("sampling")
  188. }
  189. assert.Equal(t, int64(expectDropped), dcount.Load())
  190. assert.Equal(t, int64(expectSampled), scount.Load())
  191. }