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

[NGINX] Hướng dẫn cài đặt Nginx trên CentOS 8

NGINX (đọc là “engine x”) là một phần mềm mã nguồn mở cho web serving, reverse proxying, caching,...
30/12/2020

How to set up Varnish cache on Ubuntu 18.04

Most internet users don’t stick around slow websites longer than 10 seconds. In fact, according to a case study conducted...
29/12/2020

[CentOS 8] Hướng dẫn cấu hình IP tĩnh trong CentOS 8

Sau khi cài đặt Linux System, một trong các bước đầu tiên mà bất cứ người dùng nào kể cả...
30/12/2020
Bài Viết

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

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

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