github.com/facebookincubator/ttpforge@v1.0.13-0.20240405153150-5ae801628835/pkg/logging/logger_test.go (about) 1 /* 2 Copyright © 2023-present, Meta Platforms, Inc. and affiliates 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 The above copyright notice and this permission notice shall be included in 10 all copies or substantial portions of the Software. 11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 12 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 14 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 15 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 16 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 17 THE SOFTWARE. 18 */ 19 20 package logging 21 22 import ( 23 "os" 24 "testing" 25 26 "github.com/stretchr/testify/assert" 27 "github.com/stretchr/testify/require" 28 "go.uber.org/zap" 29 "go.uber.org/zap/zapcore" 30 "go.uber.org/zap/zaptest/observer" 31 ) 32 33 // TestInitLogStacktraceNoLogfile verifies that the stack trace 34 // logging flag works correctly - it uses observer 35 // rather than a log file to test the "no log file" branch as well, 36 // which is why it is non-redundant with the file-based logging tests 37 // found later in this file 38 func TestInitLogStacktraceNoLogfile(t *testing.T) { 39 core, recordedLogs := observer.New(zapcore.InfoLevel) 40 41 err := InitLog(Config{ 42 Stacktrace: true, 43 }) 44 require.NoError(t, err) 45 46 logger := L().WithOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core { return core })) 47 48 logger.Error("should produce a stack trace") 49 50 entries := recordedLogs.All() 51 require.Len(t, entries, 1) 52 assert.Contains(t, entries[0].Stack, "logger_test.go", "stack trace should contain the test log file") 53 } 54 55 // TestInitLog verifies that the various logging flags work - 56 // it uses a log file for all cases because 57 // flags like Verbose are easier to check when the output 58 // is sent to a file rather than through observer 59 func TestInitLog(t *testing.T) { 60 61 tests := []struct { 62 name string 63 config Config 64 logFunc func(t *testing.T, testLogger *zap.SugaredLogger) 65 checkFunc func(t *testing.T, logFileContents string) 66 }{ 67 { 68 name: "verbose", 69 config: Config{ 70 Verbose: true, 71 }, 72 logFunc: func(t *testing.T, testLogger *zap.SugaredLogger) { 73 testLogger.Debug("hello, world") 74 }, 75 checkFunc: func(t *testing.T, logFileContents string) { 76 assert.Contains(t, logFileContents, "hello, world") 77 }, 78 }, 79 { 80 name: "no-color", 81 config: Config{ 82 NoColor: true, 83 }, 84 logFunc: func(t *testing.T, testLogger *zap.SugaredLogger) { 85 testLogger.Info("no color") 86 }, 87 checkFunc: func(t *testing.T, logFileContents string) { 88 // ANSI reset code 89 assert.NotContains(t, logFileContents, "\x1b[0m") 90 }, 91 }, 92 } 93 94 for _, tc := range tests { 95 t.Run(tc.name, func(t *testing.T) { 96 // use a temporary log file for each test case 97 tempFile, err := os.CreateTemp("", "logger_test") 98 require.NoError(t, err) 99 logFile := tempFile.Name() 100 defer os.Remove(logFile) 101 cfg := tc.config 102 cfg.LogFile = logFile 103 104 // initialize the logger for this test case 105 err = InitLog(cfg) 106 require.NoError(t, err) 107 108 // actually create logs 109 tc.logFunc(t, L()) 110 111 // read back result 112 content, err := os.ReadFile(logFile) 113 require.NoError(t, err) 114 tc.checkFunc(t, string(content)) 115 }) 116 } 117 }