Giter Club home page Giter Club logo

go-gelf's Introduction

go-gelf - GELF library and writer for Go

GELF is graylog2's UDP logging format. This library provides an API that applications can use to log messages directly to a graylog2 server, along with an io.Writer that can be use to redirect the standard library's log messages (or os.Stdout), to a graylog2 server.

Installing

go-gelf is go get-able:

go get github.com/Graylog2/go-gelf/gelf

Usage

The easiest way to integrate graylog logging into your go app is by having your main function (or even init) call log.SetOutput(). By using an io.MultiWriter, we can log to both stdout and graylog - giving us both centralized and local logs. (Redundancy is nice).

package main

import (
	"flag"
	"github.com/Graylog2/go-gelf/gelf"
	"io"
	"log"
	"os"
)

func main() {
	var graylogAddr string

	flag.StringVar(&graylogAddr, "graylog", "", "graylog server addr")
	flag.Parse()

	if graylogAddr != "" {
		gelfWriter, err := gelf.NewWriter(graylogAddr)
		if err != nil {
			log.Fatalf("gelf.NewWriter: %s", err)
		}
		// log to both stderr and graylog2
		log.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

Because GELF messages are sent over UDP, graylog server availability doesn't impact application performance or response time. There is a small, fixed overhead per log call, regardless of whether the target server is reachable or not.

To Do

  • WriteMessage example

License

go-gelf is offered under the MIT license, see LICENSE for details.

go-gelf's People

Contributors

bpowers avatar lethain avatar lsowen avatar mariussturm avatar mschuett avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.