Skip to content

Commit

Permalink
Rotate log file on SIGHUP
Browse files Browse the repository at this point in the history
Add functionality to rotate the log file when `SIGHUP` signal is received.

Also, a doc is added with few details about logging.

Signed-off-by: Ionut Balutoiu <[email protected]>
  • Loading branch information
ionutbalutoiu committed Jun 27, 2023
1 parent 442e76e commit 5f77279
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
15 changes: 15 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,19 @@ func main() {
log.Fatalf("fetching log writer: %+v", err)
}

if cfg.Default.LogFile != "" {
// rotate log file on SIGHUP
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGHUP)
go func() {
for range ch {
if err := logWriter.(*lumberjack.Logger).Rotate(); err != nil {
log.Printf("failed to rotate log file: %v\n", err)
}
}
}()
}

var writers []io.Writer = []io.Writer{
logWriter,
}
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 5f77279

Please sign in to comment.