-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.go
77 lines (72 loc) · 2.04 KB
/
stream.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
package logger
import (
"os"
"strings"
"time"
)
// Streamer is the interface a Logger writes to
type Streamer interface {
Write(record Record) error
ShouldWrite(level Level) bool
Flush()
}
// GetFlushFrequencyFromEnvironment fetches the flush frequency from the environment
// the frequency should be like https://golang.org/pkg/time/#ParseDuration or an ISO8601 duration.
//
// If not set, the frequency will be 5 minutes
func GetFlushFrequencyFromEnvironment() time.Duration {
if value, ok := os.LookupEnv("LOG_FLUSHFREQUENCY"); ok {
if strings.HasPrefix(value, "P") {
if duration, err := parseDuration(value); err == nil {
return duration
}
} else {
if !strings.HasSuffix(value, "h") && !strings.HasSuffix(value, "m") && !strings.HasSuffix(value, "s") {
value = value + "s"
}
if duration, err := time.ParseDuration(value); err == nil {
return duration
}
}
}
return 5 * time.Minute
}
// CreateStreamWithDestination creates a new Streamer from a list of strings
func CreateStreamWithDestination(destinations ...string) Streamer {
if len(destinations) == 0 {
destination, ok := os.LookupEnv("LOG_DESTINATION")
if !ok || len(destination) == 0 {
return &StdoutStream{}
}
destinations = []string{destination}
}
streams := []Streamer{}
for _, destination := range destinations {
var stream Streamer
switch strings.ToLower(destination) {
case "stdout":
stream = &StdoutStream{}
case "stderr":
stream = &StderrStream{}
case "gcp", "google", "googlecloud":
stream = &GCPStream{}
case "stackdriver":
stream = &StackDriverStream{}
case "nil", "null", "void", "blackhole", "nether":
stream = &NilStream{}
default:
if strings.HasPrefix(destination, "file://") {
stream = &FileStream{Path: strings.TrimPrefix(destination, "file://")}
} else if len(destination) > 0 {
stream = &FileStream{Path: destination}
} else {
stream = &StdoutStream{}
}
}
streams = append(streams, stream)
}
if len(streams) == 1 {
return streams[0]
}
return &MultiStream{ streams: streams }
}