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

Rotate log file on SIGHUP #116

Merged
merged 1 commit into from
Jun 27, 2023
Merged
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
22 changes: 22 additions & 0 deletions cmd/garm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/cloudbase/garm/apiserver/controllers"
Expand All @@ -37,6 +38,7 @@ import (
"github.com/cloudbase/garm/util"
"github.com/cloudbase/garm/util/appdefaults"
"github.com/cloudbase/garm/websocket"
lumberjack "gopkg.in/natefinch/lumberjack.v2"

"github.com/gorilla/handlers"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -82,6 +84,26 @@ func main() {
log.Fatalf("fetching log writer: %+v", err)
}

// rotate log file on SIGHUP
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGHUP)
go func() {
for {
select {
case <-ctx.Done():
// Daemon is exiting.
return
case <-ch:
// we got a SIGHUP. Rotate log file.
if logger, ok := logWriter.(*lumberjack.Logger); ok {
if err := logger.Rotate(); err != nil {
log.Printf("failed to rotate log file: %v", err)
}
}
}
}
}()

var writers []io.Writer = []io.Writer{
logWriter,
}
Expand Down
1 change: 1 addition & 0 deletions contrib/garm.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ After=multi-user.target
[Service]
Type=simple
ExecStart=/usr/local/bin/garm -config /etc/garm/config.toml
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
RestartSec=5s
User=garm
Expand Down
17 changes: 17 additions & 0 deletions doc/logging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Logging

By default, GARM is logging only on standard output.

If you would like GARM to use a logging file instead, you can use the `log_file` configuration option:

```toml
[default]
# Use this if you'd like to log to a file instead of standard output.
log_file = "/tmp/runner-manager.log"
```

## Rotating log files

If GARM uses a log file, by default it will rotate it when it reaches 500MB or 28 days, whichever comes first.

However, if you want to manually rotate the log file, you can send a `SIGHUP` signal to the GARM process.