1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 15:20:54 +00:00

check if SetEscapeHTML exists instead of checking Go version

This commit is contained in:
Waleed Gadelkareem 2017-09-13 22:14:41 +02:00
parent c04d43695c
commit d15e66a4ff
2 changed files with 24 additions and 15 deletions

View File

@ -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

View File

@ -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)
} }