Golang : Logging with logrus
While researching for the previous tutorial on how to save log messages to file. I came across an interesting package for logging in Golang - the package Logrus basically simplified the entire logging process. This tutorial will cover basic usage and if you want more advance stuff.. please see https://github.com/Sirupsen/logrus .
Before running the examples below. Please :
>go get github.com/Sirupsen/logrus
first.
Here you go :
package main
import (
"github.com/sirupsen/logrus"
)
var logger = logrus.New()
func main() {
// error, but not fatal
logger.Error("Some error, but continue.")
// just a warning, nothing to worry about ... yet
logger.Warn("Warning....crash to ground imminent.")
// this line WILL NOT appear because the default log.Level does not
// log anything that is debug level
// for debugging purpose...
logger.Debug("Just debugging information.")
// to enable debug
logger.Level = logrus.DebugLevel
// this LINE WILL APPEAR
logger.Debug("Just debugging information.")
// add fields to debug information
logger.WithFields(logrus.Fields{
"variable": "value",
"username": "adam",
}).Debug("Even useful debugging information.")
// notice that debug as no color coding for fields...but not with Info!
logger.WithFields(logrus.Fields{
"variable": "value",
"username": "adam",
}).Info("Even useful debugging information.")
// just for your information
logger.Info("Oh, just FYI!")
// Executes os.Exit(1) function after this line
logger.Fatal("Abort!")
// Executess panic() function after this line
logger.Panic("Panic!")
}
Sample output :
ERRO[0000] Some error, but continue.
WARN[0000] Warning....crash to ground imminent.
DEBU[0000] Just debugging information.
DEBU[0000] Even useful debugging information. username=adam variable=value
INFO[0000] Even useful debugging information. username=adam variable=value
INFO[0000] Oh, just FYI!
FATA[0000] Abort!
exit status 1
References :
See also : Golang : How to save log messages to file?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.6k Golang : Muxing with Martini example
+9.6k Golang : Get current, epoch time and display by year, month and day
+6k Golang & Javascript : How to save cropped image to file on server
+7.1k Golang : Accessing dataframe-go element by row, column and name example
+6.9k Golang : Transform lisp or spinal case to Pascal case example
+14.6k Golang : Submit web forms without browser by http.PostForm example
+6.6k Swift : substringWithRange() function example
+11.8k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+16.8k Golang : How to save log messages to file?
+13.8k Golang : Reverse IP address for reverse DNS lookup example
+4.7k Python : Find out the variable type and determine the type with simple test
+8.7k Golang : Build and compile multiple source files