Golang Logrus Package

28/12/2020
Chưa phân loại

In this lesson on Logrus package in Golang, we will study various examples on how effective Logging can be done in Go and see how  important logs are in Go programming language. We will get started now.

Starting with Go

Here is the directory structure which I made for my Hello World program:

Here is the program we created:

package main

import "fmt"

func main() {
    fmt.Printf("Hello, world.n")
}

We can run the above program with following command:

go run hello.go

Once we run this command, here is the output you will see:

Now that looks good. Let’s move to our main agenda.

Logrus Package in Golang

To start using Logrus package in the Go program, we must get it. Run the following command:

go get -t github.com/Sirupsen/logrus

When we start using this package in IntelliJ, we see this error which we can resolve in one click:

Once you get the package, we can start to use it. Let’s start with a simple program.

Basic Logging with Logrus

We will start with very basic INFO level logging example. Logging can be done with String messages and meta-data in form of key-value pairs which appear like the same.

package main

import (
  log "github.com/Sirupsen/logrus"
)

func main() {
    log.WithFields(log.Fields{
      "website": "linuxhint.com",
      "awesome": 100,
      "help":   200,
      }).Info("Golang pro")
}

When we run this program, we can see the following output:

Now that is both useful and colorful!

Various Logging Levels

Now, we will try another example which will show the use of various Logging levels available in Logrus and in general. They are:

  • Info
  • Warning
  • Fatal
  • Debug
  • Panic

Let’s try to build a program and see how these log levels differ when they appear in our program:

package main

import (
  log "github.com/Sirupsen/logrus"
)

func main() {
    log.WithFields(log.Fields{
      "website": "linuxhint.com",
      "awesome": 100,
      }).Info("Golang pro INFO message")

    log.WithFields(log.Fields{
      "website": "linuxhint.com",
      "awesome": 100,
      }).Warn("Golang pro WARN message")

    log.WithFields(log.Fields{
      "website": "linuxhint.com",
      "awesome": 100,
      }).Fatal("Golang pro FATAL message")

    log.WithFields(log.Fields{
      "website": "linuxhint.com",
      "awesome": 100,
      }).Panic("Golang pro PANIC message")

    log.WithFields(log.Fields{
      "website": "linuxhint.com",
      "awesome": 100,
      }).Debug("Golang pro DEBUG message")
}

When we run this program, we will see the following output:

Noticed something? The log statements after the Fatal statement doesn’t even appear in our output. This is because as soon as a Fatal error is received, program execution stops in Golang.

Let’s modify the order of these statements and check if some changes in output are also observed:

This time, even Panic Log level reacted in the same manner but output was very different and detailed.

With Panic log level, you make sure that enough information about the host machine also printed in the output in the console so that the work is debuggable.

Simpler way to make Logs

In above calls, Logs were pretty detailed and with metadata as well. There is an easier way to log your messages. Let’s try this now:

package main

import (
  log "github.com/Sirupsen/logrus"
)

func main() {
    log.Debug("Debugging data here.")
    log.Info("Messages for common info")
    log.Warn("You should look at this warning!")
    log.Error("Something failed but program will continue.")
    // Calls os.Exit(1) after logging
    log.Fatal("I am leaving.")
    // Calls panic() after logging
    log.Panic("I won’t be printed :(")
}

Here is the output for the program:

The behavior for logging was the same but this time, they were easy to make in just one line.

Conclusion

In this post, we studied simple but useful examples on how we can log important messages with different severity and verbosity in our applications using the Logrus package with Golang.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Buy and Build Powerful Linux Server for Your Home

In the era of cloud computing, building a home server may seem like a strange idea. Why would you invest time, money, and...
29/12/2020

How to install Tor Browser 7.0 on Ubuntu 17.04 and Linux Mint

Tor Browser 7.0, a privacy security browser that allows you to use Tor on Windows, Mac OS X, or Linux without needing to...
12/02/2020

Configure Git Server with HTTP on CentOS 8

In this article, I am going to show you how to configure Git Smart HTTP server on CentOS 8 for hosting your Git repositories...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024

Thuê địa chỉ IPv4 IPv6 trọn gói ở đâu chất lượng, giá RẺ nhất?
27/05/2024