Skip to content

Commit

Permalink
Redirect STDERR to error log
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Oct 28, 2024
1 parent abe4537 commit 1aa4b4d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
20 changes: 20 additions & 0 deletions fileserver/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/haiwen/seafile-server/fileserver/repomgr"
"github.com/haiwen/seafile-server/fileserver/searpc"
"github.com/haiwen/seafile-server/fileserver/share"
"github.com/haiwen/seafile-server/fileserver/utils"
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
"gopkg.in/ini.v1"
Expand Down Expand Up @@ -365,6 +366,17 @@ func main() {
logFp = fp
log.SetOutput(fp)
}

if absLogFile != "" && !logToStdout {
errorLogFile := filepath.Join(filepath.Dir(absLogFile), "fileserver-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)
}
utils.Dup(int(fp.Fd()), int(os.Stderr.Fd()))
// We need to close the old fp, because it has beed duped.
fp.Close()
}
// When logFile is "-", use default output (StdOut)

level, err := log.ParseLevel(option.LogLevel)
Expand Down Expand Up @@ -453,6 +465,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)
}
utils.Dup(int(errFp.Fd()), int(os.Stderr.Fd()))
errFp.Close()
}

var rpcclient *searpc.Client
Expand Down
11 changes: 11 additions & 0 deletions fileserver/utils/dup3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build linux && arm64

package utils

import (
"syscall"
)

func Dup(from, to int) error {
return syscall.Dup3(from, to, 0)
}
11 changes: 11 additions & 0 deletions notification-server/dup2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build !(linux && arm64)

package main

import (
"syscall"
)

func Dup(from, to int) error {
return syscall.Dup2(from, to)
}
11 changes: 11 additions & 0 deletions notification-server/dup3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build linux && arm64

package main

import (
"syscall"
)

func Dup(from, to int) error {
return syscall.Dup3(from, to, 0)
}
18 changes: 18 additions & 0 deletions notification-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ func main() {
log.SetOutput(fp)
}

if absLogFile != "" && !logToStdout {
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)
}
Dup(int(fp.Fd()), int(os.Stderr.Fd()))
fp.Close()
}

if err := loadJwtPrivateKey(); err != nil {
log.Fatalf("Failed to read config: %v", err)
}
Expand Down Expand Up @@ -256,6 +266,14 @@ func logRotate() {
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)
}
Dup(int(errFp.Fd()), int(os.Stderr.Fd()))
errFp.Close()
}

func newHTTPRouter() *mux.Router {
Expand Down

0 comments on commit 1aa4b4d

Please sign in to comment.