github.com/adacta-ru/mattermost-server@v5.11.1+incompatible/mlog/global_test.go (about)

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