feat: added option to write to logfile

This commit is contained in:
Valentin Doreau 2024-04-01 17:20:34 +02:00
parent a78752b496
commit 6d217910a9
Signed by: vdoreau
GPG key ID: F3E456CF9A14098B

13
main.go
View file

@ -21,10 +21,20 @@ const INTERVAL = 30 * time.Minute
var backupDir = flag.String("backup-dir", "/srv/backups", "Directory where the backups are located") 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 version_flag = flag.Bool("version", false, "Shows the program version")
var logfile = flag.String("logfile", "-", "Where to write the logs")
func main() { func main() {
flag.Parse() 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 { if *version_flag {
fmt.Printf("%v\n", VERSION) fmt.Printf("%v\n", VERSION)
os.Exit(0) os.Exit(0)
@ -49,8 +59,7 @@ func RecordMetrics(m Metrics) {
for { for {
entries, err := os.ReadDir(*backupDir) entries, err := os.ReadDir(*backupDir)
if err != nil { if err != nil {
log.Println(err) log.Fatalln(err)
os.Exit(1)
} }
for _, entry := range entries { for _, entry := range entries {