example_test.go 1.13 KB
Newer Older
Jeromy's avatar
Jeromy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package logging

import "os"

func Example() {
	// This call is for testing purposes and will set the time to unix epoch.
	InitForTesting(DEBUG)

	var log = MustGetLogger("example")

	// For demo purposes, create two backend for os.Stdout.
	//
	// os.Stderr should most likely be used in the real world but then the
	// "Output:" check in this example would not work.
	backend1 := NewLogBackend(os.Stdout, "", 0)
	backend2 := NewLogBackend(os.Stdout, "", 0)

	// For messages written to backend2 we want to add some additional
	// information to the output, including the used log level and the name of
	// the function.
	var format = MustStringFormatter(
		"%{time:15:04:05.000} %{shortfunc} %{level:.1s} %{message}",
	)
	backend2Formatter := NewBackendFormatter(backend2, format)

	// Only errors and more severe messages should be sent to backend2
	backend2Leveled := AddModuleLevel(backend2Formatter)
	backend2Leveled.SetLevel(ERROR, "")

	// Set the backends to be used and the default level.
	SetBackend(backend1, backend2Leveled)

	log.Debug("debug %s", "arg")
	log.Error("error")

	// Output:
	// debug arg
	// error
	// 00:00:00.000 Example E error
}