-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (67 loc) · 2.05 KB
/
main.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
package cflogger
import (
"encoding/json"
"fmt"
"github.com/r-franke/cfconfig"
"github.com/r-franke/cfrabbit/publisher"
"log"
"os"
)
type MaintenancePublisher struct {
publisher *publisher.Publisher
}
type CustomErrorLogger struct {
*log.Logger
}
//goland:noinspection GoUnusedGlobalVariable
var (
InfoLogger *log.Logger
ErrorLogger *log.Logger
MaintenanceLogger *log.Logger
internalErrorLogger *log.Logger
maintenancePublisher *MaintenancePublisher
env cfconfig.Env
reportErrors bool
)
func init() {
var err error
env = cfconfig.LoadEnvironment("cflogger-dev", []cfconfig.Request{})
_, reportErrors = os.LookupEnv("REPORT_ERRORS")
InfoLogger = log.New(os.Stdout, "", log.Lshortfile)
InfoLogger.Printf("cflogger: Starting up, REPORT_ERRORS = %t", reportErrors)
internalErrorLogger = log.New(os.Stderr, "cflogger: ", log.Lshortfile)
maintenancePublisher = &MaintenancePublisher{}
maintenancePublisher.publisher, err = publisher.NewPublisher("signal.out", "fanout")
if err != nil {
internalErrorLogger.Fatalf("cannot get RabbitPublisher\n%s", err.Error())
}
MaintenanceLogger = log.New(maintenancePublisher, fmt.Sprintf("%s:\n", env.AppName), log.Lshortfile)
ErrorLogger = log.New(&CustomErrorLogger{log.New(os.Stderr, "", log.Lshortfile)}, "", 0)
}
func (cel *CustomErrorLogger) Write(p []byte) (n int, err error) {
if reportErrors {
MaintenanceLogger.Print(string(p))
}
cel.Logger.Print(string(p))
return len(p), nil
}
func (ml *MaintenancePublisher) Write(p []byte) (n int, err error) {
signalMsg := Payload{
Payload: SignalMessage{
Channel: "signal",
Subscribers: []string{"[H] Maintenance"},
MessageBody: string(p),
},
}
marshalledMsg, err := json.Marshal(signalMsg)
if err != nil {
internalErrorLogger.Printf("error marshalling message: %v\n%s\n", signalMsg, err.Error())
return -1, err
}
err = ml.publisher.Publish("", marshalledMsg)
if err != nil {
internalErrorLogger.Printf("Could not send maintenance message: %s", string(p))
return -1, err
}
return len(p), nil
}