-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslacklogger.go
47 lines (37 loc) · 988 Bytes
/
slacklogger.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
package slacklogger
import (
"fmt"
)
type SlackLogger struct {
webhookURL string
environment string
isDebug bool
}
// NewSlackLogger returns a new instance of SlackLogger
func NewSlackLogger(webhookURL, environment string, isDebug bool) *SlackLogger {
return &SlackLogger{
webhookURL: webhookURL,
environment: environment,
isDebug: isDebug,
}
}
func (logger *SlackLogger) Log(message string) {
LogWithURL(message, logger.webhookURL, logger.environment, logger.isDebug)
}
func (logger *SlackLogger) Write(message []byte) (int, error) {
logger.Log(string(message))
return len(message), nil
}
func LogWithURL(message, url, env string, isDebug bool) {
if env != "" {
message = fmt.Sprintf("env=%s, %s", env, message)
}
if isDebug {
fmt.Println("Debug: Logging to Slack: " + message)
return
}
err := Send(url, Payload{Text: message})
if err != nil {
fmt.Printf("Error while logging to Slack: %s\nOriginal message was: %s\n", err, message)
}
}