github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/log/log_test.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package log 7 8 import ( 9 "fmt" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func baseConsoleTest(t *testing.T, logger *MultiChannelledLogger) (chan []byte, chan bool) { 16 written := make(chan []byte) 17 closed := make(chan bool) 18 19 c := CallbackWriteCloser{ 20 callback: func(p []byte, close bool) { 21 written <- p 22 closed <- close 23 }, 24 } 25 m := logger.MultiChannelledLog 26 27 channelledLog := m.GetEventLogger("console") 28 assert.NotEmpty(t, channelledLog) 29 realChanLog, ok := channelledLog.(*ChannelledLog) 30 assert.True(t, ok) 31 realCL, ok := realChanLog.loggerProvider.(*ConsoleLogger) 32 assert.True(t, ok) 33 assert.Equal(t, INFO, realCL.Level) 34 realCL.out = c 35 36 format := "test: %s" 37 args := []interface{}{"A"} 38 39 logger.Log(0, INFO, format, args...) 40 line := <-written 41 assert.Contains(t, string(line), fmt.Sprintf(format, args...)) 42 assert.False(t, <-closed) 43 44 format = "test2: %s" 45 logger.Warn(format, args...) 46 line = <-written 47 48 assert.Contains(t, string(line), fmt.Sprintf(format, args...)) 49 assert.False(t, <-closed) 50 51 format = "testerror: %s" 52 logger.Error(format, args...) 53 line = <-written 54 assert.Contains(t, string(line), fmt.Sprintf(format, args...)) 55 assert.False(t, <-closed) 56 fmt.Println(string(line)) 57 return written, closed 58 } 59 60 func TestNewLoggerUnexported(t *testing.T) { 61 level := INFO 62 logger := newLogger("UNEXPORTED", 0, true) 63 err := logger.SetLogger("console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String())) 64 assert.NoError(t, err) 65 out := logger.MultiChannelledLog.GetEventLogger("console") 66 assert.NotEmpty(t, out) 67 chanlog, ok := out.(*ChannelledLog) 68 assert.True(t, ok) 69 assert.Equal(t, "console", chanlog.provider) 70 assert.Equal(t, INFO, logger.GetLevel()) 71 baseConsoleTest(t, logger) 72 } 73 74 func TestNewLoggger(t *testing.T) { 75 level := INFO 76 logger := NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()), false) 77 78 assert.Equal(t, INFO, GetLevel()) 79 assert.False(t, IsTrace()) 80 assert.False(t, IsDebug()) 81 assert.True(t, IsInfo()) 82 assert.True(t, IsWarn()) 83 assert.True(t, IsError()) 84 85 written, closed := baseConsoleTest(t, logger) 86 87 format := "test: %s" 88 args := []interface{}{"A"} 89 90 Log(0, INFO, format, args...) 91 line := <-written 92 assert.Contains(t, string(line), fmt.Sprintf(format, args...)) 93 assert.False(t, <-closed) 94 95 Info(format, args...) 96 line = <-written 97 assert.Contains(t, string(line), fmt.Sprintf(format, args...)) 98 assert.False(t, <-closed) 99 100 go DelLogger("console") 101 line = <-written 102 assert.Equal(t, "", string(line)) 103 assert.True(t, <-closed) 104 } 105 106 func TestNewLogggerRecreate(t *testing.T) { 107 level := INFO 108 NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()), false) 109 110 assert.Equal(t, INFO, GetLevel()) 111 assert.False(t, IsTrace()) 112 assert.False(t, IsDebug()) 113 assert.True(t, IsInfo()) 114 assert.True(t, IsWarn()) 115 assert.True(t, IsError()) 116 117 format := "test: %s" 118 args := []interface{}{"A"} 119 120 Log(0, INFO, format, args...) 121 122 NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()), false) 123 124 assert.Equal(t, INFO, GetLevel()) 125 assert.False(t, IsTrace()) 126 assert.False(t, IsDebug()) 127 assert.True(t, IsInfo()) 128 assert.True(t, IsWarn()) 129 assert.True(t, IsError()) 130 131 Log(0, INFO, format, args...) 132 133 assert.Panics(t, func() { 134 NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"`, level.String()), false) 135 }) 136 137 go DelLogger("console") 138 139 // We should be able to redelete without a problem 140 go DelLogger("console") 141 } 142 143 func TestNewNamedLogger(t *testing.T) { 144 level := INFO 145 err := NewNamedLogger("test", 0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()), false) 146 assert.NoError(t, err) 147 logger, _ := NamedLoggers.Load("test") 148 assert.Equal(t, level, logger.GetLevel()) 149 150 written, closed := baseConsoleTest(t, logger) 151 go DelNamedLogger("test") 152 line := <-written 153 assert.Equal(t, "", string(line)) 154 assert.True(t, <-closed) 155 }