├── LICENSE ├── README.md └── graylog.go /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Joris Bonnefoy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-graylog 2 | 3 | [![GoDoc](https://godoc.org/github.com/Devatoria/go-graylog?status.svg)](https://godoc.org/github.com/Devatoria/go-graylog) 4 | 5 | Graylog GELF messages sending using UDP, TCP or TCP/TLS, written in Golang. 6 | 7 | # Examples 8 | 9 | ```go 10 | package main 11 | 12 | import ( 13 | "time" 14 | 15 | "github.com/Devatoria/go-graylog" 16 | ) 17 | 18 | func main() { 19 | // Initialize a new graylog client with TLS 20 | g, err := graylog.NewGraylogTLS(graylog.Endpoint{ 21 | Transport: graylog.TCP, 22 | Address: "localhost", 23 | Port: 12202, 24 | }, 3*time.Second, nil) 25 | if err != nil { 26 | panic(err) 27 | } 28 | 29 | // Send a message 30 | err = g.Send(graylog.Message{ 31 | Version: "1.1", 32 | Host: "localhost", 33 | ShortMessage: "Sample test", 34 | FullMessage: "Stacktrace", 35 | Timestamp: time.Now().Unix(), 36 | Level: 1, 37 | Extra: map[string]string{ 38 | "MY-EXTRA-FIELD": "extra_value", 39 | }, 40 | }) 41 | if err != nil { 42 | panic(err) 43 | } 44 | 45 | // Close the graylog connection 46 | if err := g.Close(); err != nil { 47 | panic(err) 48 | } 49 | } 50 | ``` 51 | -------------------------------------------------------------------------------- /graylog.go: -------------------------------------------------------------------------------- 1 | package graylog 2 | 3 | import ( 4 | "crypto/tls" 5 | "encoding/json" 6 | "fmt" 7 | "net" 8 | "time" 9 | 10 | "github.com/Jeffail/gabs" 11 | ) 12 | 13 | // Transport represents a transport type enum 14 | type Transport string 15 | 16 | const ( 17 | UDP Transport = "udp" 18 | TCP Transport = "tcp" 19 | ) 20 | 21 | // Endpoint represents a graylog endpoint 22 | type Endpoint struct { 23 | Transport Transport 24 | Address string 25 | Port uint 26 | } 27 | 28 | // Graylog represents an established graylog connection 29 | type Graylog struct { 30 | Client *net.Conn 31 | TLSClient *tls.Conn 32 | } 33 | 34 | // Message represents a GELF formated message 35 | type Message struct { 36 | Version string `json:"version"` 37 | Host string `json:"host"` 38 | ShortMessage string `json:"short_message"` 39 | FullMessage string `json:"full_message,omitempty"` 40 | Timestamp int64 `json:"timestamp,omitempty"` 41 | Level uint `json:"level,omitempty"` 42 | Extra map[string]string `json:"-"` 43 | } 44 | 45 | // NewGraylog instanciates a new graylog connection using the given endpoint 46 | func NewGraylog(e Endpoint) (*Graylog, error) { 47 | c, err := net.Dial(string(e.Transport), fmt.Sprintf("%s:%d", e.Address, e.Port)) 48 | if err != nil { 49 | return nil, err 50 | } 51 | 52 | return &Graylog{Client: &c}, nil 53 | } 54 | 55 | // NewGraylogTLS instanciates a new graylog connection with TLS, using the given endpoint 56 | func NewGraylogTLS(e Endpoint, timeout time.Duration, config *tls.Config) (*Graylog, error) { 57 | c, err := tls.DialWithDialer(&net.Dialer{Timeout: timeout}, string(e.Transport), fmt.Sprintf("%s:%d", e.Address, e.Port), config) 58 | if err != nil { 59 | return nil, err 60 | } 61 | 62 | return &Graylog{TLSClient: c}, nil 63 | } 64 | 65 | // Send writes the given message to the given graylog client 66 | func (g *Graylog) Send(m Message) error { 67 | data, err := prepareMessage(m) 68 | if err != nil { 69 | return err 70 | } 71 | 72 | // Check if TLS client is instanciated, otherwise send using the classic client 73 | if g.TLSClient != nil { 74 | _, err = (*g.TLSClient).Write(data) 75 | } else { 76 | _, err = (*g.Client).Write(data) 77 | } 78 | 79 | return err 80 | } 81 | 82 | // Close closes the opened connections of the given client 83 | func (g *Graylog) Close() error { 84 | if g.TLSClient != nil { 85 | if err := (*g.TLSClient).Close(); err != nil { 86 | return err 87 | } 88 | } 89 | 90 | if g.Client != nil { 91 | if err := (*g.Client).Close(); err != nil { 92 | return err 93 | } 94 | } 95 | 96 | return nil 97 | } 98 | 99 | // prepareMessage marshal the given message, add extra fields and append EOL symbols 100 | func prepareMessage(m Message) ([]byte, error) { 101 | // Marshal the GELF message in order to get base JSON 102 | jsonMessage, err := json.Marshal(m) 103 | if err != nil { 104 | return []byte{}, err 105 | } 106 | 107 | // Parse JSON in order to dynamically edit it 108 | c, err := gabs.ParseJSON(jsonMessage) 109 | if err != nil { 110 | return []byte{}, err 111 | } 112 | 113 | // Loop on extra fields and inject them into JSON 114 | for key, value := range m.Extra { 115 | _, err = c.Set(value, fmt.Sprintf("_%s", key)) 116 | if err != nil { 117 | return []byte{}, err 118 | } 119 | } 120 | 121 | // Append the \n\0 sequence to the given message in order to indicate 122 | // to graylog the end of the message 123 | data := append(c.Bytes(), '\n', byte(0)) 124 | 125 | return data, nil 126 | } 127 | --------------------------------------------------------------------------------