github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/log/console_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 "strings" 11 "testing" 12 "time" 13 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestConsoleLoggerBadConfig(t *testing.T) { 18 logger := NewConsoleLogger() 19 20 err := logger.Init("{") 21 assert.Error(t, err) 22 assert.Contains(t, err.Error(), "Unable to parse JSON") 23 logger.Close() 24 } 25 26 func TestConsoleLoggerMinimalConfig(t *testing.T) { 27 for _, level := range Levels() { 28 var written []byte 29 var closed bool 30 31 c := CallbackWriteCloser{ 32 callback: func(p []byte, close bool) { 33 written = p 34 closed = close 35 }, 36 } 37 prefix := "" 38 flags := LstdFlags 39 40 cw := NewConsoleLogger() 41 realCW := cw.(*ConsoleLogger) 42 cw.Init(fmt.Sprintf("{\"level\":\"%s\"}", level)) 43 nwc := realCW.out.(*nopWriteCloser) 44 nwc.w = c 45 46 assert.Equal(t, flags, realCW.Flags) 47 assert.Equal(t, FromString(level), realCW.Level) 48 assert.Equal(t, FromString(level), cw.GetLevel()) 49 assert.Equal(t, prefix, realCW.Prefix) 50 assert.Equal(t, "", string(written)) 51 cw.Close() 52 assert.False(t, closed) 53 54 } 55 } 56 57 func TestConsoleLogger(t *testing.T) { 58 var written []byte 59 var closed bool 60 61 c := CallbackWriteCloser{ 62 callback: func(p []byte, close bool) { 63 written = p 64 closed = close 65 }, 66 } 67 prefix := "TestPrefix " 68 level := INFO 69 flags := LstdFlags | LUTC | Lfuncname 70 71 cw := NewConsoleLogger() 72 realCW := cw.(*ConsoleLogger) 73 realCW.Colorize = false 74 nwc := realCW.out.(*nopWriteCloser) 75 nwc.w = c 76 77 cw.Init(fmt.Sprintf("{\"expression\":\"FILENAME\",\"prefix\":\"%s\",\"level\":\"%s\",\"flags\":%d}", prefix, level.String(), flags)) 78 79 assert.Equal(t, flags, realCW.Flags) 80 assert.Equal(t, level, realCW.Level) 81 assert.Equal(t, level, cw.GetLevel()) 82 83 location, _ := time.LoadLocation("EST") 84 85 date := time.Date(2019, time.January, 13, 22, 3, 30, 15, location) 86 87 dateString := date.UTC().Format("2006/01/02 15:04:05") 88 89 event := Event{ 90 level: INFO, 91 msg: "TEST MSG", 92 caller: "CALLER", 93 filename: "FULL/FILENAME", 94 line: 1, 95 time: date, 96 } 97 98 expected := fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg) 99 cw.LogEvent(&event) 100 assert.Equal(t, expected, string(written)) 101 assert.False(t, closed) 102 written = written[:0] 103 104 event.level = DEBUG 105 expected = "" 106 cw.LogEvent(&event) 107 assert.Equal(t, expected, string(written)) 108 assert.False(t, closed) 109 110 event.level = TRACE 111 expected = "" 112 cw.LogEvent(&event) 113 assert.Equal(t, expected, string(written)) 114 assert.False(t, closed) 115 116 nonMatchEvent := Event{ 117 level: INFO, 118 msg: "TEST MSG", 119 caller: "CALLER", 120 filename: "FULL/FI_LENAME", 121 line: 1, 122 time: date, 123 } 124 event.level = INFO 125 expected = "" 126 cw.LogEvent(&nonMatchEvent) 127 assert.Equal(t, expected, string(written)) 128 assert.False(t, closed) 129 130 event.level = WARN 131 expected = fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg) 132 cw.LogEvent(&event) 133 assert.Equal(t, expected, string(written)) 134 assert.False(t, closed) 135 written = written[:0] 136 137 cw.Close() 138 assert.False(t, closed) 139 }