From 6d217910a939757d542606b8552c5a4585e499f2 Mon Sep 17 00:00:00 2001 From: Valentin Doreau Date: Mon, 1 Apr 2024 17:20:34 +0200 Subject: [PATCH] feat: added option to write to logfile --- main.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 488b33d..68a92ca 100644 --- a/main.go +++ b/main.go @@ -21,10 +21,20 @@ const INTERVAL = 30 * time.Minute var backupDir = flag.String("backup-dir", "/srv/backups", "Directory where the backups are located") var version_flag = flag.Bool("version", false, "Shows the program version") +var logfile = flag.String("logfile", "-", "Where to write the logs") func main() { flag.Parse() + if *logfile != "-" { + f, err := os.OpenFile(*logfile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0664) + if err != nil { + log.Fatalf("Could not open logfile: %v\n", err) + } + defer f.Close() + log.SetOutput(f) + } + if *version_flag { fmt.Printf("%v\n", VERSION) os.Exit(0) @@ -49,8 +59,7 @@ func RecordMetrics(m Metrics) { for { entries, err := os.ReadDir(*backupDir) if err != nil { - log.Println(err) - os.Exit(1) + log.Fatalln(err) } for _, entry := range entries {