forked from lalluviamola/web-blog
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloghttp.go
95 lines (84 loc) · 2.37 KB
/
loghttp.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
package main
import (
"context"
"net/http"
"os"
"strings"
"time"
"github.com/kjk/common/httplogger"
"github.com/kjk/minioutil"
)
var (
httpLogger *httplogger.Logger
)
func logHTTPReq(r *http.Request, code int, size int64, dur time.Duration) {
if strings.HasPrefix(r.URL.Path, "/ping") {
return
}
if code >= 400 {
// make 400 stand out more in logs
logf(ctx(), "%s %d %s %s in %s\n", " ", code, r.RequestURI, formatSize(size), dur)
} else {
logf(ctx(), "%s %d %s %s in %s\n", r.Method, code, r.RequestURI, formatSize(size), dur)
}
// no need to log redirects
if code >= 300 && code < 400 {
return
}
err := httpLogger.LogReq(r, code, size, dur)
if err != nil {
logerrf(ctx(), "httpLogger.LogReq() failed with '%s'\n", err)
}
}
// upload httplog-2021-10-06_01.txt as
// apps/${app}/httplog/2021/10-06/2021-10-06_01.txt.br
func uploadCompressedHTTPLog(app, path string) {
timeStart := time.Now()
mc := newMinioSpacesClient()
remotePath := httplogger.RemotePathFromFilePath(app, path)
if remotePath == "" {
logf(ctx(), "uploadCompressedHTTPLog: remotePathFromFilePath() failed for '%s'\n", path)
return
}
_, err := mc.UploadFileBrotliCompressed(remotePath, path, true)
if err != nil {
logerrf(ctx(), "uploadCompressedHTTPLog: minioUploadFilePublic() failed with '%s'\n", err)
return
}
logf(ctx(), "uploadCompressedHTTPLog: uploaded '%s' as '%s' in %s\n", path, remotePath, time.Since(timeStart))
}
func OpenHTTPLog(app string) func() {
panicIf(app == "")
dir := "logs"
must(os.MkdirAll(dir, 0755))
didRotate := func(path string) {
canUpload := hasSpacesCreds() && !isWindows()
logf(ctx(), "didRotateHTTPLog: '%s', hasSpacesCreds: %v\n", path, canUpload)
if !canUpload {
return
}
go uploadCompressedHTTPLog(app, path)
}
var err error
httpLogger, err = httplogger.New(dir, didRotate)
must(err)
// TODO: should I change filerotate so that it opens the file immedaitely?
logf(context.Background(), "opened http log file\n")
return func() {
httpLogger.Close()
}
}
func hasSpacesCreds() bool {
return os.Getenv("SPACES_KEY") != "" && os.Getenv("SPACES_SECRET") != ""
}
func newMinioSpacesClient() *minioutil.Client {
config := &minioutil.Config{
Bucket: "kjklogs",
Access: os.Getenv("SPACES_KEY"),
Secret: os.Getenv("SPACES_SECRET"),
Endpoint: "nyc3.digitaloceanspaces.com",
}
mc, err := minioutil.New(config)
must(err)
return mc
}