cmd/ctrld: move log file if existed on app start

Updates #59
This commit is contained in:
Cuong Manh Le
2023-02-23 09:16:34 +07:00
committed by Cuong Manh Le
parent 84fca06c62
commit 82900eeca6

View File

@@ -1,8 +1,8 @@
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"time"
@@ -61,12 +61,16 @@ func initLogging() {
if logFilePath := normalizeLogFilePath(cfg.Service.LogPath); logFilePath != "" {
// Create parent directory if necessary.
if err := os.MkdirAll(filepath.Dir(logFilePath), 0750); err != nil {
fmt.Fprintf(os.Stderr, "failed to create log path: %v", err)
log.Printf("failed to create log path: %v", err)
os.Exit(1)
}
logFile, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_RDWR, os.FileMode(0o600))
// Backup old log file with .1 suffix.
if err := os.Rename(logFilePath, logFilePath+".1"); err != nil && !os.IsNotExist(err) {
log.Printf("could not backup old log file: %v", err)
}
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_RDWR, os.FileMode(0o600))
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create log file: %v", err)
log.Printf("failed to create log file: %v", err)
os.Exit(1)
}
writers = append(writers, logFile)