github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_server_log.go (about) 1 // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf. 6 // 默认错误日志封装. 7 8 package ghttp 9 10 import ( 11 "fmt" 12 13 "github.com/zhongdalu/gf/g/os/gtime" 14 ) 15 16 // 处理服务错误信息,主要是panic,http请求的status由access log进行管理 17 func (s *Server) handleAccessLog(r *Request) { 18 if !s.IsAccessLogEnabled() { 19 return 20 } 21 // 自定义错误处理 22 if v := s.GetLogHandler(); v != nil { 23 v(r) 24 return 25 } 26 scheme := "http" 27 if r.TLS != nil { 28 scheme = "https" 29 } 30 content := fmt.Sprintf(`%d "%s %s %s %s %s"`, 31 r.Response.Status, 32 r.Method, scheme, r.Host, r.URL.String(), r.Proto, 33 ) 34 content += fmt.Sprintf(` %.3f`, float64(r.LeaveTime-r.EnterTime)/1000) 35 content += fmt.Sprintf(`, %s, "%s", "%s"`, r.GetClientIp(), r.Referer(), r.UserAgent()) 36 s.logger.Cat("access").Stack(false, 2).Stdout(s.config.LogStdout).Println(content) 37 } 38 39 // 处理服务错误信息,主要是panic,http请求的status由access log进行管理 40 func (s *Server) handleErrorLog(error interface{}, r *Request) { 41 // 错误输出默认是开启的 42 if !s.IsErrorLogEnabled() { 43 return 44 } 45 46 // 自定义错误处理 47 if v := s.GetLogHandler(); v != nil { 48 v(r, error) 49 return 50 } 51 52 // 错误日志信息 53 scheme := "http" 54 if r.TLS != nil { 55 scheme = "https" 56 } 57 content := fmt.Sprintf(`%v, "%s %s %s %s %s"`, error, r.Method, scheme, r.Host, r.URL.String(), r.Proto) 58 if r.LeaveTime > r.EnterTime { 59 content += fmt.Sprintf(` %.3f`, float64(r.LeaveTime-r.EnterTime)/1000) 60 } else { 61 content += fmt.Sprintf(` %.3f`, float64(gtime.Microsecond()-r.EnterTime)/1000) 62 } 63 content += fmt.Sprintf(`, %s, "%s", "%s"`, r.GetClientIp(), r.Referer(), r.UserAgent()) 64 s.logger.Cat("error").Stack(true, 2).Stdout(s.config.LogStdout).Error(content) 65 }