Skip to content

Commit 721cbef

Browse files
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 <[email protected]>
1 parent 442e76e commit 721cbef

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

cmd/garm/main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"net/http"
2525
"os"
2626
"os/signal"
27+
"syscall"
2728
"time"
2829

2930
"github.com/cloudbase/garm/apiserver/controllers"
@@ -37,6 +38,7 @@ import (
3738
"github.com/cloudbase/garm/util"
3839
"github.com/cloudbase/garm/util/appdefaults"
3940
"github.com/cloudbase/garm/websocket"
41+
lumberjack "gopkg.in/natefinch/lumberjack.v2"
4042

4143
"github.com/gorilla/handlers"
4244
"github.com/gorilla/mux"
@@ -82,6 +84,26 @@ func main() {
8284
log.Fatalf("fetching log writer: %+v", err)
8385
}
8486

87+
// rotate log file on SIGHUP
88+
ch := make(chan os.Signal, 1)
89+
signal.Notify(ch, syscall.SIGHUP)
90+
go func() {
91+
for {
92+
select {
93+
case <-ctx.Done():
94+
// Daemon is exiting.
95+
return
96+
case <-ch:
97+
// we got a SIGHUP. Rotate log file.
98+
if logger, ok := logWriter.(*lumberjack.Logger); ok {
99+
if err := logger.Rotate(); err != nil {
100+
log.Printf("failed to rotate log file: %v", err)
101+
}
102+
}
103+
}
104+
}
105+
}()
106+
85107
var writers []io.Writer = []io.Writer{
86108
logWriter,
87109
}

contrib/garm.service

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ After=multi-user.target
55
[Service]
66
Type=simple
77
ExecStart=/usr/local/bin/garm -config /etc/garm/config.toml
8+
ExecReload=/bin/kill -HUP $MAINPID
89
Restart=always
910
RestartSec=5s
1011
User=garm

doc/logging.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Logging
2+
3+
By default, GARM is logging only on standard output.
4+
5+
If you would like GARM to use a logging file instead, you can use the `log_file` configuration option:
6+
7+
```toml
8+
[default]
9+
# Use this if you'd like to log to a file instead of standard output.
10+
log_file = "/tmp/runner-manager.log"
11+
```
12+
13+
## Rotating log files
14+
15+
If GARM uses a log file, by default it will rotate it when it reaches 500MB or 28 days, whichever comes first.
16+
17+
However, if you want to manually rotate the log file, you can send a `SIGHUP` signal to the GARM process.

0 commit comments

Comments
 (0)