Skip to content

Commit 7b0895a

Browse files
updated event to use interface, and updated README
1 parent e274f65 commit 7b0895a

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ func main() {
4343
)
4444

4545
// Send a log event with the client
46-
splunk.Log(map[string]string{"msg": "send key/val pairs here", "msg2": "anything that is useful to you in the log event"})
46+
err := splunk.Log(interface{"msg": "send key/val pairs or json objects here", "msg2": "anything that is useful to you in the log event"})
47+
if err != nil {
48+
return err
49+
}
4750
}
4851

4952
```

splunk/splunk.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ type Event struct {
1717
Source string `json:"source" binding:"required"` // app name
1818
SourceType string `json:"sourcetype" binding:"required"` // Splunk bucket to group logs in
1919
Index string `json:"index" binding:"required"` // idk what it does..
20-
Event map[string]string `json:"event" binding:"required"` // throw any useful key/val pairs here
20+
Event interface{} `json:"event" binding:"required"` // throw any useful key/val pairs here
2121
}
2222

23-
2423
// Client manages communication with Splunk's HTTP Event Collector.
2524
// New client objects should be created using the NewClient function.
2625
//
@@ -55,7 +54,7 @@ func NewClient(httpClient *http.Client, URL string, Token string, Source string,
5554

5655
// NewEvent creates a new log event to send to Splunk.
5756
// This should be the primary way a Splunk log object is constructed, and is automatically called by the Log function attached to the client.
58-
func NewEvent(event map[string]string, source string, sourcetype string, index string) (Event) {
57+
func NewEvent(event interface{}, source string, sourcetype string, index string) (Event) {
5958
hostname, _ := os.Hostname()
6059
e := Event{Time: time.Now().Unix(), Host: hostname, Source: source, SourceType: sourcetype, Index: index, Event: event}
6160

@@ -67,7 +66,7 @@ func NewEvent(event map[string]string, source string, sourcetype string, index s
6766
// All that must be provided for a log event are the desired map[string]string key/val pairs. These can be anything
6867
// that provide context or information for the situation you are trying to log (i.e. err messages, status codes, etc).
6968
// The function auto-generates the event timestamp and hostname for you.
70-
func (c *Client) Log(event map[string]string) (error) {
69+
func (c *Client) Log(event interface{}) (error) {
7170
// create Splunk log
7271
log := NewEvent(event, c.Source, c.SourceType, c.Index)
7372

@@ -94,15 +93,15 @@ func (c *Client) Log(event map[string]string) (error) {
9493
// If statusCode is not good, return error string
9594
switch res.StatusCode {
9695
case 200:
96+
return nil
9797
default:
9898
// Turn response into string and return it
9999
buf := new(bytes.Buffer)
100100
buf.ReadFrom(res.Body)
101101
responseBody := buf.String()
102102
err = errors.New(responseBody)
103103

104-
//log.Print(responseBody) // print error to screen for checking/debugging
105104
}
106-
105+
//log.Print(responseBody) // print error to screen for checking/debugging
107106
return err
108107
}

0 commit comments

Comments
 (0)