-
Notifications
You must be signed in to change notification settings - Fork 7
/
accesslog.go
210 lines (176 loc) · 5.45 KB
/
accesslog.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package ozone
import (
"context"
"flag"
"fmt"
"io"
"net/http"
"os"
"strings"
"sync"
"github.com/One-com/gone/daemon"
"github.com/One-com/gone/daemon/ctrl"
"github.com/One-com/gone/log"
"github.com/One-com/gone/http/handlers/accesslog"
)
// representing an active accesslog file and the handler logging to it.
type activeAccesslog struct {
name string
filename string
handler accesslog.DynamicLogHandler
writer io.WriteCloser
}
// a global registry of all active accesslogs
var registryLock sync.Mutex
var registry map[string]*activeAccesslog
// a control socket command to control the access logs.
var accessLogControl = newAccessLogCommand(daemon.Log)
func init() {
registry = make(map[string]*activeAccesslog)
ctrl.RegisterCommand("alog", accessLogControl)
}
// ReopenAccessLogFiles opens the configured accesslog files and atomically replaces
// the old filehandles with the new ones - and closes the old file handles.
func ReopenAccessLogFiles() {
registryLock.Lock()
defer registryLock.Unlock()
log.NOTICE("Reopening access log files")
for _, spec := range registry {
// Open the file again
file, err := accessLogFile(spec.filename)
if err != nil {
log.ERROR("Could not reopen accesslog", "err", err, "file", spec.filename)
}
spec.handler.ToggleAccessLog(spec.writer, file) // swap the writer this handler is writing to
spec.writer.Close()
spec.writer = file
}
}
func registerAccessLogFile(name, filename string, handler accesslog.DynamicLogHandler, w io.WriteCloser) {
registryLock.Lock()
defer registryLock.Unlock()
registry[name] = &activeAccesslog{name: name, filename: filename, writer: w, handler: handler}
}
func unregisterAccessLogFile(name string, w io.Writer) {
registryLock.Lock()
defer registryLock.Unlock()
delete(registry, name)
}
// wrapAuditHandler takes an http.Handler and wraps it in a accesslog capable handler which also does a callback to the provided audit function.
// it returns the resulting handler and a function to be called to cleanup when the handler is no longer in use.
func wrapAuditHandler(servername string, h http.Handler, accessLogDest string, mfunc accesslog.AuditFunction) (oh accesslog.DynamicLogHandler, cleanup daemon.CleanupFunc) {
var out io.WriteCloser
var err error
oh = accesslog.NewDynamicLogHandler(h, mfunc)
accessLogControl.RegisterLogHandler(servername, oh)
if accessLogDest != "" {
log.INFO("Opening logfile", "file", accessLogDest)
out, err = accessLogFile(accessLogDest)
if err != nil {
log.CRIT("Unable to open access log", "file", accessLogDest, "err", err)
}
if out == nil {
log.DEBUG("No access log")
}
if f, ok := log.DEBUGok(); ok {
f(fmt.Sprintf("Setting up access log: %s", accessLogDest))
}
registerAccessLogFile(servername, accessLogDest, oh, out)
oh.ToggleAccessLog(nil, out)
cleanup = func() error {
oh.ToggleAccessLog(out, nil)
unregisterAccessLogFile(servername, out)
log.INFO("Closing logfile", "file", accessLogDest)
return out.Close()
}
}
return
}
func accessLogFile(dest string) (file io.WriteCloser, err error) {
if dest == "" {
// None - should not happen
err = fmt.Errorf("Invalid access log specification: \"\"")
return
}
switch dest[0] {
case '|':
err = fmt.Errorf("Unimplemented access log spec: |")
return
case '/': // file
fallthrough
default:
file, err = os.OpenFile(dest, os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModeAppend|0640)
}
return
}
// ----------------------------- Control socket ------------------------------------
// A command turning on/off accesslog for registered HTTP handlers.
type accessLogCommand struct {
mu sync.Mutex
handlers map[string]accesslog.DynamicLogHandler
logger daemon.LoggerFunc
}
func (lc *accessLogCommand) Reset() {
lc.mu.Lock()
defer lc.mu.Unlock()
lc.handlers = make(map[string]accesslog.DynamicLogHandler)
}
func (lc *accessLogCommand) RegisterLogHandler(name string, lh accesslog.DynamicLogHandler) {
lc.mu.Lock()
defer lc.mu.Unlock()
lc.handlers[name] = lh
}
func (lc *accessLogCommand) ShortUsage() (syntax, comment string) {
syntax = "-list | <handler>"
comment = "Output accesslog"
return
}
func (lc *accessLogCommand) Usage(cmd string, w io.Writer) {
fmt.Fprintln(w, cmd, "-list List available handlers")
fmt.Fprintln(w, cmd, "<handler> Output access log for this handler")
}
func (lc *accessLogCommand) Invoke(ctx context.Context, w io.Writer, cmd string, args []string) (async func(), persistent string, err error) {
fs := flag.NewFlagSet("alog", flag.ContinueOnError)
list := fs.Bool("list", false, "List HTTP handlers capable of access log")
fs.SetOutput(w)
err = fs.Parse(args)
if err != nil {
fmt.Fprintf(w, "Syntax error: %s", err.Error())
return
}
if *list && fs.NArg() == 0 {
lc.mu.Lock()
for handler := range lc.handlers {
fmt.Fprintln(w, handler)
}
lc.mu.Unlock()
return
}
args = fs.Args()
var hname string
if len(args) == 1 {
hname = args[0]
}
lc.mu.Lock()
handler, ok := lc.handlers[hname]
lc.mu.Unlock()
if !ok {
fmt.Fprintln(w, "No logging")
return
}
argstr := strings.Join(args, " ")
persistent = strings.Join([]string{cmd, argstr}, " ")
async = func() {
lc.logger(daemon.LvlINFO, "Turning on accesslog")
handler.ToggleAccessLog(nil, w)
<-ctx.Done()
lc.logger(daemon.LvlINFO, "Turning off accesslog")
handler.ToggleAccessLog(w, nil)
}
return
}
func newAccessLogCommand(logger daemon.LoggerFunc) *accessLogCommand {
lc := &accessLogCommand{logger: logger}
lc.Reset()
return lc
}