github.com/mundipagg/tracer-splunk-writer@v1.0.6/README.md (about) 1 # Splunk Writer 2 This library is a writer(sink) [Splunk](https://www.splunk.com/en_us) to use with [Tracer](https://github.com/mralves/tracer) 3 4 ## How to install 5 Using go get (not recommended): 6 ```bash 7 go get github.com/mundipagg/tracer-splunk-writer 8 ``` 9 10 Using [dep](github.com/golang/dep) (recommended): 11 ```bash 12 dep ensure --add github.com/mundipagg/tracer-splunk-writer@<version> 13 ``` 14 15 ## Configuration 16 17 |Field|Type|Mandatory?|Default|Description| 18 |---|---|---|:---:|---| 19 |Address|string|Y||Splunk **full** endpoint (i.e. http://localhost:8088/services/collector)| 20 |Key|string|N|""|Splunk [Token Key](https://docs.splunk.com/Documentation/Splunk/8.0.0/Data/UsetheHTTPEventCollector)| 21 |Application|string|Y||Application name| 22 |Minimum Level|uint8|N|DEBUG|Minimum Level to log following the [syslog](https://en.wikipedia.org/wiki/Syslog#Severity_level) standard| 23 |Timeout|time.Duration|N|0 (infinite)|Timeout of the HTTP client| 24 |MessageEnvelop|string|N|"%v"|A envelop that *wraps* the original message| 25 |DefaultProperties|splunk.Entry|N|{}|A generic object to append to *every* log entry, but can be overwritten by the original log entry| 26 |Buffer.Cap|int|N|100| Maximum capacity of the log buffer, when the buffer is full all logs are sent at once| 27 |Buffer.OnWait|int|N|100| Maximum size of the queue to send to Splunk| 28 |Buffer.BackOff|time.Duration|N|60 seconds| Delay between retries to Splunk| 29 |ConfigLineLog | map[string]interface{} | S | {} | Properties needed to insert log in splunk (host, source, sourcetype and index) 30 |DefaultPropertiesSplunk | map[string]interface{} | S | {} | Properties set by administrador on splunk 31 |DefaultPropertiesApp | map[string]interface{} | S | {} | Properties to information about your application 32 33 ## How to use 34 35 Below follows a simple example of how to use this lib: 36 37 ```go 38 package main 39 40 import ( 41 "fmt" 42 splunk "github.com/mundipagg/tracer-splunk-writer" 43 "time" 44 45 "github.com/mralves/tracer" 46 47 48 bsp "github.com/mundipagg/tracer-splunk-writer/buffer" 49 ) 50 51 type Safe struct { 52 tracer.Writer 53 } 54 55 type LogEntry = map[string]interface{} 56 57 func (s *Safe) Write(entry tracer.Entry) { 58 defer func() { 59 err := recover() 60 if err != nil { 61 fmt.Printf("%v", err) 62 } 63 }() 64 s.Writer.Write(entry) 65 } 66 67 func configureTracer() { 68 var writers []tracer.Writer 69 tracer.DefaultContext.OverwriteChildren() 70 writers = append(writers, &Safe{splunk.New(splunk.Config{ 71 Timeout: 3 * time.Second, 72 MinimumLevel: tracer.Debug, 73 ConfigLineLog: LogEntry{ 74 "host": "MyMachineName", 75 "source": "MySourceLog", 76 "sourcetype": "_json", 77 "index": "main", 78 }, 79 DefaultPropertiesSplunk: LogEntry{ 80 "ProcessName": "MyProcessSourceLog", 81 "ProductCompany": "MyCompanyName", 82 "ProductName": "MyProductName", 83 "ProductVersion": "1.0", 84 }, 85 DefaultPropertiesApp: LogEntry{ 86 "Properties": "Add here your default properties.", 87 88 }, 89 Application: "MyApplicationName", 90 Key: "dd4a1733-75c8-48b2-abba-c102af7b9523", 91 Address: "http://localhost:8088/services/collector", 92 Buffer: bsp.Config{ 93 OnWait: 2, 94 BackOff: 1 * time.Second, 95 Expiration: 5 * time.Second, 96 }, 97 })}) 98 99 for _, writer := range writers { 100 tracer.RegisterWriter(writer) 101 } 102 } 103 104 func inner() { 105 logger := tracer.GetLogger("moduleA.inner") 106 logger.Info("don't know which transaction is this") 107 logger.Info("but this log in this transaction") 108 logger = logger.Trace() 109 go func() { 110 logger.Info("this is also inside the same transaction") 111 func() { 112 logger := tracer.GetLogger("moduleA.inner.nested") 113 logger.Info("but not this one...") 114 115 }() 116 }() 117 } 118 119 func main() { 120 configureTracer() 121 logger := tracer.GetLogger("moduleA") 122 logger.Info("logging in transaction 'A'", "B") 123 logger.Info("logging in transaction 'B'", "B") 124 logger.Info("logging in transaction 'B'", "B") 125 logger.Info("logging in transaction 'A'", "A") 126 logger.Info("logging in transaction 'A'", "A") 127 logger = logger.Trace("C") // now all logs on this logger will be on the transaction C 128 logger.Info("logging in transaction 'C'") 129 logger.Info("logging in transaction 'C'", "A") 130 inner() 131 fmt.Scanln() 132 } 133 134 ```