-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackdriver.go
More file actions
75 lines (67 loc) · 1.98 KB
/
Copy pathstackdriver.go
File metadata and controls
75 lines (67 loc) · 1.98 KB
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
package logger
import (
"context"
"encoding/json"
"os"
"regexp"
"strings"
"cloud.google.com/go/logging"
"github.com/sirupsen/logrus"
)
//Logs for init global var logger
var Logs NewLogger
var hooksType string
var projectID string
//SetHooks for setting hooks based on main threw
func SetHooks(typ string, projectid string) {
hooksType = typ
projectID = projectid
log.Print(hooksType)
}
func StackDataBasic(entry string, Severity logging.Severity) {
hooks := strings.Contains(hooksType, "STACK")
if hooks {
ctx := context.Background()
client, err := logging.NewClient(ctx, projectID)
if err != nil {
logrus.Warning("Failed to create driverClient. Are you online? ", err)
return
}
defer client.Close()
//Sanitize input because stackdriver only support _ - alphabet and numeric and no spaces
reg, _ := regexp.Compile("[^a-zA-Z0-9_-]+")
logName := reg.ReplaceAllString(os.Getenv("APP_NAME"), "")
lg := client.Logger(logName)
//stackdriver logging entry has already automagically determine the type of payload, if string, it will be inputted to textPayload, if struct it will be inputted to jsonPayload and marshalled internally
entrys := logging.Entry{
Payload: entry,
Severity: Severity,
}
lg.Log(entrys)
return
}
}
//StackData logging to stack driver
func StackData(entry *logrus.Entry, Severity logging.Severity) {
hooks := strings.Contains(hooksType, "STACK")
if hooks {
ctx := context.Background()
client, err := logging.NewClient(ctx, projectID)
if err != nil {
logrus.Warning("Failed to create driverClient. Are you online? ", err)
return
}
defer client.Close()
//Sanitize input because stackdriver only support _ - alphabet and numeric and no spaces
reg, _ := regexp.Compile("[^a-zA-Z0-9_-]+")
logName := reg.ReplaceAllString(os.Getenv("APP_NAME"), "")
lg := client.Logger(logName)
b, _ := json.Marshal(entry.Data)
entrys := logging.Entry{
Payload: json.RawMessage(b),
Severity: Severity,
}
lg.Log(entrys)
return
}
}