...

Source file src/github.com/sirupsen/logrus/example_basic_test.go

Documentation: github.com/sirupsen/logrus

     1  package logrus_test
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/sirupsen/logrus"
     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,
    67  	}).Panic("It's over 9000!")
    68  
    69  	// Output:
    70  	// level=trace msg="Went to the beach" animal=walrus number=0
    71  	// level=debug msg="Started observing beach" animal=walrus number=8
    72  	// level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10
    73  	// level=warning msg="The group's number increased tremendously!" number=122 omg=true
    74  	// level=debug msg="Temperature changes" temperature=-4
    75  	// level=panic msg="It's over 9000!" animal=orca size=9009
    76  	// level=error msg="The ice breaks!" err_animal=orca err_level=panic err_message="It's over 9000!" err_size=9009 number=100 omg=true
    77  }
    78  

View as plain text