Logrus in Golang — How to use Logrus in Go?

Yash Chauhan
3 min readJun 30, 2022

What is Logrus?

Logrus is a simple, flexible, and powerful logging library for Go. Repo

Why there is a need for Logrus?

It is a drop-in replacement for the standard Go log package. It supports all the standard log levels and provides a simple and flexible API for creating log messages.

What is the difference between Logrus and the standard Go log package?

To use the logrus package we need to import it first using the following command:

go get github.com/sirupsen/logrus

Log levels:

Unlike the standard log package, logrus supports log levels.

Logrus has seven log levels: Trace, Debug, Info, Warn, Error, Fatal, and Panic. The severity of each level increases as you go down the list.

log.Trace("Something very low level.")
log.Debug("Useful debugging information.")
log.Info("Something noteworthy happened!")
log.Warn("You should probably take a look at this.")
log.Error("Something failed but I'm not quitting.")
// Calls os.Exit(1) after logging
log.Fatal("Bye.")
// Calls panic() after logging
log.Panic("I'm bailing.")

Wherein Go Log Package we don’t have the ability to log the severity of the log message. We have manually coded it in the log package. Logrus made the job of logging easy. It provides a simple and flexible API for creating log messages.

Using SetFormatter we can change the format of the log message. There are two formats: Text(Default) and JSON.

log.SetFormatter(&logrus.TextFormatter{})

If we use TextFormatter, the output will be in text format. It is the default format.

output:
time=”2009–11–10T23:00:00Z” level=info msg=”Text Log”

and

log.SetFormatter(&logrus.JSONFormatter{})

If we use the JSONFormatter, the output will be in JSON format.

output:
{“level”:”info”,”msg”:”Json Log”,”time”:”2009–11–10T23:00:00Z”}

we can even configure the JSONFormatter to output the time in the format we want.

log.SetFormatter(&logrus.TextFormatter{
DisableColors: true,
DisableTimestamp: false,
TimestampFormat: “2006–01–02 15:04:05”,
})

for more information you can read here (Logrus Docs)

We use SetOutput to set the output of the log message to the standard output.

log.SetOutput(os.Stdout) // set the output to stdout

We use SetLevel to set the level of the log message. As mentioned we have a total of seven log levels: Trace, Debug, Info, Warn, Error, Fatal, and Panic.

The default level is info. In this case, only the info, warn, error, fatal, and panic log messages will be logged.

log.SetLevel(log.DebugLevel)

If we set the level to debug, then only the info, warn, error, fatal, panic & debug log will be logged.

Use SetReportCaller to see on which line of the code the log message is generated.

log.SetReportCaller(true) // set the report caller to true.

Why do we need to set the level of the log message?

For example, you are working on a project and you want to log the messages. But you don’t want to log the debug messages in production. So you can set the level of the log message to info. In this case, only the info, warn, error, fatal, and panic log messages will be logged. If you want to see the debug logs in the production, you can set the level to debug. Else usually we don’t show the debug logs in the production we only see that in the development stage.

Golang Logrus Simple Implementation

I hope you find it helpful. If you have any suggestions, please feel free to drop a comment.

Thanks for reading! A few (hopefully 50) claps? are always appreciated. Follow me and share this article if you like it.

--

--

Yash Chauhan

Software Engineer @Qube | Exploring Golang & Reactjs