Skip to content

Commit

Permalink
add debug log
Browse files Browse the repository at this point in the history
  • Loading branch information
pinguo-yangbing committed Dec 24, 2019
1 parent 16f027d commit 57ceea2
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 59 deletions.
8 changes: 5 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type objectItem struct {
// safe, copy context to use in other goroutines
type Context struct {
enableAccessLog bool
debug bool
response Response
input *http.Request
output http.ResponseWriter
Expand All @@ -43,7 +44,8 @@ type Context struct {
logs.Logger
}

func (c *Context) HttpRW(enableAccessLog bool, r *http.Request, w http.ResponseWriter) {
func (c *Context) HttpRW(debug, enableAccessLog bool, r *http.Request, w http.ResponseWriter) {
c.debug = debug
c.enableAccessLog = enableAccessLog
c.input = r
c.output = &c.response
Expand Down Expand Up @@ -72,7 +74,7 @@ func (c *Context) Process(plugins []iface.IPlugin) {

}

func (c *Context) Start(plugins []iface.IPlugin){
func (c *Context) Start(plugins []iface.IPlugin) {
// generate request id
logId := c.Header("X-Log-Id", "")
if logId == "" {
Expand Down Expand Up @@ -103,7 +105,7 @@ func (c *Context) finish(goLog bool) {
c.End(status, []byte(http.StatusText(status)))
}

c.Error("%s, trace[%s]", util.ToString(v), util.PanicTrace(TraceMaxDepth, false))
c.Error("%s, trace[%s]", util.ToString(v), util.PanicTrace(TraceMaxDepth, false, c.debug))
}

if !goLog {
Expand Down
28 changes: 14 additions & 14 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestContext_Getter(t *testing.T) {
rb := bytes.NewBuffer([]byte(str))
w.Body = rb

context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

context.Output().WriteHeader(rICode)
context.Output().Write([]byte(str))
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestContext_ClientIp(t *testing.T) {
r := httptest.NewRequest(method, path, body)
ip := "129.1.1.1"
r.Header.Set("X-Forwarded-For", ip+",")
context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)
if context.ClientIp() != ip {
t.FailNow()
}
Expand All @@ -199,7 +199,7 @@ func TestContext_ClientIp(t *testing.T) {
r := httptest.NewRequest(method, path, body)
ip := "129.1.1.1"
r.Header.Set("X-Forwarded-For", ip)
context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)
if context.ClientIp() != ip {
t.FailNow()
}
Expand All @@ -209,7 +209,7 @@ func TestContext_ClientIp(t *testing.T) {
r := httptest.NewRequest(method, path, body)
ip := "129.1.1.1"
r.Header.Set("X-Client-Ip", ip)
context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)
if context.ClientIp() != ip {
t.FailNow()
}
Expand All @@ -219,7 +219,7 @@ func TestContext_ClientIp(t *testing.T) {
r := httptest.NewRequest(method, path, body)
ip := "129.1.1.1"
r.Header.Set("X-Real-Ip", ip)
context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)
if context.ClientIp() != ip {
t.FailNow()
}
Expand All @@ -229,7 +229,7 @@ func TestContext_ClientIp(t *testing.T) {
r := httptest.NewRequest(method, path, body)
ip := "129.1.1.1"
r.RemoteAddr = ip
context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)
if context.ClientIp() != ip {
t.FailNow()
}
Expand All @@ -255,7 +255,7 @@ func TestContext_Cookie(t *testing.T) {
cookieUser1 := &http.Cookie{Name: cName + "1", Value: cNameV + "1", Path: "/"}
r.AddCookie(cookieUser1)

context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)
t.Run("GetCookie", func(t *testing.T) {
if context.Cookie(cName, "") != cNameV {
t.FailNow()
Expand Down Expand Up @@ -296,7 +296,7 @@ func TestContext_Header(t *testing.T) {
r.Header.Set(cName, cNameV)
r.Header.Set(cName+"1", cNameV+"1")

context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)
t.Run("GetHeader", func(t *testing.T) {
if context.Header(cName, "") != cNameV {
t.FailNow()
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestContext_Query(t *testing.T) {
body := bytes.NewReader([]byte(str))
w := httptest.NewRecorder()
r := httptest.NewRequest(method, path, body)
context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)
t.Run("Query", func(t *testing.T) {
if context.Query(param1, "") != paramV1 {
t.FailNow()
Expand Down Expand Up @@ -370,7 +370,7 @@ func TestContext_QueryArray(t *testing.T) {
body := bytes.NewReader([]byte(str))
w := httptest.NewRecorder()
r := httptest.NewRequest(method, path, body)
context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)
if len(context.QueryArray(param1)) != 2 {
t.FailNow()
}
Expand All @@ -397,7 +397,7 @@ func TestContext_Post(t *testing.T) {
r := httptest.NewRequest(method, path, body)
r.Header.Set("Content-type", "application/x-www-form-urlencoded")

context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

t.Run("Post", func(t *testing.T) {
if context.Post(param1, "") != paramV1 {
Expand Down Expand Up @@ -434,7 +434,7 @@ func TestContext_PostArray(t *testing.T) {
r := httptest.NewRequest(method, path, body)
r.Header.Set("Content-type", "application/x-www-form-urlencoded")

context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

if len(context.PostArray(param1)) != 2 {
t.FailNow()
Expand Down Expand Up @@ -472,7 +472,7 @@ func TestContext_Param(t *testing.T) {
r := httptest.NewRequest(method, path, body)
r.Header.Set("Content-type", "application/x-www-form-urlencoded")

context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

if context.Param(param1, "") != paramV1 {
t.Fatal("context.Param(param1, \"\") != ", paramV1)
Expand Down Expand Up @@ -518,7 +518,7 @@ func TestContext_ParamArray(t *testing.T) {
r := httptest.NewRequest(method, path, body)
r.Header.Set("Content-type", "application/x-www-form-urlencoded")

context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

if len(context.ParamArray(param1)) != 2 {
t.Fatal("len(context.ParamArray(param1)) !=", 2)
Expand Down
4 changes: 2 additions & 2 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *Controller) AfterAction(action string) {
}

// HandlePanic process unhandled action panic
func (c *Controller) HandlePanic(v interface{}) {
func (c *Controller) HandlePanic(v interface{}, debug bool) {
status := http.StatusInternalServerError
switch e := v.(type) {
case *perror.Error:
Expand All @@ -54,7 +54,7 @@ func (c *Controller) HandlePanic(v interface{}) {
c.Json(EmptyObject, status)
}

c.Context().Error("%s, trace[%s]", util.ToString(v), util.PanicTrace(TraceMaxDepth, false))
c.Context().Error("%s, trace[%s]", util.ToString(v), util.PanicTrace(TraceMaxDepth, false, debug))
}

// Redirect output redirect response
Expand Down
16 changes: 8 additions & 8 deletions controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestController_HandlePanic(t *testing.T) {
})
defer patchesController.Reset()

mockC.HandlePanic("testerr")
mockC.HandlePanic("testerr", false)

}

Expand All @@ -64,7 +64,7 @@ func TestController_Json(t *testing.T) {
r := httptest.NewRequest(method, path, body)
w := httptest.NewRecorder()

context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

mockC := &Controller{}
mockC.SetContext(context)
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestController_Jsonp(t *testing.T) {
r := httptest.NewRequest(method, path, body)
w := httptest.NewRecorder()

context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

mockC := &Controller{}
mockC.SetContext(context)
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestController_Data(t *testing.T) {
r := httptest.NewRequest(method, path, body)
w := httptest.NewRecorder()

context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

mockC := &Controller{}
mockC.SetContext(context)
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestController_Xml(t *testing.T) {
r := httptest.NewRequest(method, path, body)
w := httptest.NewRecorder()

context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

mockC := &Controller{}
mockC.SetContext(context)
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestController_Xml(t *testing.T) {
// r := httptest.NewRequest(method, path, body)
// w := httptest.NewRecorder()
//
// context.HttpRW(true, r, w)
// context.HttpRW(false,true, r, w)
//
// mockC := &Controller{}
// mockC.SetContext(context)
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestController_Render(t *testing.T) {
r := httptest.NewRequest(method, path, body)
w := httptest.NewRecorder()

context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

mockC := &Controller{}
mockC.SetContext(context)
Expand Down Expand Up @@ -275,7 +275,7 @@ func TestController_View(t *testing.T) {
r := httptest.NewRequest(method, path, body)
w := httptest.NewRecorder()

context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

mockC := &Controller{}
mockC.SetContext(context)
Expand Down
10 changes: 5 additions & 5 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestFile_HandleRequest(t *testing.T) {
r := httptest.NewRequest(method, "/view", body)
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

file := NewFile(nil)
file.HandleRequest(context)
Expand All @@ -61,7 +61,7 @@ func TestFile_HandleRequest(t *testing.T) {
r := httptest.NewRequest(method, "/view.html", body)
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)
file := NewFile(nil)
file.SetExcludeExtensions([]interface{}{".html"})
file.HandleRequest(context)
Expand All @@ -79,7 +79,7 @@ func TestFile_HandleRequest(t *testing.T) {
r := httptest.NewRequest("POST", "/view.html", body)
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)
file := NewFile(nil)
file.HandleRequest(context)

Expand All @@ -93,7 +93,7 @@ func TestFile_HandleRequest(t *testing.T) {
r := httptest.NewRequest(method, "/viewNotExists.html", body)
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

file := NewFile(nil)
file.HandleRequest(context)
Expand All @@ -108,7 +108,7 @@ func TestFile_HandleRequest(t *testing.T) {
r := httptest.NewRequest(method, "/view.html", body)
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

file := NewFile(nil)
file.HandleRequest(context)
Expand Down
6 changes: 3 additions & 3 deletions gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestGzip_HandleRequest(t *testing.T) {
r := httptest.NewRequest("GET", "/view", body)
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

gzip := NewGzip()
gzip.HandleRequest(context)
Expand All @@ -37,7 +37,7 @@ func TestGzip_HandleRequest(t *testing.T) {
r.Header.Set("Accept-Encoding", "gzip")
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

gzip := NewGzip()
gzip.HandleRequest(context)
Expand All @@ -52,7 +52,7 @@ func TestGzip_HandleRequest(t *testing.T) {
r.Header.Set("Accept-Encoding", "gzip")
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(true, r, w)
context.HttpRW(false,true, r, w)

gzip := NewGzip()
gzip.HandleRequest(context)
Expand Down
4 changes: 2 additions & 2 deletions iface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type IObject interface {
type IController interface {
BeforeAction(action string)
AfterAction(action string)
HandlePanic(v interface{})
HandlePanic(v interface{}, debug bool)
}

type IPlugin interface {
Expand Down Expand Up @@ -73,7 +73,7 @@ type IView interface {
}

type IContext interface {
HttpRW(enableAccessLog bool, r *http.Request, w http.ResponseWriter)
HttpRW(debug, enableAccessLog bool, r *http.Request, w http.ResponseWriter)
Process(plugins []IPlugin)
Notice(format string, v ...interface{})
Debug(format string, v ...interface{})
Expand Down
Loading

0 comments on commit 57ceea2

Please sign in to comment.