Logging
Context
Section titled “Context”The log package provides simple logging. For structured logging, use log/slog (Go 1.21+). Logging is essential for CLI tools to output status, errors, and debug information.
Example
Section titled “Example”Use log and slog to log messages.
Code example
Section titled “Code example”package main
import ( "log" "log/slog" "os")
func main() { // Standard log log.Println("This is a log message")
// Structured logging with slog slog.Info("Server started", "port", 8080) slog.Warn("Disk usage high", "usage", "85%") slog.Error("Failed to connect", "error", "timeout")}Output
Section titled “Output”2025/01/01 12:00:00 This is a log message2025-01-01T12:00:00.000 INFO Server started port=80802025-01-01T12:00:00.000 WARN Disk usage high usage=85%2025-01-01T12:00:00.000 ERROR Failed to connect error=timeout