forked from SigNoz/flog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flog.go
145 lines (128 loc) · 3.33 KB
/
flog.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
package main
import (
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// Generate generates the logs with given options
func Generate(option *Option) error {
var (
splitCount = 1
created = time.Now()
interval time.Duration
delay time.Duration
)
if option.Delay > 0 {
interval = option.Delay
delay = interval
}
if option.Sleep > 0 {
interval = option.Sleep
}
logFileName := option.Output
writer, err := NewWriter(option.Type, logFileName)
if err != nil {
return err
}
if option.Forever {
for {
time.Sleep(delay)
log := NewLog(option.Format, option.LogLineBytes, created)
_, _ = writer.Write([]byte(log + "\n"))
created = created.Add(interval)
}
}
if option.Bytes == 0 {
// Generates the logs until the certain number of lines is reached
for line := 0; line < option.Number; line++ {
time.Sleep(delay)
log := NewLog(option.Format, option.LogLineBytes, created)
_, _ = writer.Write([]byte(log + "\n"))
if (option.Type != "stdout") && (option.SplitBy > 0) && (line > option.SplitBy*splitCount) {
_ = writer.Close()
fmt.Println(logFileName, "is created.")
logFileName = NewSplitFileName(option.Output, splitCount)
writer, _ = NewWriter(option.Type, logFileName)
splitCount++
}
created = created.Add(interval)
}
} else {
// Generates the logs until the certain size in bytes is reached
bytes := 0
for bytes < option.Bytes {
time.Sleep(delay)
log := NewLog(option.Format, option.LogLineBytes, created)
_, _ = writer.Write([]byte(log + "\n"))
bytes += len(log)
if (option.Type != "stdout") && (option.SplitBy > 0) && (bytes > option.SplitBy*splitCount+1) {
_ = writer.Close()
fmt.Println(logFileName, "is created.")
logFileName = NewSplitFileName(option.Output, splitCount)
writer, _ = NewWriter(option.Type, logFileName)
splitCount++
}
created = created.Add(interval)
}
}
if option.Type != "stdout" {
_ = writer.Close()
fmt.Println(logFileName, "is created.")
}
return nil
}
// NewWriter returns a closeable writer corresponding to given log type
func NewWriter(logType string, logFileName string) (io.WriteCloser, error) {
switch logType {
case "stdout":
return os.Stdout, nil
case "log":
logFile, err := os.Create(logFileName)
if err != nil {
return nil, err
}
return logFile, nil
case "gz":
logFile, err := os.Create(logFileName)
if err != nil {
return nil, err
}
return gzip.NewWriter(logFile), nil
default:
return nil, nil
}
}
// NewLog creates a log for given format
func NewLog(format string, logLineByte int, t time.Time) string {
switch format {
case "apache_common":
return NewApacheCommonLog(t)
case "apache_combined":
return NewApacheCombinedLog(t)
case "apache_error":
return NewApacheErrorLog(t)
case "rfc3164":
return NewRFC3164Log(t)
case "rfc5424":
return NewRFC5424Log(t)
case "common_log":
return NewCommonLogFormat(t)
case "json":
return NewJSONLogFormat(t)
case "json_with_trace":
return NewJSONLogFormatWithTrace(logLineByte, t)
default:
return ""
}
}
// NewSplitFileName creates a new file path with split count
func NewSplitFileName(path string, count int) string {
logFileNameExt := filepath.Ext(path)
pathWithoutExt := strings.TrimSuffix(path, logFileNameExt)
return pathWithoutExt + strconv.Itoa(count) + logFileNameExt
}