GoLang Logs — log

Kassim Damilola
Nov 8 · 2 min read

A well-considered logging strategy can help you understand user behavior, localize errors, and monitor the performance of your applications. when building application to run on multiple platforms or application that communicate with multiple services it is important to follow a standard native logging methods to ensure basic simple conventions are followed.

The Go logging package called “log” provides a simple, easy to use logger strategy. This articles show how to implement a simple logger in GO.

Tracing simple logs

import (
"log"
)
...
log.PrintLn("Application failed")
log.FatalLn("Application has failed")
...
> 2019/11/08 04:17:10 Application started
2019/11/08 04:17:10 Application has failed
exit status 1

That logger writes to standard error and prints the date and time of each logged message. Every log message is output on a separate line: if the message being printed does not end in a newline, the logger will add one. The Fatal functions call os.Exit(1) after writing the log message.

Sample code

package mainimport (
"errors"
"fmt"
"log"
)
func divide(a float32, b float32) (float32, error) {
if b == 0 {
return 0, errors.New("can't divide by zero")
}
return a / b, nil
}
func main() {var a float32 = 10
var b float32
ret, err := divide(a, b)
if err != nil {
log.Print(err)
}
fmt.Println(ret)
}

Because our example divides by zero, it will output the following log message:

2019/11/08 04:19:31 can't divide by zero
0

Logging into a file

By default the standard logger, logs to standard error. But if you want to log to a different file instead, you would have to use the log.SetOutput(f) function. SetOutput takes in any value which satisfies the io.Writer Interface. Lets see a simple example which logs to a local file.

package mainimport (
"log"
"os"
)
func main() {
file, err := os.OpenFile("info.log", os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
defer file.Close()log.SetOutput(file)
log.Print("Logging to a file in Go!")
}

Now go back to the terminal and build and run the program. This time, no output will appear in the terminal because it was written to the file, info.log in the project working directory.

This is a quick and easy way to setup logging in your go code. Formatting the logger, i.e. deciding what information to log, can get messy really fast. It is very helpful to log, the type/level of logger, date and time at which the event occurred, file in which the event occurred, function in which the event occurred. Take advantage of external libraries like logrus that provides a JSON structured logger for Go (golang), completely API compatible with the standard library logger.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade