mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
add logging for error in ghttp.Request
This commit is contained in:
@ -81,28 +81,29 @@ func (r *Request) GetVar(key string, def ... interface{}) gvar.VarRead {
|
||||
|
||||
// 获取原始请求输入二进制。
|
||||
func (r *Request) GetRaw() []byte {
|
||||
err := error(nil)
|
||||
if r.rawContent == nil {
|
||||
r.rawContent, _ = ioutil.ReadAll(r.Body)
|
||||
r.rawContent, err = ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
r.Error("error reading request body: ", err)
|
||||
}
|
||||
}
|
||||
return r.rawContent
|
||||
}
|
||||
|
||||
// 获取原始请求输入字符串。
|
||||
func (r *Request) GetRawString() string {
|
||||
if r.rawContent == nil {
|
||||
r.rawContent, _ = ioutil.ReadAll(r.Body)
|
||||
}
|
||||
return string(r.rawContent)
|
||||
return string(r.GetRaw())
|
||||
}
|
||||
|
||||
// 获取原始json请求输入字符串,并解析为json对象
|
||||
func (r *Request) GetJson() *gjson.Json {
|
||||
data := r.GetRaw()
|
||||
if data != nil {
|
||||
if len(data) > 0 {
|
||||
if j, err := gjson.DecodeToJson(data); err == nil {
|
||||
return j
|
||||
} else {
|
||||
panic(err)
|
||||
r.Error(err, ": ", string(data))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
14
g/net/ghttp/ghttp_request_log.go
Normal file
14
g/net/ghttp/ghttp_request_log.go
Normal file
@ -0,0 +1,14 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package ghttp
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 打印error日志
|
||||
func (r *Request) Error(value... interface{}) {
|
||||
r.Server.handleErrorLog(fmt.Sprint(value...), r)
|
||||
}
|
||||
@ -169,7 +169,7 @@ func (r *Request) GetPostMap(def...map[string]string) map[string]string {
|
||||
}
|
||||
|
||||
// 将所有的request参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系
|
||||
func (r *Request) GetPostToStruct(object interface{}, mapping...map[string]string) {
|
||||
func (r *Request) GetPostToStruct(object interface{}, mapping...map[string]string) error {
|
||||
tagmap := r.getStructParamsTagMap(object)
|
||||
if len(mapping) > 0 {
|
||||
for k, v := range mapping[0] {
|
||||
@ -180,5 +180,5 @@ func (r *Request) GetPostToStruct(object interface{}, mapping...map[string]strin
|
||||
for k, v := range r.GetPostMap() {
|
||||
params[k] = v
|
||||
}
|
||||
gconv.Struct(params, object, tagmap)
|
||||
return gconv.Struct(params, object, tagmap)
|
||||
}
|
||||
@ -177,7 +177,7 @@ func (r *Request) GetQueryMap(def ... map[string]string) map[string]string {
|
||||
}
|
||||
|
||||
// 将所有的get参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系
|
||||
func (r *Request) GetQueryToStruct(object interface{}, mapping...map[string]string) {
|
||||
func (r *Request) GetQueryToStruct(object interface{}, mapping...map[string]string) error {
|
||||
tagmap := r.getStructParamsTagMap(object)
|
||||
if len(mapping) > 0 {
|
||||
for k, v := range mapping[0] {
|
||||
@ -188,5 +188,5 @@ func (r *Request) GetQueryToStruct(object interface{}, mapping...map[string]stri
|
||||
for k, v := range r.GetQueryMap() {
|
||||
params[k] = v
|
||||
}
|
||||
gconv.Struct(params, object, tagmap)
|
||||
return gconv.Struct(params, object, tagmap)
|
||||
}
|
||||
@ -162,7 +162,7 @@ func (r *Request) GetRequestMap(def...map[string]string) map[string]string {
|
||||
}
|
||||
|
||||
// 将所有的request参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系
|
||||
func (r *Request) GetRequestToStruct(object interface{}, mapping...map[string]string) {
|
||||
func (r *Request) GetRequestToStruct(object interface{}, mapping...map[string]string) error {
|
||||
tagmap := r.getStructParamsTagMap(object)
|
||||
if len(mapping) > 0 {
|
||||
for k, v := range mapping[0] {
|
||||
@ -178,6 +178,6 @@ func (r *Request) GetRequestToStruct(object interface{}, mapping...map[string]st
|
||||
params = j.ToMap()
|
||||
}
|
||||
}
|
||||
gconv.Struct(params, object, tagmap)
|
||||
return gconv.Struct(params, object, tagmap)
|
||||
}
|
||||
|
||||
|
||||
@ -10,6 +10,7 @@ package ghttp
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
)
|
||||
|
||||
// 处理服务错误信息,主要是panic,http请求的status由access log进行管理
|
||||
@ -46,7 +47,11 @@ func (s *Server) handleErrorLog(error interface{}, r *Request) {
|
||||
|
||||
// 错误日志信息
|
||||
content := fmt.Sprintf(`%v, "%s %s %s %s"`, error, r.Method, r.Host, r.URL.String(), r.Proto)
|
||||
content += fmt.Sprintf(` %.3f`, float64(r.LeaveTime - r.EnterTime)/1000)
|
||||
if r.LeaveTime > r.EnterTime {
|
||||
content += fmt.Sprintf(` %.3f`, float64(r.LeaveTime - r.EnterTime)/1000)
|
||||
} else {
|
||||
content += fmt.Sprintf(` %.3f`, float64(gtime.Microsecond() - r.EnterTime)/1000)
|
||||
}
|
||||
content += fmt.Sprintf(`, %s, "%s", "%s"`, r.GetClientIp(), r.Referer(), r.UserAgent())
|
||||
|
||||
if s.logger.GetPath() == "" {
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.BindHandler("/log/handler", func(r *ghttp.Request){
|
||||
r.Response.WriteStatus(http.StatusNotFound, "文件找不到了")
|
||||
})
|
||||
s.SetAccessLogEnabled(true)
|
||||
s.SetErrorLogEnabled(true)
|
||||
//s.SetLogHandler(func(r *ghttp.Request, error ...interface{}) {
|
||||
// if len(error) > 0 {
|
||||
// // 如果是错误日志
|
||||
// fmt.Println("错误产生了:", error[0])
|
||||
// }
|
||||
// // 这里是请求日志
|
||||
// fmt.Println("请求处理完成,请求地址:", r.URL.String(), "请求结果:", r.Response.Status)
|
||||
//})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
@ -1,6 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
||||
fmt.Println(json.Valid([]byte("111")))
|
||||
}
|
||||
Reference in New Issue
Block a user