increase_level_test.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (c) 2020 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. "bytes"
  23. "testing"
  24. "github.com/stretchr/testify/assert"
  25. "go.uber.org/zap/zapcore"
  26. "go.uber.org/zap/zaptest/observer"
  27. )
  28. func newLoggedEntry(level zapcore.Level, msg string, fields ...zapcore.Field) observer.LoggedEntry {
  29. if len(fields) == 0 {
  30. fields = []zapcore.Field{}
  31. }
  32. return observer.LoggedEntry{
  33. Entry: zapcore.Entry{Level: level, Message: msg},
  34. Context: fields,
  35. }
  36. }
  37. func TestIncreaseLevelTryDecrease(t *testing.T) {
  38. errorOut := &bytes.Buffer{}
  39. opts := []Option{
  40. ErrorOutput(zapcore.AddSync(errorOut)),
  41. }
  42. withLogger(t, WarnLevel, opts, func(logger *Logger, logs *observer.ObservedLogs) {
  43. logger.Warn("original warn log")
  44. debugLogger := logger.WithOptions(IncreaseLevel(DebugLevel))
  45. debugLogger.Debug("ignored debug log")
  46. debugLogger.Warn("increase level warn log")
  47. debugLogger.Error("increase level error log")
  48. assert.Equal(t, []observer.LoggedEntry{
  49. newLoggedEntry(WarnLevel, "original warn log"),
  50. newLoggedEntry(WarnLevel, "increase level warn log"),
  51. newLoggedEntry(ErrorLevel, "increase level error log"),
  52. }, logs.AllUntimed(), "unexpected logs")
  53. assert.Equal(t,
  54. `failed to IncreaseLevel: invalid increase level, as level "info" is allowed by increased level, but not by existing core`,
  55. errorOut.String(),
  56. "unexpected error output",
  57. )
  58. })
  59. }
  60. func TestIncreaseLevel(t *testing.T) {
  61. errorOut := &bytes.Buffer{}
  62. opts := []Option{
  63. ErrorOutput(zapcore.AddSync(errorOut)),
  64. }
  65. withLogger(t, WarnLevel, opts, func(logger *Logger, logs *observer.ObservedLogs) {
  66. logger.Warn("original warn log")
  67. errorLogger := logger.WithOptions(IncreaseLevel(ErrorLevel))
  68. errorLogger.Debug("ignored debug log")
  69. errorLogger.Warn("ignored warn log")
  70. errorLogger.Error("increase level error log")
  71. withFields := errorLogger.With(String("k", "v"))
  72. withFields.Debug("ignored debug log with fields")
  73. withFields.Warn("ignored warn log with fields")
  74. withFields.Error("increase level error log with fields")
  75. assert.Equal(t, []observer.LoggedEntry{
  76. newLoggedEntry(WarnLevel, "original warn log"),
  77. newLoggedEntry(ErrorLevel, "increase level error log"),
  78. newLoggedEntry(ErrorLevel, "increase level error log with fields", String("k", "v")),
  79. }, logs.AllUntimed(), "unexpected logs")
  80. assert.Empty(t, errorOut.String(), "expect no error output")
  81. })
  82. }