github.com/adacta-ru/mattermost-server/v6@v6.0.0/mlog/global_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package mlog_test 5 6 import ( 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 "regexp" 11 "strings" 12 "testing" 13 14 "github.com/stretchr/testify/require" 15 16 "github.com/adacta-ru/mattermost-server/v6/mlog" 17 ) 18 19 func TestLoggingBeforeInitialized(t *testing.T) { 20 require.NotPanics(t, func() { 21 // None of these should segfault before mlog is globally configured 22 mlog.Info("info log") 23 mlog.Debug("debug log") 24 mlog.Warn("warning log") 25 mlog.Error("error log") 26 mlog.Critical("critical log") 27 }) 28 } 29 30 func TestLoggingAfterInitialized(t *testing.T) { 31 testCases := []struct { 32 Description string 33 LoggerConfiguration *mlog.LoggerConfiguration 34 ExpectedLogs []string 35 }{ 36 { 37 "file logging, json, debug", 38 &mlog.LoggerConfiguration{ 39 EnableConsole: false, 40 EnableFile: true, 41 FileJson: true, 42 FileLevel: mlog.LevelDebug, 43 }, 44 []string{ 45 `{"level":"debug","ts":0,"caller":"mlog/global_test.go:0","msg":"real debug log"}`, 46 `{"level":"info","ts":0,"caller":"mlog/global_test.go:0","msg":"real info log"}`, 47 `{"level":"warn","ts":0,"caller":"mlog/global_test.go:0","msg":"real warning log"}`, 48 `{"level":"error","ts":0,"caller":"mlog/global_test.go:0","msg":"real error log"}`, 49 `{"level":"error","ts":0,"caller":"mlog/global_test.go:0","msg":"real critical log"}`, 50 }, 51 }, 52 { 53 "file logging, json, error", 54 &mlog.LoggerConfiguration{ 55 EnableConsole: false, 56 EnableFile: true, 57 FileJson: true, 58 FileLevel: mlog.LevelError, 59 }, 60 []string{ 61 `{"level":"error","ts":0,"caller":"mlog/global_test.go:0","msg":"real error log"}`, 62 `{"level":"error","ts":0,"caller":"mlog/global_test.go:0","msg":"real critical log"}`, 63 }, 64 }, 65 { 66 "file logging, non-json, debug", 67 &mlog.LoggerConfiguration{ 68 EnableConsole: false, 69 EnableFile: true, 70 FileJson: false, 71 FileLevel: mlog.LevelDebug, 72 }, 73 []string{ 74 `TIME debug mlog/global_test.go:0 real debug log`, 75 `TIME info mlog/global_test.go:0 real info log`, 76 `TIME warn mlog/global_test.go:0 real warning log`, 77 `TIME error mlog/global_test.go:0 real error log`, 78 `TIME error mlog/global_test.go:0 real critical log`, 79 }, 80 }, 81 { 82 "file logging, non-json, error", 83 &mlog.LoggerConfiguration{ 84 EnableConsole: false, 85 EnableFile: true, 86 FileJson: false, 87 FileLevel: mlog.LevelError, 88 }, 89 []string{ 90 `TIME error mlog/global_test.go:0 real error log`, 91 `TIME error mlog/global_test.go:0 real critical log`, 92 }, 93 }, 94 } 95 96 for _, testCase := range testCases { 97 t.Run(testCase.Description, func(t *testing.T) { 98 var filePath string 99 if testCase.LoggerConfiguration.EnableFile { 100 tempDir, err := ioutil.TempDir(os.TempDir(), "TestLoggingAfterInitialized") 101 require.NoError(t, err) 102 defer os.Remove(tempDir) 103 104 filePath = filepath.Join(tempDir, "file.log") 105 testCase.LoggerConfiguration.FileLocation = filePath 106 } 107 108 logger := mlog.NewLogger(testCase.LoggerConfiguration) 109 mlog.InitGlobalLogger(logger) 110 111 mlog.Debug("real debug log") 112 mlog.Info("real info log") 113 mlog.Warn("real warning log") 114 mlog.Error("real error log") 115 mlog.Critical("real critical log") 116 117 if testCase.LoggerConfiguration.EnableFile { 118 logs, err := ioutil.ReadFile(filePath) 119 require.NoError(t, err) 120 121 actual := strings.TrimSpace(string(logs)) 122 123 if testCase.LoggerConfiguration.FileJson { 124 reTs := regexp.MustCompile(`"ts":[0-9\.]+`) 125 reCaller := regexp.MustCompile(`"caller":"([^"]+):[0-9\.]+"`) 126 actual = reTs.ReplaceAllString(actual, `"ts":0`) 127 actual = reCaller.ReplaceAllString(actual, `"caller":"$1:0"`) 128 } else { 129 actualRows := strings.Split(actual, "\n") 130 for i, actualRow := range actualRows { 131 actualFields := strings.Split(actualRow, "\t") 132 if len(actualFields) > 3 { 133 actualFields[0] = "TIME" 134 reCaller := regexp.MustCompile(`([^"]+):[0-9\.]+`) 135 actualFields[2] = reCaller.ReplaceAllString(actualFields[2], "$1:0") 136 actualRows[i] = strings.Join(actualFields, "\t") 137 } 138 } 139 140 actual = strings.Join(actualRows, "\n") 141 } 142 require.ElementsMatch(t, testCase.ExpectedLogs, strings.Split(actual, "\n")) 143 } 144 }) 145 } 146 }