- Notifications
You must be signed in to change notification settings - Fork64
Graylog2/go-gelf
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
GELF (Graylog Extended Log Format) is an application-level loggingprotocol that avoids many of the shortcomings ofsyslog. While itcan be run over any stream or datagram transport protocol, it hasspecial support (chunking) to allow long messages to be split overmultiple datagrams.
In order to enable versionning of this package with Go, this projectis using GoPkg.in. The default branch of this project will be v1for some time to prevent breaking clients. We encourage all projectto change their imports to the new GoPkg.in URIs as soon as possible.
To see up to date code, make sure to switch to the master branch.
This implementation currently supports only UDP as a transportprotocol. TCP and TLS are unsupported.
The library provides an API that applications can use to log messagesdirectly to a Graylog server and anio.Writer
that can be used toredirect the standard library's log messages (os.Stdout
) to aGraylog server.
go-gelf is go get-able:
go get gopkg.in/Graylog2/go-gelf.v1/gelforgo get github.com/Graylog2/go-gelf/gelf
This will get you version 1.0.0, with only UDP support and legacy API.Newer versions are available through GoPkg.in:
go get gopkg.in/Graylog2/go-gelf.v2/gelf
The easiest way to integrate graylog logging into your go app is byhaving yourmain
function (or eveninit
) calllog.SetOutput()
.By using anio.MultiWriter
, we can log to both stdout and graylog -giving us both centralized and local logs. (Redundancy is nice).
package mainimport ("flag""gopkg.in/Graylog2/go-gelf.v1/gelf""io""log""os")funcmain() {vargraylogAddrstringflag.StringVar(&graylogAddr,"graylog","","graylog server addr")flag.Parse()ifgraylogAddr!="" {gelfWriter,err:=gelf.NewWriter(graylogAddr)iferr!=nil {log.Fatalf("gelf.NewWriter: %s",err)}// log to both stderr and graylog2log.SetOutput(io.MultiWriter(os.Stderr,gelfWriter))log.Printf("logging to stderr & graylog2@'%s'",graylogAddr)}// From here on out, any calls to log.Print* functions// will appear on stdout, and be sent over UDP to the// specified Graylog2 server.log.Printf("Hello gray World")// ...}
The above program can be invoked as:
go run test.go -graylog=localhost:12201
When using UDP messages may be dropped or re-ordered. However, Graylogserver availability will not impact application performance; there isa small, fixed overhead per log call regardless of whether the targetserver is reachable or not.
- WriteMessage example
go-gelf is offered under the MIT license, see LICENSE for details.