Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logrotate support: reopen log file on SIGHUP #1120

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ package cmd
import (
"errors"
"fmt"
"github.com/stripe/stripe-go/v74"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
"heckel.io/ntfy/v2/log"
"heckel.io/ntfy/v2/server"
"heckel.io/ntfy/v2/user"
"heckel.io/ntfy/v2/util"
"io/fs"
"math"
"net"
Expand All @@ -22,6 +15,14 @@ import (
"strings"
"syscall"
"time"

"github.com/stripe/stripe-go/v74"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
"heckel.io/ntfy/v2/log"
"heckel.io/ntfy/v2/server"
"heckel.io/ntfy/v2/user"
"heckel.io/ntfy/v2/util"
)

func init() {
Expand Down Expand Up @@ -492,5 +493,8 @@ func reloadLogLevel(inputSource altsrc.InputSourceContext) error {
} else {
log.Info("Log level is %v", strings.ToUpper(newLevelStr))
}
if err := log.Reopen(); err != nil {
return fmt.Errorf("cannot reopen log file: %s", err.Error())
}
return nil
}
17 changes: 17 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ func SetOutput(w io.Writer) {
log.SetOutput(output)
}

func Reopen() error {
mu.Lock()
defer mu.Unlock()
if f, ok := output.(*os.File); ok {
logFile := f.Name()
f.Close()

w, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
return err
}
output = &peekLogWriter{w}
log.SetOutput(output)
}
return nil
}

// File returns the log file, if any, or an empty string otherwise
func File() string {
mu.RLock()
Expand Down