forked from aws/amazon-cloudwatch-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lumberjack_const_test.go
105 lines (87 loc) · 2.73 KB
/
lumberjack_const_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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package logger
import (
"log"
"os"
"testing"
"github.com/influxdata/telegraf/logger"
"github.com/stretchr/testify/assert"
)
func TestWriteLogToFile(t *testing.T) {
tmpfile, err := os.CreateTemp("", "")
assert.NoError(t, err)
defer func() { os.Remove(tmpfile.Name()) }()
config := createBasicLogConfig(tmpfile.Name())
logger.SetupLogging(config)
log.Printf("I! TEST")
log.Printf("D! TEST") // <- should be ignored
f, err := os.ReadFile(tmpfile.Name())
log.Printf("log: %v\n", string(f))
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
logger.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
logger.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
logger.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
logger.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 createBasicLogConfig(filename string) logger.LogConfig {
return logger.LogConfig{
Logfile: filename,
LogTarget: LogTargetLumberjack,
RotationMaxArchives: -1,
LogWithTimezone: "UTC",
}
}