From b17cde886f4cb20c9aa2ad1eae9ac3f30205d1a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E8=B5=AB=E7=84=B6?= Date: Wed, 6 Mar 2024 11:43:37 +0800 Subject: [PATCH 1/3] Support error log rotate and notification log rotate --- fileserver/fileserver.go | 13 ++++++++++-- notification-server/server.go | 40 +++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/fileserver/fileserver.go b/fileserver/fileserver.go index 9b84ec90..d47af355 100644 --- a/fileserver/fileserver.go +++ b/fileserver/fileserver.go @@ -363,6 +363,7 @@ func main() { log.Fatalf("Failed to open or create error log file: %v", err) } syscall.Dup3(int(fp.Fd()), int(os.Stderr.Fd()), 0) + fp.Close() } repomgr.Init(seafileDB) @@ -390,7 +391,7 @@ func main() { router := newHTTPRouter() go handleSignals() - go handleUser1Singal() + go handleUser1Signal() log.Print("Seafile file server started.") @@ -409,7 +410,7 @@ func handleSignals() { os.Exit(0) } -func handleUser1Singal() { +func handleUser1Signal() { signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, syscall.SIGUSR1) <-signalChan @@ -433,6 +434,14 @@ func logRotate() { logFp.Close() logFp = fp } + + errorLogFile := filepath.Join(filepath.Dir(absLogFile), "fileserver-error.log") + errFp, err := os.OpenFile(errorLogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) + if err != nil { + log.Fatalf("Failed to reopen fileserver error log: %v", err) + } + syscall.Dup3(int(errFp.Fd()), int(os.Stderr.Fd()), 0) + errFp.Close() } var rpcclient *searpc.Client diff --git a/notification-server/server.go b/notification-server/server.go index de938b40..c599476d 100644 --- a/notification-server/server.go +++ b/notification-server/server.go @@ -8,6 +8,7 @@ import ( "io" "net/http" "os" + "os/signal" "path/filepath" "strings" "syscall" @@ -26,6 +27,7 @@ var logFile, absLogFile string var privateKey string var host string var port uint32 +var logFp *os.File var ccnetDB *sql.DB @@ -171,6 +173,7 @@ func main() { if err != nil { log.Fatalf("Failed to open or create log file: %v", err) } + logFp = fp log.SetOutput(fp) } else if logFile != "-" { absLogFile, err = filepath.Abs(logFile) @@ -181,6 +184,7 @@ func main() { if err != nil { log.Fatalf("Failed to open or create log file: %v", err) } + logFp = fp log.SetOutput(fp) } @@ -191,6 +195,7 @@ func main() { log.Fatalf("Failed to open or create error log file: %v", err) } syscall.Dup3(int(fp.Fd()), int(os.Stderr.Fd()), 0) + fp.Close() } loadNotifConfig() @@ -198,6 +203,8 @@ func main() { Init() + go handleUser1Signal() + router := newHTTPRouter() log.Info("notification server started.") @@ -209,6 +216,39 @@ func main() { } } +func handleUser1Signal() { + signalChan := make(chan os.Signal, 1) + signal.Notify(signalChan, syscall.SIGUSR1) + <-signalChan + + for { + select { + case <-signalChan: + logRotate() + } + } +} + +func logRotate() { + fp, err := os.OpenFile(absLogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) + if err != nil { + log.Fatalf("Failed to reopen notification log: %v", err) + } + log.SetOutput(fp) + if logFp != nil { + logFp.Close() + logFp = fp + } + + errorLogFile := filepath.Join(filepath.Dir(absLogFile), "notification_server_error.log") + errFp, err := os.OpenFile(errorLogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) + if err != nil { + log.Fatalf("Failed to reopen notification error log: %v", err) + } + syscall.Dup3(int(errFp.Fd()), int(os.Stderr.Fd()), 0) + errFp.Close() +} + func newHTTPRouter() *mux.Router { r := mux.NewRouter() r.Handle("/", appHandler(messageCB)) From b427b3f919995d5f714ff1c669b88df93806c51b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E8=B5=AB=E7=84=B6?= Date: Wed, 6 Mar 2024 11:51:16 +0800 Subject: [PATCH 2/3] Add comment --- fileserver/fileserver.go | 1 + 1 file changed, 1 insertion(+) diff --git a/fileserver/fileserver.go b/fileserver/fileserver.go index d47af355..a1d044f6 100644 --- a/fileserver/fileserver.go +++ b/fileserver/fileserver.go @@ -363,6 +363,7 @@ func main() { log.Fatalf("Failed to open or create error log file: %v", err) } syscall.Dup3(int(fp.Fd()), int(os.Stderr.Fd()), 0) + // We need to close the old fp, because it has beed duped. fp.Close() } From 9b5aff1aa3f25dd0e8dca531959158552437c165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E8=B5=AB=E7=84=B6?= Date: Tue, 12 Mar 2024 09:34:58 +0800 Subject: [PATCH 3/3] Modify log name --- notification-server/server.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/notification-server/server.go b/notification-server/server.go index c599476d..a490371f 100644 --- a/notification-server/server.go +++ b/notification-server/server.go @@ -168,7 +168,7 @@ func main() { } if logFile == "" { - absLogFile = filepath.Join(configDir, "notification.log") + absLogFile = filepath.Join(configDir, "notification-server.log") fp, err := os.OpenFile(absLogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) if err != nil { log.Fatalf("Failed to open or create log file: %v", err) @@ -189,7 +189,7 @@ func main() { } if absLogFile != "" { - errorLogFile := filepath.Join(filepath.Dir(absLogFile), "notification_server_error.log") + errorLogFile := filepath.Join(filepath.Dir(absLogFile), "notification-server-error.log") fp, err := os.OpenFile(errorLogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) if err != nil { log.Fatalf("Failed to open or create error log file: %v", err) @@ -240,7 +240,7 @@ func logRotate() { logFp = fp } - errorLogFile := filepath.Join(filepath.Dir(absLogFile), "notification_server_error.log") + errorLogFile := filepath.Join(filepath.Dir(absLogFile), "notification-server-error.log") errFp, err := os.OpenFile(errorLogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) if err != nil { log.Fatalf("Failed to reopen notification error log: %v", err)