View : 0

12/04/2026 18:16pm

Ep.17 Go and Logging - Recording Every Event in the Program

Ep.17 Go and Logging - Recording Every Event in the Program

#Superdev School

#programming language

#programming for beginners

#programmers

#Practice programming

#Programming Education

#log.Panic

#log.Fatal

#log.Print

#Log Management

#Event Recording

#Logging

#Golang

Go and Logging - Recording Every Event in the Program

 

What is Logging?

Logging is the process of recording data or messages that indicate what is happening in the program, such as logging when a database connection is successful or logging warning messages when an error occurs. Logging helps us troubleshoot issues more easily when the program malfunctions.

 

Basic Usage of Logging in Go

Go has a log package that makes it easy to implement logging using main functions like log.Print, log.Println, and log.Printf.

Explanation of the Code:

log.Println prints a message followed by a new line.

log.Print prints a message.

log.Printf is used to format messages, such as using %d for numbers.

Example of Logging Usage:

package main

import (
    "log"
)

func main() {
    log.Println("Starting the application...")
    log.Print("This is a regular log message.")
    log.Printf("This is a formatted log message: %d", 123)
}

 

Logging Errors and Fatal Logs

In addition to logging regular messages, we can also log messages when errors occur using log.Fatal and log.Panic.

log.Fatal: Logs the error and stops the program.

log.Panic: Logs the error and shows the stack trace before stopping the program.

Example of Usage:

In this example, log.Fatal will log a message and immediately stop the program when an error occurs.

package main

import (
    "log"
    "os"
)

func main() {
    file, err := os.Open("nonexistent_file.txt")
    if err != nil {
        log.Fatal("Error opening file:", err)
    }
    defer file.Close()
}

 

Logging to a File Instead of Displaying on the Screen

Sometimes we may want to store logs in a file instead of printing them to the screen for later review of events.

Example of Logging to a File:

In this example:

log.SetOutput(file) allows log messages to be recorded in the app.log file.

package main

import (
    "log"
    "os"
)

func main() {
    file, err := os.OpenFile("app.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
    if err != nil {
        log.Fatal("Could not open log file:", err)
    }
    defer file.Close()

    log.SetOutput(file) // ตั้งให้ log เขียนลงไฟล์แทนหน้าจอ

    log.Println("Starting application...")
    log.Println("Logging information to file.")
}

 

Using Flags to Add Details to Logs

We can add details to logs by using flags to indicate that the log should show the date and time of the recorded messages.

Commonly Used Flags:

log.LstdFlags: Shows the date and time.

log.Lshortfile: Shows the location of the file where the event occurred.

Example:

log.SetFlags(log.LstdFlags | log.Lshortfile)
log.Println("This is a log message with date, time, and file location.")

 

In Summary

  • Use log.Print, log.Println, and log.Printf for regular messages.
  • Use log.Fatal and log.Panic for logging errors and stopping the program.
  • Logs can be stored in files for long-term data retention.
  • Use flags to add date, time, and file location information to logs.