Rotate log file on SIGHUP

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 <ibalutoiu@cloudbasesolutions.com>
This commit is contained in:
Ionut Balutoiu 2023-06-27 18:20:25 +03:00
parent 442e76e278
commit 721cbefd6a
3 changed files with 40 additions and 0 deletions

View file

@ -24,6 +24,7 @@ import (
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"syscall"
"time" "time"
"github.com/cloudbase/garm/apiserver/controllers" "github.com/cloudbase/garm/apiserver/controllers"
@ -37,6 +38,7 @@ import (
"github.com/cloudbase/garm/util" "github.com/cloudbase/garm/util"
"github.com/cloudbase/garm/util/appdefaults" "github.com/cloudbase/garm/util/appdefaults"
"github.com/cloudbase/garm/websocket" "github.com/cloudbase/garm/websocket"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
"github.com/gorilla/handlers" "github.com/gorilla/handlers"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -82,6 +84,26 @@ func main() {
log.Fatalf("fetching log writer: %+v", err) 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{ var writers []io.Writer = []io.Writer{
logWriter, logWriter,
} }

View file

@ -5,6 +5,7 @@ After=multi-user.target
[Service] [Service]
Type=simple Type=simple
ExecStart=/usr/local/bin/garm -config /etc/garm/config.toml ExecStart=/usr/local/bin/garm -config /etc/garm/config.toml
ExecReload=/bin/kill -HUP $MAINPID
Restart=always Restart=always
RestartSec=5s RestartSec=5s
User=garm User=garm

17
doc/logging.md Normal file
View file

@ -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.