Skip to content

Commit 64266ae

Browse files
committed
Add logrotate example
Example template that uses a comma-separated value of log files stored in a container environment variable named "LOG_FILES." The value is split on the comma character and then each log file is listed in the logrotate config file through the hosts file system. Each container's docker JSON log file is also listed for rotation to prevent it from growin indefinitely.
1 parent 012a5ae commit 64266ae

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

template.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io/ioutil"
88
"os"
99
"path/filepath"
10+
"strings"
1011
"text/template"
1112
)
1213

@@ -33,6 +34,7 @@ func generateFile(config Config, containers []*RuntimeContainer) bool {
3334
tmpl, err := template.New(filepath.Base(templatePath)).Funcs(template.FuncMap{
3435
"contains": contains,
3536
"groupBy": groupBy,
37+
"split": strings.Split,
3638
}).ParseFiles(templatePath)
3739
if err != nil {
3840
panic(err)

templates/logrotate.tmpl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{{ range $index, $value := $ }}
2+
{{ $logs := $value.Env.LOG_FILES }}
3+
{{ if $logs }}
4+
{{ range $index, $logfile := split $logs "," }}
5+
/var/lib/docker/containers/{{ $value.ID }}/root{{ $logfile }}{{ end}}
6+
{
7+
daily
8+
missingok
9+
rotate 52
10+
compress
11+
delaycompress
12+
notifempty
13+
create 644 root root
14+
}
15+
{{ end }}
16+
/var/lib/docker/containers/{{ $value.ID }}/{{ $value.ID }}-json.log
17+
{
18+
daily
19+
missingok
20+
rotate 7
21+
compress
22+
delaycompress
23+
notifempty
24+
create 644 root root
25+
}
26+
{{ end }}
27+

0 commit comments

Comments
 (0)