File: buflog_test.go

package info (click to toggle)
golang-github-alexcesaro-log 0.0~git20150915.61e6862-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 128 kB
  • sloc: makefile: 2
file content (49 lines) | stat: -rw-r--r-- 1,291 bytes parent folder | download | duplicates (2)
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
package buflog

import (
	"bytes"
	"testing"

	"github.com/alexcesaro/log"
	"github.com/alexcesaro/log/logtest"
)

const testMessage = "Test Message!"

func TestBufferedLogging(t *testing.T) {
	logger, out := createLogger(log.Info, log.Error)

	logger.Debug(logtest.Messages[log.Debug])
	logtest.AssertNotContain(t, out, logtest.Messages[log.Debug])

	logger.Info(logtest.Messages[log.Info])
	logtest.AssertNotContain(t, out, logtest.Messages[log.Info])

	logger.Error(logtest.Messages[log.Error])
	logtest.AssertNotContain(t, out, logtest.Messages[log.Debug])
	logtest.AssertContains(t, out, logtest.Messages[log.Info])
	logtest.AssertContains(t, out, logtest.Messages[log.Error])

	logger.Warning(logtest.Messages[log.Warning])
	logtest.AssertContains(t, out, logtest.Messages[log.Warning])

	logger.Alert(logtest.Messages[log.Alert])
	logtest.AssertContains(t, out, logtest.Messages[log.Alert])

	logtest.AssertLineCount(t, out, 4)
}

func BenchmarkBufferedLogging(b *testing.B) {
	logger, _ := createLogger(log.Info, log.Error)

	for i := 0; i < b.N; i++ {
		logger.Info(testMessage)
	}
	logger.Error(testMessage)
}

func createLogger(threshold log.Level, flushThreshold log.Level) (*Logger, *bytes.Buffer) {
	out := new(bytes.Buffer)

	return New(out, threshold, flushThreshold), out
}