Skip to content

Commit

Permalink
add support to use lumberjack for log rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
sushanth0910 committed Apr 10, 2024
1 parent 3cf5251 commit bc0b7e4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
34 changes: 28 additions & 6 deletions images/build/go-runner/go-runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@ import (
"os/signal"
"strings"
"syscall"

"gopkg.in/natefinch/lumberjack.v2"
)

var (
logFilePath = flag.String("log-file", "", "If non-empty, save stdout to this file")
alsoToStdOut = flag.Bool("also-stdout", false, "useful with log-file, log to standard output as well as the log file")
redirectStderr = flag.Bool("redirect-stderr", true, "treat stderr same as stdout")
logFilePath = flag.String("log-file", "", "If non-empty, save stdout to this file")
alsoToStdOut = flag.Bool("also-stdout", false, "useful with log-file, log to standard output as well as the log file")
redirectStderr = flag.Bool("redirect-stderr", true, "treat stderr same as stdout")
logRotate = flag.Bool("log-rotate", false, "allow go-runner to handle log rotation")
logMaxSize = flag.Int("log-maxsize", 100, "The maximum size in megabytes of the log file before it gets rotated")
logMaxAge = flag.Int("log-maxage", 0, "The maximum number of days to retain old log files based on the timestamp encoded in their filename")
logMaxBackups = flag.Int("log-maxbackup", 1, "The maximum number of old log files to retain. Setting a value of 0 will mean there's no restriction on the number of files")
logBackupCompress = flag.Bool("log-compress", false, "If set, the rotated log files will be compressed using gzip")
)

func main() {
Expand All @@ -46,6 +53,7 @@ func configureAndRun() error {
var (
outputStream io.Writer = os.Stdout
errStream io.Writer = os.Stderr
logFile io.Writer
)

args := flag.Args()
Expand All @@ -54,9 +62,23 @@ func configureAndRun() error {
}

if logFilePath != nil && *logFilePath != "" {
logFile, err := os.OpenFile(*logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return fmt.Errorf("failed to create log file %v: %w", *logFilePath, err)

if *logRotate {
logger := &lumberjack.Logger{
Filename: *logFilePath,
MaxSize: *logMaxSize, // megabytes
MaxBackups: *logMaxBackups, // log file retain count
MaxAge: *logMaxAge, // days
Compress: *logBackupCompress, // disabled by default
}
defer logger.Close()
logFile = logger
} else {
file, err := os.OpenFile(*logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return fmt.Errorf("failed to create log file %v: %w", *logFilePath, err)
}
logFile = file
}
if *alsoToStdOut {
outputStream = io.MultiWriter(os.Stdout, logFile)
Expand Down
2 changes: 2 additions & 0 deletions images/build/go-runner/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module k8s.io/release/images/build/go-runner

go 1.21

require gopkg.in/natefinch/lumberjack.v2 v2.2.1
2 changes: 2 additions & 0 deletions images/build/go-runner/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=

0 comments on commit bc0b7e4

Please sign in to comment.