forked from caddyserver/caddy
/
setup.go
102 lines (83 loc) · 2.07 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package log
import (
"strings"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
// setup sets up the logging middleware.
func setup(c *caddy.Controller) error {
rules, err := logParse(c)
if err != nil {
return err
}
for _, rule := range rules {
for _, entry := range rule.Entries {
entry.Log.Attach(c)
}
}
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
return Logger{Next: next, Rules: rules, ErrorFunc: httpserver.DefaultErrorFunc}
})
return nil
}
func logParse(c *caddy.Controller) ([]*Rule, error) {
var rules []*Rule
for c.Next() {
args := c.RemainingArgs()
var logRoller *httpserver.LogRoller
logRoller = httpserver.DefaultLogRoller()
for c.NextBlock() {
what := c.Val()
where := c.RemainingArgs()
// only support roller related options inside a block
if !httpserver.IsLogRollerSubdirective(what) {
return nil, c.ArgErr()
}
if err := httpserver.ParseRoller(logRoller, what, where...); err != nil {
return nil, err
}
}
path := "/"
format := DefaultLogFormat
output := DefaultLogFilename
switch len(args) {
case 0:
// nothing to change
case 1:
// Only an output file specified
output = args[0]
case 2, 3:
// Path scope, output file, and maybe a format specified
path = args[0]
output = args[1]
if len(args) > 2 {
format = strings.Replace(args[2], "{common}", CommonLogFormat, -1)
format = strings.Replace(format, "{combined}", CombinedLogFormat, -1)
}
default:
// Maximum number of args in log directive is 3.
return nil, c.ArgErr()
}
rules = appendEntry(rules, path, &Entry{
Log: &httpserver.Logger{
Output: output,
Roller: logRoller,
},
Format: format,
})
}
return rules, nil
}
func appendEntry(rules []*Rule, pathScope string, entry *Entry) []*Rule {
for _, rule := range rules {
if rule.PathScope == pathScope {
rule.Entries = append(rule.Entries, entry)
return rules
}
}
rules = append(rules, &Rule{
PathScope: pathScope,
Entries: []*Entry{entry},
})
return rules
}