MCPcopy
hub / github.com/op/go-logging / Example

Function Example

example_test.go:5–40  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3import "os"
4
5func Example() {
6 // This call is for testing purposes and will set the time to unix epoch.
7 InitForTesting(DEBUG)
8
9 var log = MustGetLogger("example")
10
11 // For demo purposes, create two backend for os.Stdout.
12 //
13 // os.Stderr should most likely be used in the real world but then the
14 // "Output:" check in this example would not work.
15 backend1 := NewLogBackend(os.Stdout, "", 0)
16 backend2 := NewLogBackend(os.Stdout, "", 0)
17
18 // For messages written to backend2 we want to add some additional
19 // information to the output, including the used log level and the name of
20 // the function.
21 var format = MustStringFormatter(
22 `%{time:15:04:05.000} %{shortfunc} %{level:.1s} %{message}`,
23 )
24 backend2Formatter := NewBackendFormatter(backend2, format)
25
26 // Only errors and more severe messages should be sent to backend2
27 backend2Leveled := AddModuleLevel(backend2Formatter)
28 backend2Leveled.SetLevel(ERROR, "")
29
30 // Set the backends to be used and the default level.
31 SetBackend(backend1, backend2Leveled)
32
33 log.Debugf("debug %s", "arg")
34 log.Error("error")
35
36 // Output:
37 // debug arg
38 // error
39 // 00:00:00.000 Example E error
40}

Callers

nothing calls this directly

Calls 10

InitForTestingFunction · 0.85
MustGetLoggerFunction · 0.85
MustStringFormatterFunction · 0.85
NewBackendFormatterFunction · 0.85
AddModuleLevelFunction · 0.85
SetBackendFunction · 0.85
DebugfMethod · 0.80
ErrorMethod · 0.80
NewLogBackendFunction · 0.70
SetLevelMethod · 0.65

Tested by

no test coverage detected