mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 20:30:55 +00:00
check if SetEscapeHTML exists instead of checking Go version
This commit is contained in:
parent
c04d43695c
commit
d15e66a4ff
@ -35,7 +35,6 @@ install:
|
|||||||
- go get github.com/gogo/protobuf/proto
|
- go get github.com/gogo/protobuf/proto
|
||||||
- go get github.com/Knetic/govaluate
|
- go get github.com/Knetic/govaluate
|
||||||
- go get github.com/casbin/casbin
|
- go get github.com/casbin/casbin
|
||||||
- go get github.com/mcuadros/go-version
|
|
||||||
- go get -u honnef.co/go/tools/cmd/gosimple
|
- go get -u honnef.co/go/tools/cmd/gosimple
|
||||||
- go get -u github.com/mdempsky/unconvert
|
- go get -u github.com/mdempsky/unconvert
|
||||||
- go get -u github.com/gordonklaus/ineffassign
|
- go get -u github.com/gordonklaus/ineffassign
|
||||||
|
@ -19,14 +19,12 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"time"
|
"time"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mcuadros/go-version"
|
|
||||||
"runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ApacheFormatPattern = "%s - - [%s] \"%s %d %d\" %f %s %s\n"
|
apacheFormatPattern = "%s - - [%s] \"%s %d %d\" %f %s %s\n"
|
||||||
ApacheFormat = "APACHE_FORMAT"
|
apacheFormat = "APACHE_FORMAT"
|
||||||
JsonFormat = "JSON_FORMAT"
|
jsonFormat = "JSON_FORMAT"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AccessLogRecord struct {
|
type AccessLogRecord struct {
|
||||||
@ -44,31 +42,43 @@ type AccessLogRecord struct {
|
|||||||
RemoteUser string `json:"remote_user"`
|
RemoteUser string `json:"remote_user"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *AccessLogRecord) JSON() ([]byte, error) {
|
func (r *AccessLogRecord) json() ([]byte, error) {
|
||||||
buffer := &bytes.Buffer{}
|
buffer := &bytes.Buffer{}
|
||||||
encoder := json.NewEncoder(buffer)
|
encoder := json.NewEncoder(buffer)
|
||||||
currentGoVersion := version.Normalize(runtime.Version()[2:])
|
disableEscapeHTML(encoder)
|
||||||
if version.Compare("1.7", currentGoVersion, "<") {
|
|
||||||
encoder.SetEscapeHTML(false)
|
|
||||||
}
|
|
||||||
err := encoder.Encode(r)
|
err := encoder.Encode(r)
|
||||||
return buffer.Bytes(), err
|
return buffer.Bytes(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func disableEscapeHTML(i interface{}) {
|
||||||
|
e, ok := i.(interface {
|
||||||
|
SetEscapeHTML(bool)
|
||||||
|
});
|
||||||
|
if ok {
|
||||||
|
e.SetEscapeHTML(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func AccessLog(r *AccessLogRecord, format string) {
|
func AccessLog(r *AccessLogRecord, format string) {
|
||||||
var msg string
|
var msg string
|
||||||
|
s
|
||||||
|
switch format {
|
||||||
|
|
||||||
if format == ApacheFormat {
|
case apacheFormat:
|
||||||
timeFormatted := r.RequestTime.Format("02/Jan/2006 03:04:05")
|
timeFormatted := r.RequestTime.Format("02/Jan/2006 03:04:05")
|
||||||
msg = fmt.Sprintf(ApacheFormatPattern, r.RemoteAddr, timeFormatted, r.Request, r.Status, r.BodyBytesSent,
|
msg = fmt.Sprintf(apacheFormatPattern, r.RemoteAddr, timeFormatted, r.Request, r.Status, r.BodyBytesSent,
|
||||||
r.ElapsedTime.Seconds(), r.HttpReferrer, r.HttpUserAgent)
|
r.ElapsedTime.Seconds(), r.HttpReferrer, r.HttpUserAgent)
|
||||||
} else {
|
case jsonFormat:
|
||||||
jsonData, err := r.JSON()
|
fallthrough
|
||||||
|
default:
|
||||||
|
jsonData, err := r.json()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg = fmt.Sprintf(`{"Error": "%s"}`, err)
|
msg = fmt.Sprintf(`{"Error": "%s"}`, err)
|
||||||
} else {
|
} else {
|
||||||
msg = string(jsonData)
|
msg = string(jsonData)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
beeLogger.Debug(msg)
|
beeLogger.Debug(msg)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user