To implement logging with rotation and log file management in Go, you can use the log
package provided in the Go standard library along with additional libraries such as lumberjack
or rotatelogs
to handle log rotation and file management.
Here is an example of how to achieve it:
Install the required libraries:
go get github.com/natefinch/lumberjack
go get github.com/lestrrat-go/file-rotatelogs
Import the necessary packages:
import (
"io"
"log"
"os"
"path/filepath"
"time"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
)
Initialize the log file and configure its rotation:
func initLogger() {
// Create a file for logging (rotate based on file size)
logPath := "logs/application.log"
logFile := &lumberjack.Logger{
Filename: logPath,
MaxSize: 10, // Maximum log file size in megabytes
MaxBackups: 5, // Maximum number of log file backups
MaxAge: 30, // Maximum number of days to retain log files
Compress: true,
}
// Create an output writer for the logger to write into
logWriter := io.MultiWriter(os.Stdout, logFile)
// Set logger output to the log writer
log.SetOutput(logWriter)
}
Optionally, configure log rotation based on time interval:
func initLoggerWithTimeRotation() {
// Create a file for logging (rotate based on time interval)
logPath := "logs/application.log"
logFile, _ := rotatelogs.New(
filepath.Join("logs", "application.%Y%m%d.log"),
rotatelogs.WithMaxAge(30*24*time.Hour),
rotatelogs.WithRotationTime(24*time.Hour),
)
// Create an output writer for the logger to write into
logWriter := io.MultiWriter(os.Stdout, logFile)
// Set logger output to the log writer
log.SetOutput(logWriter)
}
Start using the logger:
func main() {
initLogger() // or initLoggerWithTimeRotation()
log.Println("Logging with rotation in Go!")
}
With these steps, you will have a logger that logs messages to a file with rotation and file management capabilities. The logs will be written to the specified file, rotated based on the configured size or time interval, and older log files will be automatically deleted according to the MaxBackups
and MaxAge
properties.