Exemplo n.º 1
0
// Here we'll setup a klog pipeline which will be used to cleanup our output
// stream.
func Example_Pipeline() {

	// It's pretty easy to create a printer so, because this is go, let's log to
	// a channel.
	linesC := make(chan string, 100)
	chanPrinter := func(line *klog.Line) {
		linesC <- fmt.Sprintf("<%s> %s", line.Key, line.Value)
	}

	// Next up we'll create a filter stage for our pipeline. FilterIn indicates
	// that all matching patterns will be removed from the log stream. Filters
	// are applied on the keys and can test for a full, prefix or suffix match.
	filter := klog.NewFilter(klog.FilterOut)
	filter.AddSuffix("debug")

	// Dedup is another useful stage that can be used to avoid spamming the logs
	// with duplicated messages. Deduping is done seperately for each key-stream
	// and is periodically flushed every second by default.
	dedup := klog.NewDedup()

	// Now we'll setup our pipeline using the handy klog.Chain function. klog
	// also includes a klog.Fork function which can be used to duplicate the
	// log-stream down multiple pipelines.
	klog.SetPrinter(
		klog.Chain(filter,
			klog.Chain(dedup,
				klog.PrinterFunc(chanPrinter))))

	// Time to print stuff out.
	klog.KPrint("test.info", "hello")
	klog.KPrint("test.info", "hello")
	klog.KPrint("test.debug", "whoops")
	klog.KPrint("test.info", "hello")
	klog.KPrint("test.info", "world")

	// Alright, let's read our output.
	for i := 0; i < 3; i++ {
		fmt.Println(<-linesC)
	}

	// Output:
	// <test.info> hello
	// <test.info> hello [2 times]
	// <test.info> world
}
Exemplo n.º 2
0
// The klogr package provides wrapppers for printer stages which makes them
// accessible from a rest interface using the gorest package.
func Example_REST() {

	// Here we create a REST enabled filter which allows us to modify the
	// filtering rules of the stage while the program is running.
	filter := klog.NewFilterREST("/path/to/filter", klog.FilterIn)

	// The ring stage prints to a ring buffer containing the last N elements
	// printed. Wrapping this stage in a REST interface allows us to query the
	// most recent log elements remotely.
	ring := klog.NewRingREST("/path/to/ring", 1000)

	// REST enabled stages act like regular stages so they can be used directly
	// in a klog pipeline.
	klog.SetPrinter(
		klog.Chain(filter,
			klog.Fork(ring, klog.GetPrinter())))

	klog.KPrint("a.b.c", "hello world")
}