Skip to content

Instantly share code, notes, and snippets.

@painhardcore
Last active April 26, 2020 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save painhardcore/6e309e661883740ded883e3743dc9748 to your computer and use it in GitHub Desktop.
Save painhardcore/6e309e661883740ded883e3743dc9748 to your computer and use it in GitHub Desktop.
Logrus test
1 package main
2
3 import (
4 "os"
5
6 "github.com/sirupsen/logrus"
7 )
8
9 func main() {
10 log := logrus.New()
11 // set where we log
12 log.SetOutput(os.Stdout)
13 // set the levell
14 log.SetLevel(logrus.TraceLevel)
15
16 // simple thing logged
17 log.Info("HELLO WORLD")
18 log.Warn("Warning for the WORLD")
19
20 // more advanced
21 clog := log.WithFields(logrus.Fields{"id": 129923213, "user": "John"})
22 clog.Info("John entered the room")
23
24 // add one more level
25
26 moreContext := clog.WithFields(logrus.Fields{"money": 1000})
27 moreContext.Error("John left the building")
28
29 // you can change gloval formatter
30 clog.Logger.SetFormatter(&logrus.JSONFormatter{})
31
32 // and change context value
33 moreContext = moreContext.WithField("user", "Jason")
34 moreContext = moreContext.WithField("id", 1)
35 moreContext.Error("John became a JASON")
36
37 }
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment