-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlog.go
94 lines (78 loc) · 2.02 KB
/
log.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
// Copyright (C) 2012 Numerotron Inc.
// Use of this source code is governed by an MIT-style license
// that can be found in the LICENSE file.
package bingo
import (
"fmt"
"log"
"net/http"
"os"
"time"
)
var AccessLogFilename = "/tmp/access.log"
var ErrorLogFilename = "/tmp/error.log"
var alf *os.File
var elf *os.File
var accessLog *log.Logger
var errorLog *log.Logger
var qaccess chan string
var qerror chan string
func init() {
qaccess = make(chan string, 1000)
qerror = make(chan string, 1000)
var err error
alf, err = os.OpenFile(AccessLogFilename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0664)
if err != nil {
panic(fmt.Sprintf("couldn't open access log file: %s", err))
}
accessLog = log.New(alf, "", log.LstdFlags)
elf, err = os.OpenFile(ErrorLogFilename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0664)
if err != nil {
panic("couldn't open error log file")
}
errorLog = log.New(elf, "", log.LstdFlags)
go writeAccess()
go writeErrors()
}
func LogAccess(req *http.Request, elapsed time.Duration) {
// qaccess <- fmt.Sprintf("%s \"%s %s %s\" %dms", req.RemoteAddr, req.Method, req.RequestURI, req.Proto, elapsed / time.Millisecond)
qaccess <- fmt.Sprintf("%s \"%s %s %s\" %s", req.RemoteAddr, req.Method, req.RequestURI, req.Proto, elapsed)
}
func LogError(req *http.Request, err *AppError) {
qerror <- fmt.Sprintf("[error] [client %s] %q %s", req.RemoteAddr, req.RequestURI, err.Message)
}
func writeAccess() {
for x := range qaccess {
accessLog.Println(x)
}
}
func writeErrors() {
for x := range qerror {
errorLog.Println(x)
}
}
func logCleanup() {
close(qaccess)
close(qerror)
alf.Close()
elf.Close()
}
func logReload() {
a, err := os.OpenFile(AccessLogFilename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0664)
if err != nil {
return
}
accessLog = log.New(a, "", log.LstdFlags)
alf.Close()
alf = a
b, err := os.OpenFile(ErrorLogFilename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0664)
if err != nil {
return
}
errorLog = log.New(b, "", log.LstdFlags)
elf.Close()
elf = b
}
func LogReload() {
logReload()
}