-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream-gcp.go
52 lines (43 loc) · 1.19 KB
/
stream-gcp.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
package logger
import (
"encoding/json"
"os"
"github.com/pkg/errors"
)
// GCPStream is the Stream that writes to the standard output
type GCPStream struct {
*json.Encoder
FilterLevel Level
}
// Write writes the given Record
// implements logger.Stream
func (stream *GCPStream) Write(record Record) error {
if stream.Encoder == nil {
stream.Encoder = json.NewEncoder(os.Stdout)
if stream.FilterLevel == 0 {
stream.FilterLevel = GetLevelFromEnvironment()
}
}
record["severity"] = severity(record["level"]) // see stackdriver
delete(record, "level")
delete(record, "time")
delete(record, "name")
if err := stream.Encoder.Encode(record); err != nil {
return errors.WithStack(err)
}
return nil
}
// ShouldWrite tells if the given level should be written to this stream
// implements logger.Stream
func (stream *GCPStream) ShouldWrite(level Level) bool {
return level.ShouldWrite(stream.FilterLevel)
}
// Flush flushes the stream (makes sure records are actually written)
// implements logger.Stream
func (stream *GCPStream) Flush() {
}
// String gets a string version
// implements the fmt.Stringer interface
func (stream GCPStream) String() string {
return "Stream to Google Cloud"
}