/
accessLog.go
116 lines (101 loc) · 2.75 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
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package model
import (
"regexp"
"strings"
"github.com/admpub/nging/application/dbschema"
"github.com/admpub/nging/application/model/base"
"github.com/webx-top/echo"
)
func NewAccessLog(ctx echo.Context) *AccessLog {
return &AccessLog{
AccessLog: &dbschema.AccessLog{},
base: base.New(ctx),
}
}
type AccessLog struct {
*dbschema.AccessLog
base *base.Base
}
// Parse 解析单行字符串到日志对象
func (l *AccessLog) Parse(line string, args ...interface{}) (err error) {
line = strings.TrimRight(line, " \r\n")
if len(line) == 0 {
return nil
}
size := len(args)
if size > 0 {
var layout string
if size > 1 {
layout, _ = args[1].(string)
}
switch v := args[0].(type) {
case *regexp.Regexp:
err = l.parseWithPattern(line, v)
case string:
switch v {
//case `nginx`:
default:
err = l.parseCaddy(line, layout)
}
}
} else {
err = l.parseCaddy(line, ``)
}
return
}
func (m *AccessLog) ToLite() *AccessLogLite {
a := &AccessLogLite{
Date: m.TimeLocal,
OS: ``,
Brower: m.BrowerName,
Region: ``,
Type: m.BrowerType,
Version: m.Version,
User: m.User,
Method: m.Method,
Scheme: m.Scheme,
URI: m.Uri,
Referer: m.Referer,
BodyBytes: m.BodyBytes,
Elapsed: m.Elapsed,
StatusCode: m.StatusCode,
UserAgent: m.UserAgent,
}
return a
}
func (m *AccessLog) ToMap() echo.Store {
data := echo.Store{}
data.Set(`TimeLocal`, m.TimeLocal)
data.Set(`RemoteAddr`, m.RemoteAddr)
data.Set(`XRealIP`, m.XRealIp)
data.Set(`XForwardFor`, m.XForwardFor)
data.Set(`LocalAddr`, m.LocalAddr)
data.Set(`User`, m.User)
data.Set(`Version`, m.Version)
data.Set(`Referer`, m.Referer)
data.Set(`UserAgent`, m.UserAgent)
data.Set(`Path`, m.Uri)
data.Set(`Method`, m.Method)
data.Set(`Scheme`, m.Scheme)
data.Set(`BrowerName`, m.BrowerName)
data.Set(`BrowerType`, m.BrowerType)
data.Set(`BytesSent`, m.BodyBytes)
data.Set(`StatusCode`, m.StatusCode)
data.Set(`UpstreamTime`, m.Elapsed)
data.Set(`RequestTime`, m.Elapsed)
return data
}