increase_level_test.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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) observer.LoggedEntry {
  29. return observer.LoggedEntry{
  30. Entry: zapcore.Entry{Level: level, Message: msg},
  31. Context: []zapcore.Field{},
  32. }
  33. }
  34. func TestIncreaseLevelTryDecrease(t *testing.T) {
  35. errorOut := &bytes.Buffer{}
  36. opts := []Option{
  37. ErrorOutput(zapcore.AddSync(errorOut)),
  38. }
  39. withLogger(t, WarnLevel, opts, func(logger *Logger, logs *observer.ObservedLogs) {
  40. logger.Warn("original warn log")
  41. debugLogger := logger.WithOptions(IncreaseLevel(DebugLevel))
  42. debugLogger.Debug("ignored debug log")
  43. debugLogger.Warn("increase level warn log")
  44. debugLogger.Error("increase level error log")
  45. assert.Equal(t, []observer.LoggedEntry{
  46. newLoggedEntry(WarnLevel, "original warn log"),
  47. newLoggedEntry(WarnLevel, "increase level warn log"),
  48. newLoggedEntry(ErrorLevel, "increase level error log"),
  49. }, logs.AllUntimed(), "unexpected logs")
  50. assert.Equal(t,
  51. `failed to IncreaseLevel: invalid increase level, as level "info" is allowed by increased level, but not by existing core`,
  52. errorOut.String(),
  53. "unexpected error output",
  54. )
  55. })
  56. }
  57. func TestIncreaseLevel(t *testing.T) {
  58. errorOut := &bytes.Buffer{}
  59. opts := []Option{
  60. ErrorOutput(zapcore.AddSync(errorOut)),
  61. }
  62. withLogger(t, WarnLevel, opts, func(logger *Logger, logs *observer.ObservedLogs) {
  63. logger.Warn("original warn log")
  64. errorLogger := logger.WithOptions(IncreaseLevel(ErrorLevel))
  65. errorLogger.Debug("ignored debug log")
  66. errorLogger.Warn("ignored warn log")
  67. errorLogger.Error("increase level error log")
  68. assert.Equal(t, []observer.LoggedEntry{
  69. newLoggedEntry(WarnLevel, "original warn log"),
  70. newLoggedEntry(ErrorLevel, "increase level error log"),
  71. }, logs.AllUntimed(), "unexpected logs")
  72. assert.Empty(t, errorOut.String(), "expect no error output")
  73. })
  74. }