-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
42 lines (32 loc) · 1.17 KB
/
main.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
38
39
40
41
42
// The purpose of this example is to show how to integrate with logrus.
package main
import (
"log"
"github.com/sirupsen/logrus"
adapter "github.com/axiomhq/axiom-go/adapters/logrus"
)
func main() {
// Export "AXIOM_DATASET" in addition to the required environment variables.
// 1. Setup the Axiom hook for logrus.
hook, err := adapter.New()
if err != nil {
log.Fatal(err)
}
// 2. Register an exit handler to have all logs flushed before the
// application exits in case of a "fatal" log operation.
logrus.RegisterExitHandler(hook.Close)
// 3. This makes sure logrus calls the registered exit handler. Alternaively
// hook.Close() can be called manually. It is safe to call multiple times.
//
// ❗THIS IS IMPORTANT❗ Without it, the logs will not be sent to Axiom as
// the buffer will not be flushed when the application exits.
defer logrus.Exit(0)
// 4. Spawn the logger.
logger := logrus.New()
// 5. Attach the Axiom hook.
logger.AddHook(hook)
// 6. Log ⚡
logger.WithField("mood", "hyped").Info("This is awesome!")
logger.WithField("mood", "worried").Warn("This is no that awesome...")
logger.WithField("mood", "depressed").Error("This is rather bad.")
}