2016-10-11 12:06:17 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-05-03 17:25:28 +00:00
|
|
|
"io"
|
2016-10-11 12:06:17 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2019-05-03 17:25:28 +00:00
|
|
|
"path/filepath"
|
2016-10-11 12:06:17 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-05-03 17:25:28 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-10-11 12:06:17 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-05-03 17:25:28 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-10-11 12:06:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWriteLogToFile(t *testing.T) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer func() { os.Remove(tmpfile.Name()) }()
|
|
|
|
|
2019-05-03 17:25:28 +00:00
|
|
|
config := createBasicLogConfig(tmpfile.Name())
|
|
|
|
SetupLogging(config)
|
2016-10-11 12:06:17 +00:00
|
|
|
log.Printf("I! TEST")
|
|
|
|
log.Printf("D! TEST") // <- should be ignored
|
|
|
|
|
|
|
|
f, err := ioutil.ReadFile(tmpfile.Name())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, f[19:], []byte("Z I! TEST\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDebugWriteLogToFile(t *testing.T) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer func() { os.Remove(tmpfile.Name()) }()
|
2019-05-03 17:25:28 +00:00
|
|
|
config := createBasicLogConfig(tmpfile.Name())
|
|
|
|
config.Debug = true
|
|
|
|
SetupLogging(config)
|
2016-10-11 12:06:17 +00:00
|
|
|
log.Printf("D! TEST")
|
|
|
|
|
|
|
|
f, err := ioutil.ReadFile(tmpfile.Name())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, f[19:], []byte("Z D! TEST\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorWriteLogToFile(t *testing.T) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer func() { os.Remove(tmpfile.Name()) }()
|
2019-05-03 17:25:28 +00:00
|
|
|
config := createBasicLogConfig(tmpfile.Name())
|
|
|
|
config.Quiet = true
|
|
|
|
SetupLogging(config)
|
2016-10-11 12:06:17 +00:00
|
|
|
log.Printf("E! TEST")
|
|
|
|
log.Printf("I! TEST") // <- should be ignored
|
|
|
|
|
|
|
|
f, err := ioutil.ReadFile(tmpfile.Name())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, f[19:], []byte("Z E! TEST\n"))
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:41:26 +00:00
|
|
|
func TestAddDefaultLogLevel(t *testing.T) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer func() { os.Remove(tmpfile.Name()) }()
|
2019-05-03 17:25:28 +00:00
|
|
|
config := createBasicLogConfig(tmpfile.Name())
|
|
|
|
config.Debug = true
|
|
|
|
SetupLogging(config)
|
2017-04-12 17:41:26 +00:00
|
|
|
log.Printf("TEST")
|
|
|
|
|
|
|
|
f, err := ioutil.ReadFile(tmpfile.Name())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, f[19:], []byte("Z I! TEST\n"))
|
|
|
|
}
|
|
|
|
|
2019-02-28 00:33:38 +00:00
|
|
|
func TestWriteToTruncatedFile(t *testing.T) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer func() { os.Remove(tmpfile.Name()) }()
|
2019-05-03 17:25:28 +00:00
|
|
|
config := createBasicLogConfig(tmpfile.Name())
|
|
|
|
config.Debug = true
|
|
|
|
SetupLogging(config)
|
2019-02-28 00:33:38 +00:00
|
|
|
log.Printf("TEST")
|
|
|
|
|
|
|
|
f, err := ioutil.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_TRUNC, 0644)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, tmpf.Close())
|
|
|
|
|
|
|
|
log.Printf("SHOULD BE FIRST")
|
|
|
|
|
|
|
|
f, err = ioutil.ReadFile(tmpfile.Name())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, f[19:], []byte("Z I! SHOULD BE FIRST\n"))
|
|
|
|
}
|
|
|
|
|
2019-05-03 17:25:28 +00:00
|
|
|
func TestWriteToFileInRotation(t *testing.T) {
|
|
|
|
tempDir, err := ioutil.TempDir("", "LogRotation")
|
|
|
|
require.NoError(t, err)
|
|
|
|
config := createBasicLogConfig(filepath.Join(tempDir, "test.log"))
|
|
|
|
config.RotationMaxSize = internal.Size{Size: int64(30)}
|
|
|
|
writer := newLogWriter(config)
|
|
|
|
// 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, _ := ioutil.ReadDir(tempDir)
|
|
|
|
assert.Equal(t, 2, len(files))
|
|
|
|
}
|
|
|
|
|
2016-10-11 12:06:17 +00:00
|
|
|
func BenchmarkTelegrafLogWrite(b *testing.B) {
|
|
|
|
var msg = []byte("test")
|
|
|
|
var buf bytes.Buffer
|
|
|
|
w := newTelegrafWriter(&buf)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
buf.Reset()
|
|
|
|
w.Write(msg)
|
|
|
|
}
|
|
|
|
}
|
2019-05-03 17:25:28 +00:00
|
|
|
|
|
|
|
func createBasicLogConfig(filename string) LogConfig {
|
|
|
|
return LogConfig{
|
|
|
|
Logfile: filename,
|
|
|
|
RotationMaxArchives: -1,
|
|
|
|
}
|
|
|
|
}
|