The log package provides a simple, thread-safe interface for logging messages with timestamps, prefixes, and optional flags. You can use the default logger for quick output or create a custom logger to control formatting, output destination, and error handling.
For basic usage, import the package and call Print, Printf, or Fatal directly. The default logger writes to os.Stderr and includes a timestamp.
package main
import (
"log"
"os"
)
func main() {
// Basic usage with default logger
log.Println("Application started")
log.Printf("Processing request ID: %d", 12345)
// Log a fatal error and exit
if false {
log.Fatal("Critical failure occurred")
}
}
To customize the logger, create a new instance using New(). This allows you to change the output writer (e.g., to a file), set a custom prefix, and adjust flags like Lshortfile to include the source file and line number.
package main
import (
"log"
"os"
"time"
)
func main() {
// Create a custom logger writing to a file
f, err := os.OpenFile("app.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
}
defer f.Close()
// Configure flags: timestamp, short file name, line number
customLogger := log.New(f, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile)
customLogger.Println("Custom log entry with file and line info")
customLogger.Printf("Value at %s: %v", time.Now(), 42)
}
Key flags to remember include Ldate, Ltime, Lshortfile (file:line), and Llongfile (full path:line). If you need to suppress output entirely, pass nil as the writer to log.New(). For production systems, consider using structured logging libraries like zap or logrus for better performance and JSON formatting, but the standard log package remains ideal for simple scripts and debugging.