Skip to content

Commit

Permalink
Merge pull request #116 from ionutbalutoiu/sighup-log-rotate
Browse files Browse the repository at this point in the history
Rotate log file on SIGHUP
  • Loading branch information
gabriel-samfira authored Jun 27, 2023
2 parents c45bd1d + 721cbef commit aa44bf4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
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.

0 comments on commit aa44bf4

Please sign in to comment.