项目作者: ZachtimusPrime

项目描述 :
A simple and lightweight HTTP Splunk logging package for Go. Instantiates a client to your Splunk server and allows you to submit log events as desired.
高级语言: Go
项目地址: git://github.com/ZachtimusPrime/Go-Splunk-HTTP.git
创建时间: 2017-01-03T18:19:03Z
项目社区:https://github.com/ZachtimusPrime/Go-Splunk-HTTP

开源协议:MIT License

下载


Go-Splunk-HTTP

A simple and lightweight HTTP Splunk logging package for Go. Instantiates a logging connection object to your Splunk server and allows you to submit log events as desired. Uses HTTP event collection on a Splunk server.

GoDoc
Build Status
Coverage Status
Go Report Card

Table of Contents

Installation

  1. go get "github.com/ZachtimusPrime/Go-Splunk-HTTP/splunk/v2"

Usage

Construct a new Splunk HTTP client, then send log events as desired.

For example:

  1. package main
  2. import "github.com/ZachtimusPrime/Go-Splunk-HTTP/splunk/v2"
  3. func main() {
  4. // Create new Splunk client
  5. splunk := splunk.NewClient(
  6. nil,
  7. "https://{your-splunk-URL}:8088/services/collector",
  8. "{your-token}",
  9. "{your-source}",
  10. "{your-sourcetype}",
  11. "{your-index}"
  12. )
  13. // Use the client to send a log with the go host's current time
  14. err := splunk.Log(
  15. interface{"msg": "send key/val pairs or json objects here", "msg2": "anything that is useful to you in the log event"}
  16. )
  17. if err != nil {
  18. return err
  19. }
  20. // Use the client to send a log with a provided timestamp
  21. err = splunk.LogWithTime(
  22. time.Now(),
  23. interface{"msg": "send key/val pairs or json objects here", "msg2": "anything that is useful to you in the log event"}
  24. )
  25. if err != nil {
  26. return err
  27. }
  28. // Use the client to send a batch of log events
  29. var events []splunk.Event
  30. events = append(
  31. events,
  32. splunk.NewEvent(
  33. interface{"msg": "event1"},
  34. "{desired-source}",
  35. "{desired-sourcetype}",
  36. "{desired-index}"
  37. )
  38. )
  39. events = append(
  40. events,
  41. splunk.NewEvent(
  42. interface{"msg": "event2"},
  43. "{desired-source}",
  44. "{desired-sourcetype}",
  45. "{desired-index}"
  46. )
  47. )
  48. err = splunk.LogEvents(events)
  49. if err != nil {
  50. return err
  51. }
  52. }

Splunk Writer

To support logging libraries, and other output, we’ve added an asynchronous Writer. It supports retries, and different intervals for flushing messages & max log messages in its buffer

The easiest way to get access to the writer with an existing client is to do:

  1. writer := splunkClient.Writer()

This will give you an io.Writer you can use to direct output to splunk. However, since the io.Writer() is asynchronous, it will never return an error from its Write() function. To access errors generated from the Client,
Instantiate your Writer this way:

  1. splunk.Writer{
  2. Client: splunkClient
  3. }

Since the type will now be splunk.Writer(), you can access the Errors() function, which returns a channel of errors. You can then spin up a goroutine to listen on this channel and report errors, or you can handle however you like.

Optionally, you can add more configuration to the writer.

  1. splunk.Writer {
  2. Client: splunkClient,
  3. FlushInterval: 10 *time.Second, // How often we'll flush our buffer
  4. FlushThreshold: 25, // Max messages we'll keep in our buffer, regardless of FlushInterval
  5. MaxRetries: 2, // Number of times we'll retry a failed send
  6. }