summaryrefslogtreecommitdiffstats
path: root/modules/log/log_test.go
blob: 819cdb521fa59a070a08d00ab35f4e2649d61601 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package log

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
)

func baseConsoleTest(t *testing.T, logger *MultiChannelledLogger) (chan []byte, chan bool) {
	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)
	assert.True(t, ok)
	realCL, ok := realChanLog.loggerProvider.(*ConsoleLogger)
	assert.True(t, ok)
	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...))
	assert.False(t, <-closed)

	format = "test2: %s"
	logger.Warn(format, args...)
	line = <-written

	assert.Contains(t, string(line), fmt.Sprintf(format, args...))
	assert.False(t, <-closed)

	format = "testerror: %s"
	logger.Error(format, args...)
	line = <-written
	assert.Contains(t, string(line), fmt.Sprintf(format, args...))
	assert.False(t, <-closed)
	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)
	assert.True(t, ok)
	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())
	assert.False(t, IsTrace())
	assert.False(t, IsDebug())
	assert.True(t, IsInfo())
	assert.True(t, IsWarn())
	assert.True(t, IsError())

	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...))
	assert.False(t, <-closed)

	Info(format, args...)
	line = <-written
	assert.Contains(t, string(line), fmt.Sprintf(format, args...))
	assert.False(t, <-closed)

	go DelLogger("console")
	line = <-written
	assert.Equal(t, "", string(line))
	assert.True(t, <-closed)
}

func TestNewLogggerRecreate(t *testing.T) {
	level := INFO
	NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()))

	assert.Equal(t, INFO, GetLevel())
	assert.False(t, IsTrace())
	assert.False(t, IsDebug())
	assert.True(t, IsInfo())
	assert.True(t, IsWarn())
	assert.True(t, IsError())

	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())
	assert.False(t, IsTrace())
	assert.False(t, IsDebug())
	assert.True(t, IsInfo())
	assert.True(t, IsWarn())
	assert.True(t, IsError())

	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)
	logger, _ := NamedLoggers.Load("test")
	assert.Equal(t, level, logger.GetLevel())

	written, closed := baseConsoleTest(t, logger)
	go DelNamedLogger("test")
	line := <-written
	assert.Equal(t, "", string(line))
	assert.True(t, <-closed)
}