-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathlogger_test.go
155 lines (133 loc) · 4.02 KB
/
logger_test.go
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
153
154
155
package logger
import (
"bytes"
"io"
"log"
"os"
"path/filepath"
"testing"
"github.com/influxdata/telegraf/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWriteLogToFile(t *testing.T) {
tmpfile, err := os.CreateTemp("", "")
assert.NoError(t, err)
defer func() { os.Remove(tmpfile.Name()) }()
config := createBasicLogConfig(tmpfile.Name())
SetupLogging(config)
log.Printf("I! TEST")
log.Printf("D! TEST") // <- should be ignored
f, err := os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
assert.Equal(t, f[19:], []byte("Z I! TEST\n"))
}
func TestDebugWriteLogToFile(t *testing.T) {
tmpfile, err := os.CreateTemp("", "")
assert.NoError(t, err)
defer func() { os.Remove(tmpfile.Name()) }()
config := createBasicLogConfig(tmpfile.Name())
config.Debug = true
SetupLogging(config)
log.Printf("D! TEST")
f, err := os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
assert.Equal(t, f[19:], []byte("Z D! TEST\n"))
}
func TestErrorWriteLogToFile(t *testing.T) {
tmpfile, err := os.CreateTemp("", "")
assert.NoError(t, err)
defer func() { os.Remove(tmpfile.Name()) }()
config := createBasicLogConfig(tmpfile.Name())
config.Quiet = true
SetupLogging(config)
log.Printf("E! TEST")
log.Printf("I! TEST") // <- should be ignored
f, err := os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
assert.Equal(t, f[19:], []byte("Z E! TEST\n"))
}
func TestAddDefaultLogLevel(t *testing.T) {
tmpfile, err := os.CreateTemp("", "")
assert.NoError(t, err)
defer func() { os.Remove(tmpfile.Name()) }()
config := createBasicLogConfig(tmpfile.Name())
config.Debug = true
SetupLogging(config)
log.Printf("TEST")
f, err := os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
assert.Equal(t, f[19:], []byte("Z I! TEST\n"))
}
func TestWriteToTruncatedFile(t *testing.T) {
tmpfile, err := os.CreateTemp("", "")
assert.NoError(t, err)
defer func() { os.Remove(tmpfile.Name()) }()
config := createBasicLogConfig(tmpfile.Name())
config.Debug = true
SetupLogging(config)
log.Printf("TEST")
f, err := os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
assert.Equal(t, f[19:], []byte("Z I! TEST\n"))
tmpf, err := os.OpenFile(tmpfile.Name(), os.O_RDWR|os.O_TRUNC, 0644)
assert.NoError(t, err)
assert.NoError(t, tmpf.Close())
log.Printf("SHOULD BE FIRST")
f, err = os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
assert.Equal(t, f[19:], []byte("Z I! SHOULD BE FIRST\n"))
}
func TestWriteToFileInRotation(t *testing.T) {
tempDir, err := os.MkdirTemp("", "LogRotation")
require.NoError(t, err)
cfg := createBasicLogConfig(filepath.Join(tempDir, "test.log"))
cfg.LogTarget = LogTargetFile
cfg.RotationMaxSize = config.Size(30)
writer := newLogWriter(cfg)
// Close the writer here, otherwise the temp folder cannot be deleted because the current log file is in use.
closer, isCloser := writer.(io.Closer)
assert.True(t, isCloser)
defer func() { closer.Close(); os.RemoveAll(tempDir) }()
log.Printf("I! TEST 1") // Writes 31 bytes, will rotate
log.Printf("I! TEST") // Writes 29 byes, no rotation expected
files, _ := os.ReadDir(tempDir)
assert.Equal(t, 2, len(files))
}
func TestLogTargetSettings(t *testing.T) {
config := LogConfig{
LogTarget: "",
Quiet: true,
}
SetupLogging(config)
logger, isTelegrafLogger := actualLogger.(*telegrafLog)
assert.True(t, isTelegrafLogger)
assert.Equal(t, logger.internalWriter, os.Stderr)
config = LogConfig{
LogTarget: "stderr",
Quiet: true,
}
SetupLogging(config)
logger, isTelegrafLogger = actualLogger.(*telegrafLog)
assert.True(t, isTelegrafLogger)
assert.Equal(t, logger.internalWriter, os.Stderr)
}
func BenchmarkTelegrafLogWrite(b *testing.B) {
var msg = []byte("test")
var buf bytes.Buffer
w, err := newTelegrafWriter(&buf, LogConfig{})
if err != nil {
panic("Unable to create log writer.")
}
for i := 0; i < b.N; i++ {
buf.Reset()
w.Write(msg)
}
}
func createBasicLogConfig(filename string) LogConfig {
return LogConfig{
Logfile: filename,
LogTarget: LogTargetFile,
RotationMaxArchives: -1,
}
}