()
| 3 | import "os" |
| 4 | |
| 5 | func 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 | } |
nothing calls this directly
no test coverage detected