forked from agtorre/go-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apex.go
37 lines (30 loc) · 781 Bytes
/
apex.go
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
package structured
import (
"errors"
"os"
"github.com/apex/log"
"github.com/apex/log/handlers/text"
)
// ThrowError throws an error that we'll trace
func ThrowError() error {
err := errors.New("a crazy failure")
log.WithField("id", "123").Trace("ThrowError").Stop(&err)
return err
}
// CustomHandler splits to two streams
type CustomHandler struct {
id string
handler log.Handler
}
// HandleLog adds a hook and does the emitting
func (h *CustomHandler) HandleLog(e *log.Entry) error {
e.WithField("id", h.id)
return h.handler.HandleLog(e)
}
// Apex has a number of useful tricks
func Apex() {
log.SetHandler(&CustomHandler{"123", text.New(os.Stdout)})
err := ThrowError()
//With error convenience function
log.WithError(err).Error("an error occurred")
}