0
0
GoHow-ToBeginner · 3 min read

How to Use log Package in Go: Syntax and Examples

In Go, use the log package to write logs easily with functions like log.Print, log.Println, and log.Fatalf. Import log and call these functions to output messages to the standard error with timestamps by default.
📐

Syntax

The log package provides simple logging functions:

  • log.Print(args ...interface{}): Prints log message without newline.
  • log.Println(args ...interface{}): Prints log message with newline.
  • log.Printf(format string, args ...interface{}): Prints formatted log message.
  • log.Fatal(args ...interface{}): Prints message then calls os.Exit(1).
  • log.Fatalf(format string, args ...interface{}): Formatted print then exit.
  • log.Panic(args ...interface{}): Prints message then panics.

By default, logs include date and time.

go
import "log"

func main() {
    log.Print("Simple log message")
    log.Println("Log message with newline")
    log.Printf("Formatted number: %d", 42)
}
💻

Example

This example shows basic usage of the log package to print messages and handle a fatal error.

go
package main

import (
    "log"
)

func main() {
    log.Println("Starting the program")

    value := 10
    if value < 20 {
        log.Printf("Value %d is less than 20", value)
    }

    // Simulate a fatal error
    if value < 0 {
        log.Fatal("Value cannot be negative")
    }

    log.Println("Program finished successfully")
}
Output
2024/06/01 00:00:00 Starting the program 2024/06/01 00:00:00 Value 10 is less than 20 2024/06/01 00:00:00 Program finished successfully
⚠️

Common Pitfalls

Common mistakes when using the log package include:

  • Using log.Fatal or log.Fatalf unintentionally, which stops the program immediately.
  • Not importing the log package.
  • Expecting log.Print to add a newline automatically (use log.Println for that).
  • Ignoring the default timestamp format if you want cleaner logs (you can customize flags).
go
package main

import "log"

func main() {
    // Wrong: log.Fatal stops the program immediately
    // log.Fatal("This will stop the program")

    // Right: Use log.Print or log.Println for normal messages
    log.Println("This is a normal log message")
}
Output
2024/06/01 00:00:00 This is a normal log message
📊

Quick Reference

FunctionDescription
log.Print(args ...interface{})Prints log message without newline
log.Println(args ...interface{})Prints log message with newline
log.Printf(format string, args ...interface{})Prints formatted log message
log.Fatal(args ...interface{})Prints message then exits program
log.Fatalf(format string, args ...interface{})Formatted print then exit
log.Panic(args ...interface{})Prints message then panics

Key Takeaways

Import the log package to use simple logging functions like log.Print and log.Println.
Use log.Fatalf or log.Fatal to log errors and stop the program immediately.
log.Print does not add a newline; use log.Println for automatic newlines.
Default logs include timestamps; customize with log.SetFlags if needed.
Avoid unintentional program exit by careful use of log.Fatal functions.