| 7 | ) |
| 8 | |
| 9 | func Example_basic() { |
| 10 | var log = logrus.New() |
| 11 | log.Formatter = new(logrus.JSONFormatter) |
| 12 | log.Formatter = new(logrus.TextFormatter) //default |
| 13 | log.Formatter.(*logrus.TextFormatter).DisableColors = true // remove colors |
| 14 | log.Formatter.(*logrus.TextFormatter).DisableTimestamp = true // remove timestamp from test output |
| 15 | log.Level = logrus.TraceLevel |
| 16 | log.Out = os.Stdout |
| 17 | |
| 18 | // file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666) |
| 19 | // if err == nil { |
| 20 | // log.Out = file |
| 21 | // } else { |
| 22 | // log.Info("Failed to log to file, using default stderr") |
| 23 | // } |
| 24 | |
| 25 | defer func() { |
| 26 | err := recover() |
| 27 | if err != nil { |
| 28 | entry := err.(*logrus.Entry) |
| 29 | log.WithFields(logrus.Fields{ |
| 30 | "omg": true, |
| 31 | "err_animal": entry.Data["animal"], |
| 32 | "err_size": entry.Data["size"], |
| 33 | "err_level": entry.Level, |
| 34 | "err_message": entry.Message, |
| 35 | "number": 100, |
| 36 | }).Error("The ice breaks!") // or use Fatal() to force the process to exit with a nonzero code |
| 37 | } |
| 38 | }() |
| 39 | |
| 40 | log.WithFields(logrus.Fields{ |
| 41 | "animal": "walrus", |
| 42 | "number": 0, |
| 43 | }).Trace("Went to the beach") |
| 44 | |
| 45 | log.WithFields(logrus.Fields{ |
| 46 | "animal": "walrus", |
| 47 | "number": 8, |
| 48 | }).Debug("Started observing beach") |
| 49 | |
| 50 | log.WithFields(logrus.Fields{ |
| 51 | "animal": "walrus", |
| 52 | "size": 10, |
| 53 | }).Info("A group of walrus emerges from the ocean") |
| 54 | |
| 55 | log.WithFields(logrus.Fields{ |
| 56 | "omg": true, |
| 57 | "number": 122, |
| 58 | }).Warn("The group's number increased tremendously!") |
| 59 | |
| 60 | log.WithFields(logrus.Fields{ |
| 61 | "temperature": -4, |
| 62 | }).Debug("Temperature changes") |
| 63 | |
| 64 | log.WithFields(logrus.Fields{ |
| 65 | "animal": "orca", |
| 66 | "size": 9009, |