Merge pull request #116 from ionutbalutoiu/sighup-log-rotate

Rotate log file on SIGHUP
This commit is contained in:
Gabriel 2023-06-27 20:12:55 +03:00 committed by GitHub
commit aa44bf4f98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 0 deletions

View file

@ -24,6 +24,7 @@ import (
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/cloudbase/garm/apiserver/controllers"
@ -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"
@ -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,
}

View file

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

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.