github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/common/flogging/loggerlevels_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package flogging_test
     8  
     9  import (
    10  	"errors"
    11  	"strconv"
    12  	"testing"
    13  
    14  	"github.com/hyperledger/fabric/common/flogging"
    15  	"github.com/stretchr/testify/assert"
    16  	"go.uber.org/zap/zapcore"
    17  )
    18  
    19  func TestLoggerLevelsActivateSpec(t *testing.T) {
    20  	var tests = []struct {
    21  		spec                 string
    22  		expectedLevels       map[string]zapcore.Level
    23  		expectedDefaultLevel zapcore.Level
    24  	}{
    25  		{
    26  			spec:                 "DEBUG",
    27  			expectedLevels:       map[string]zapcore.Level{},
    28  			expectedDefaultLevel: zapcore.DebugLevel,
    29  		},
    30  		{
    31  			spec:                 "INFO",
    32  			expectedLevels:       map[string]zapcore.Level{},
    33  			expectedDefaultLevel: zapcore.InfoLevel,
    34  		},
    35  		{
    36  			spec: "logger=info:DEBUG",
    37  			expectedLevels: map[string]zapcore.Level{
    38  				"logger":     zapcore.InfoLevel,
    39  				"logger.a":   zapcore.InfoLevel,
    40  				"logger.b":   zapcore.InfoLevel,
    41  				"logger.a.b": zapcore.InfoLevel,
    42  			},
    43  			expectedDefaultLevel: zapcore.DebugLevel,
    44  		},
    45  		{
    46  			spec: "logger=info:logger.=error:DEBUG",
    47  			expectedLevels: map[string]zapcore.Level{
    48  				"logger":     zapcore.ErrorLevel,
    49  				"logger.a":   zapcore.InfoLevel,
    50  				"logger.b":   zapcore.InfoLevel,
    51  				"logger.a.b": zapcore.InfoLevel,
    52  			},
    53  			expectedDefaultLevel: zapcore.DebugLevel,
    54  		},
    55  		{
    56  			spec: "logger.a,logger.b=info:logger.c=WARN:DEBUG",
    57  			expectedLevels: map[string]zapcore.Level{
    58  				"logger.a": zapcore.InfoLevel,
    59  				"logger.b": zapcore.InfoLevel,
    60  				"logger.c": zapcore.WarnLevel,
    61  			},
    62  			expectedDefaultLevel: zapcore.DebugLevel,
    63  		},
    64  		{
    65  			spec: "a.b=info:a,z=error:c.b=info:c.=warn:debug",
    66  			expectedLevels: map[string]zapcore.Level{
    67  				"a":       zapcore.ErrorLevel,
    68  				"z":       zapcore.ErrorLevel,
    69  				"a.b":     zapcore.InfoLevel,
    70  				"a.b.c":   zapcore.InfoLevel,
    71  				"a.b.c.d": zapcore.InfoLevel,
    72  				"a.c":     zapcore.ErrorLevel,
    73  				"c":       zapcore.WarnLevel,
    74  				"c.a":     zapcore.DebugLevel,
    75  				"c.b":     zapcore.InfoLevel,
    76  				"d":       zapcore.DebugLevel,
    77  				"ab.c":    zapcore.DebugLevel,
    78  				"c.b.a":   zapcore.InfoLevel,
    79  				"c.b.a.b": zapcore.InfoLevel,
    80  			},
    81  			expectedDefaultLevel: zapcore.DebugLevel,
    82  		},
    83  		{
    84  			spec: "info:warn",
    85  			expectedLevels: map[string]zapcore.Level{
    86  				"a":   zapcore.WarnLevel,
    87  				"a.b": zapcore.WarnLevel,
    88  				"b":   zapcore.WarnLevel,
    89  				"c":   zapcore.WarnLevel,
    90  				"d":   zapcore.WarnLevel,
    91  			},
    92  			expectedDefaultLevel: zapcore.WarnLevel,
    93  		},
    94  	}
    95  
    96  	for _, tc := range tests {
    97  		t.Run(tc.spec, func(t *testing.T) {
    98  			ll := &flogging.LoggerLevels{}
    99  
   100  			err := ll.ActivateSpec(tc.spec)
   101  			assert.NoError(t, err)
   102  			assert.Equal(t, tc.expectedDefaultLevel, ll.DefaultLevel())
   103  			for name, lvl := range tc.expectedLevels {
   104  				assert.Equal(t, lvl, ll.Level(name))
   105  			}
   106  		})
   107  	}
   108  }
   109  
   110  func TestLoggerLevelsActivateSpecErrors(t *testing.T) {
   111  	var tests = []struct {
   112  		spec string
   113  		err  error
   114  	}{
   115  		{spec: "=INFO:DEBUG", err: errors.New("invalid logging specification '=INFO:DEBUG': no logger specified in segment '=INFO'")},
   116  		{spec: "=INFO=:DEBUG", err: errors.New("invalid logging specification '=INFO=:DEBUG': bad segment '=INFO='")},
   117  		{spec: "bogus", err: errors.New("invalid logging specification 'bogus': bad segment 'bogus'")},
   118  		{spec: "a.b=info:a=broken:c.b=info:c.=warn:debug", err: errors.New("invalid logging specification 'a.b=info:a=broken:c.b=info:c.=warn:debug': bad segment 'a=broken'")},
   119  		{spec: "a*=info:debug", err: errors.New("invalid logging specification 'a*=info:debug': bad logger name 'a*'")},
   120  		{spec: ".a=info:debug", err: errors.New("invalid logging specification '.a=info:debug': bad logger name '.a'")},
   121  	}
   122  	for _, tc := range tests {
   123  		t.Run(tc.spec, func(t *testing.T) {
   124  			ll := &flogging.LoggerLevels{}
   125  			err := ll.ActivateSpec("fatal:a=warn")
   126  			assert.Nil(t, err)
   127  
   128  			err = ll.ActivateSpec(tc.spec)
   129  			assert.EqualError(t, err, tc.err.Error())
   130  
   131  			assert.Equal(t, zapcore.FatalLevel, ll.DefaultLevel(), "default should not change")
   132  			assert.Equal(t, zapcore.WarnLevel, ll.Level("a.b"), "log levels should not change")
   133  		})
   134  	}
   135  }
   136  
   137  func TestSpec(t *testing.T) {
   138  	var tests = []struct {
   139  		input  string
   140  		output string
   141  	}{
   142  		{input: "", output: "info"},
   143  		{input: "debug", output: "debug"},
   144  		{input: "a.=info:warning", output: "a.=info:warn"},
   145  		{input: "a-b=error", output: "a-b=error:info"},
   146  		{input: "a#b=error", output: "a#b=error:info"},
   147  		{input: "a_b=error", output: "a_b=error:info"},
   148  		{input: "debug:a=info:b=warn", output: "a=info:b=warn:debug"},
   149  		{input: "b=warn:a=error", output: "a=error:b=warn:info"},
   150  	}
   151  
   152  	for _, tc := range tests {
   153  		ll := &flogging.LoggerLevels{}
   154  		err := ll.ActivateSpec(tc.input)
   155  		assert.NoError(t, err)
   156  
   157  		assert.Equal(t, tc.output, ll.Spec())
   158  	}
   159  }
   160  
   161  func TestEnabled(t *testing.T) {
   162  	var tests = []struct {
   163  		spec      string
   164  		enabledAt zapcore.Level
   165  	}{
   166  		{spec: "payload", enabledAt: flogging.PayloadLevel},
   167  		{spec: "debug", enabledAt: zapcore.DebugLevel},
   168  		{spec: "info", enabledAt: zapcore.InfoLevel},
   169  		{spec: "warn", enabledAt: zapcore.WarnLevel},
   170  		{spec: "panic", enabledAt: zapcore.PanicLevel},
   171  		{spec: "fatal", enabledAt: zapcore.FatalLevel},
   172  		{spec: "fatal:a=debug", enabledAt: zapcore.DebugLevel},
   173  		{spec: "a=fatal:b=warn", enabledAt: zapcore.InfoLevel},
   174  		{spec: "a=warn", enabledAt: zapcore.InfoLevel},
   175  		{spec: "a=debug", enabledAt: zapcore.DebugLevel},
   176  	}
   177  
   178  	for i, tc := range tests {
   179  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   180  			ll := &flogging.LoggerLevels{}
   181  			err := ll.ActivateSpec(tc.spec)
   182  			assert.NoError(t, err)
   183  
   184  			for i := flogging.PayloadLevel; i <= zapcore.FatalLevel; i++ {
   185  				if tc.enabledAt <= i {
   186  					assert.Truef(t, ll.Enabled(i), "expected level %s and spec %s to be enabled", zapcore.Level(i), tc.spec)
   187  				} else {
   188  					assert.False(t, ll.Enabled(i), "expected level %s and spec %s to be disabled", zapcore.Level(i), tc.spec)
   189  				}
   190  			}
   191  		})
   192  	}
   193  }