Ejemplo n.º 1
0
// HandleMeters sends the given values to klog.
func (KlogHandler) HandleMeters(values map[string]float64) {
	body, err := json.Marshal(values)
	if err != nil {
		log.Panicf("unable to marshal meter values: %s", err)
	}

	klog.KPrint("meter.klog.info", string(body))
}
Ejemplo n.º 2
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
}
Ejemplo n.º 3
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")
}
Ejemplo n.º 4
0
// Barebone basic usage example of klog.
func Example_Simple() {

	// Default klog printer is a light wrapper around the standard log package
	// so we need to modify it a bit to make it work for our example.
	log.SetFlags(0)
	log.SetOutput(os.Stdout)
	log.SetPrefix("klog ")

	// KPrint and KPrintf are the only supported print functions where the first
	// argument is a key identifying the source of the log message. The key will
	// be used in future example to manipulate the log lines as they flow
	// through klog.
	klog.KPrint("a.b.c", "hello")
	klog.KPrintf("c.b.a", "%s", "world")

	// Output:
	// klog <a.b.c> hello
	// klog <c.b.a> world
}