[Golang] How to Record System Activity with Log on Go.

Amiruddin Saddam
Learning About Golang

--

A good system is a system that when a problem occurs, it can be identified and resolved quickly so that the system can be used properly again.

To be able to identify problems quickly we can make a note that can record all activities or processes carried out by the system. These records are commonly referred to as logs.

The logs can be saved or written to a separate file or written to a third party monitoring system according to the user’s business needs.

According to techoped.com,

An application log is a file of events that are logged by a software application. It contains errors, informational events and warnings. The format and content of an application log are determined by the developer of the software program, rather than the OS.

On this occasion, we will discuss how to record system activity with log on go programming language.

Actually in the programming language go, already available packages for logging. for its use we just need to import the package “log”

example of writing the default log package
example of log writing results using the default log package

As we saw above, the log that we have created only displays messages or system activity without displaying the log level, time, etc.

There are several log levels that we can use, including:

A. Info
Logs show information on what happened to the system. This log is usually used for information about processes or activities carried out by the system

B. Warn
Logs show information on something unexpected that happened to the system. At this level, logs are needed to prevent problems that will occur on the system, so we can make repairs before something happens.

C. Error
Logs show information on problems that occur in the system. At this level, the log function is very useful to be able to find out the problems that occur in the system, so that we will be able to fix these problems faster.

There are several types of packages or external libraries that we can use and modify according to our needs.

1.Logrus

Logrus is a structured logger for Go (golang), completely API compatible with the standard library logger.

How to implement ?

  1. Import logrus package at “github.com/sirupsen/logrus”
  2. Put code below in your project
logrus.WithFields(logrus.Fields{"at":time.Now().Format("2006-01-02 15:04:05")}).Error("message")
example of logging using logrus for level error
example of log writing results using logrus for level error

2.Zap

Zap takes a different approach. It includes a reflection-free, zero-allocation JSON encoder, and the base Logger strives to avoid serialization overhead and allocations wherever possible. By building the high-level SugaredLogger on that foundation, zap lets users choose when they need to count every allocation and when they'd prefer a more familiar, loosely typed API.

How to implement ?

  1. Import zaplogger package at “github.com/uber-go/zap”
  2. Put code below in your project
zaplogger, _ := zap.NewProduction()
defer zaplogger.Sync()
zaplogger.Info("success to update product",
zap.String("product ID", req.ProductID),
)
example of logging using zap loggerfor level info
example of log writing results using zaplogger for level error

Those are some external log packages that are widely used in the Go programming language.

With the log on the system created, it will make it easier for us as developers to be able to find out activities or obstacles that occur in the system.

Reference:

  1. https://www.techopedia.com/definition/1819/application-log
  2. https://sematext.com/blog/logging-levels/
  3. https://pkg.go.dev/log
  4. https://github.com/sirupsen/logrus
  5. https://github.com/uber-go/zap

--

--