github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_log_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 // static service testing. 8 9 package ghttp_test 10 11 import ( 12 "fmt" 13 "testing" 14 "time" 15 16 "github.com/wangyougui/gf/v2/frame/g" 17 "github.com/wangyougui/gf/v2/net/ghttp" 18 "github.com/wangyougui/gf/v2/os/gfile" 19 "github.com/wangyougui/gf/v2/os/gtime" 20 "github.com/wangyougui/gf/v2/test/gtest" 21 "github.com/wangyougui/gf/v2/text/gstr" 22 "github.com/wangyougui/gf/v2/util/guid" 23 ) 24 25 func Test_Log(t *testing.T) { 26 gtest.C(t, func(t *gtest.T) { 27 logDir := gfile.Temp(gtime.TimestampNanoStr()) 28 s := g.Server(guid.S()) 29 s.BindHandler("/hello", func(r *ghttp.Request) { 30 r.Response.Write("hello") 31 }) 32 s.BindHandler("/error", func(r *ghttp.Request) { 33 panic("custom error") 34 }) 35 s.SetLogPath(logDir) 36 s.SetAccessLogEnabled(true) 37 s.SetErrorLogEnabled(true) 38 s.SetLogStdout(false) 39 s.Start() 40 defer s.Shutdown() 41 defer gfile.Remove(logDir) 42 time.Sleep(100 * time.Millisecond) 43 client := g.Client() 44 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 45 46 t.Assert(client.GetContent(ctx, "/hello"), "hello") 47 t.Assert(client.GetContent(ctx, "/error"), "exception recovered: custom error") 48 49 var ( 50 logPath1 = gfile.Join(logDir, gtime.Now().Format("Y-m-d")+".log") 51 content = gfile.GetContents(logPath1) 52 ) 53 t.Assert(gstr.Contains(content, "http server started listening on"), true) 54 t.Assert(gstr.Contains(content, "HANDLER"), true) 55 56 logPath2 := gfile.Join(logDir, "access-"+gtime.Now().Format("Ymd")+".log") 57 // fmt.Println(gfile.GetContents(logPath2)) 58 t.Assert(gstr.Contains(gfile.GetContents(logPath2), " /hello "), true) 59 60 logPath3 := gfile.Join(logDir, "error-"+gtime.Now().Format("Ymd")+".log") 61 // fmt.Println(gfile.GetContents(logPath3)) 62 t.Assert(gstr.Contains(gfile.GetContents(logPath3), "custom error"), true) 63 }) 64 }