2019-04-02 01:48:31 -06:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-04-02 01:48:31 -06:00
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-10-30 23:36:46 -06:00
|
|
|
func baseConsoleTest(t *testing.T, logger *MultiChannelledLogger) (chan []byte, chan bool) {
|
2019-04-02 01:48:31 -06:00
|
|
|
written := make(chan []byte)
|
|
|
|
closed := make(chan bool)
|
|
|
|
|
|
|
|
c := CallbackWriteCloser{
|
|
|
|
callback: func(p []byte, close bool) {
|
|
|
|
written <- p
|
|
|
|
closed <- close
|
|
|
|
},
|
|
|
|
}
|
|
|
|
m := logger.MultiChannelledLog
|
|
|
|
|
|
|
|
channelledLog := m.GetEventLogger("console")
|
|
|
|
assert.NotEmpty(t, channelledLog)
|
|
|
|
realChanLog, ok := channelledLog.(*ChannelledLog)
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.True(t, ok)
|
2019-04-02 01:48:31 -06:00
|
|
|
realCL, ok := realChanLog.loggerProvider.(*ConsoleLogger)
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.True(t, ok)
|
2019-04-02 01:48:31 -06:00
|
|
|
assert.Equal(t, INFO, realCL.Level)
|
|
|
|
realCL.out = c
|
|
|
|
|
|
|
|
format := "test: %s"
|
|
|
|
args := []interface{}{"A"}
|
|
|
|
|
|
|
|
logger.Log(0, INFO, format, args...)
|
|
|
|
line := <-written
|
|
|
|
assert.Contains(t, string(line), fmt.Sprintf(format, args...))
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.False(t, <-closed)
|
2019-04-02 01:48:31 -06:00
|
|
|
|
|
|
|
format = "test2: %s"
|
|
|
|
logger.Warn(format, args...)
|
|
|
|
line = <-written
|
|
|
|
|
|
|
|
assert.Contains(t, string(line), fmt.Sprintf(format, args...))
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.False(t, <-closed)
|
2019-04-02 01:48:31 -06:00
|
|
|
|
|
|
|
format = "testerror: %s"
|
|
|
|
logger.Error(format, args...)
|
|
|
|
line = <-written
|
|
|
|
assert.Contains(t, string(line), fmt.Sprintf(format, args...))
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.False(t, <-closed)
|
2019-04-02 01:48:31 -06:00
|
|
|
return written, closed
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewLoggerUnexported(t *testing.T) {
|
|
|
|
level := INFO
|
|
|
|
logger := newLogger("UNEXPORTED", 0)
|
|
|
|
err := logger.SetLogger("console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
out := logger.MultiChannelledLog.GetEventLogger("console")
|
|
|
|
assert.NotEmpty(t, out)
|
|
|
|
chanlog, ok := out.(*ChannelledLog)
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.True(t, ok)
|
2019-04-02 01:48:31 -06:00
|
|
|
assert.Equal(t, "console", chanlog.provider)
|
|
|
|
assert.Equal(t, INFO, logger.GetLevel())
|
|
|
|
baseConsoleTest(t, logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewLoggger(t *testing.T) {
|
|
|
|
level := INFO
|
|
|
|
logger := NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()))
|
|
|
|
|
|
|
|
assert.Equal(t, INFO, GetLevel())
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.False(t, IsTrace())
|
|
|
|
assert.False(t, IsDebug())
|
|
|
|
assert.True(t, IsInfo())
|
|
|
|
assert.True(t, IsWarn())
|
|
|
|
assert.True(t, IsError())
|
2019-04-02 01:48:31 -06:00
|
|
|
|
|
|
|
written, closed := baseConsoleTest(t, logger)
|
|
|
|
|
|
|
|
format := "test: %s"
|
|
|
|
args := []interface{}{"A"}
|
|
|
|
|
|
|
|
Log(0, INFO, format, args...)
|
|
|
|
line := <-written
|
|
|
|
assert.Contains(t, string(line), fmt.Sprintf(format, args...))
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.False(t, <-closed)
|
2019-04-02 01:48:31 -06:00
|
|
|
|
|
|
|
Info(format, args...)
|
|
|
|
line = <-written
|
|
|
|
assert.Contains(t, string(line), fmt.Sprintf(format, args...))
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.False(t, <-closed)
|
2019-04-02 01:48:31 -06:00
|
|
|
|
|
|
|
go DelLogger("console")
|
|
|
|
line = <-written
|
|
|
|
assert.Equal(t, "", string(line))
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.True(t, <-closed)
|
2019-04-02 01:48:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewLogggerRecreate(t *testing.T) {
|
|
|
|
level := INFO
|
|
|
|
NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()))
|
|
|
|
|
|
|
|
assert.Equal(t, INFO, GetLevel())
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.False(t, IsTrace())
|
|
|
|
assert.False(t, IsDebug())
|
|
|
|
assert.True(t, IsInfo())
|
|
|
|
assert.True(t, IsWarn())
|
|
|
|
assert.True(t, IsError())
|
2019-04-02 01:48:31 -06:00
|
|
|
|
|
|
|
format := "test: %s"
|
|
|
|
args := []interface{}{"A"}
|
|
|
|
|
|
|
|
Log(0, INFO, format, args...)
|
|
|
|
|
|
|
|
NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()))
|
|
|
|
|
|
|
|
assert.Equal(t, INFO, GetLevel())
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.False(t, IsTrace())
|
|
|
|
assert.False(t, IsDebug())
|
|
|
|
assert.True(t, IsInfo())
|
|
|
|
assert.True(t, IsWarn())
|
|
|
|
assert.True(t, IsError())
|
2019-04-02 01:48:31 -06:00
|
|
|
|
|
|
|
Log(0, INFO, format, args...)
|
|
|
|
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"`, level.String()))
|
|
|
|
})
|
|
|
|
|
|
|
|
go DelLogger("console")
|
|
|
|
|
|
|
|
// We should be able to redelete without a problem
|
|
|
|
go DelLogger("console")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewNamedLogger(t *testing.T) {
|
|
|
|
level := INFO
|
|
|
|
err := NewNamedLogger("test", 0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()))
|
|
|
|
assert.NoError(t, err)
|
2019-09-17 03:39:37 -06:00
|
|
|
logger, _ := NamedLoggers.Load("test")
|
2019-04-02 01:48:31 -06:00
|
|
|
assert.Equal(t, level, logger.GetLevel())
|
|
|
|
|
|
|
|
written, closed := baseConsoleTest(t, logger)
|
|
|
|
go DelNamedLogger("test")
|
|
|
|
line := <-written
|
|
|
|
assert.Equal(t, "", string(line))
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.True(t, <-closed)
|
2019-04-02 01:48:31 -06:00
|
|
|
}
|