github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_server_log.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/gogf/gf.
     6  
     7  package ghttp
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/gogf/gf/v2/errors/gerror"
    13  	"github.com/gogf/gf/v2/internal/instance"
    14  	"github.com/gogf/gf/v2/os/glog"
    15  	"github.com/gogf/gf/v2/text/gstr"
    16  )
    17  
    18  // handleAccessLog handles the access logging for server.
    19  func (s *Server) handleAccessLog(r *Request) {
    20  	if !s.IsAccessLogEnabled() {
    21  		return
    22  	}
    23  	var (
    24  		scheme            = r.GetSchema()
    25  		loggerInstanceKey = fmt.Sprintf(`Acccess Logger Of Server:%s`, s.instance)
    26  	)
    27  	content := fmt.Sprintf(
    28  		`%d "%s %s %s %s %s" %.3f, %s, "%s", "%s"`,
    29  		r.Response.Status, r.Method, scheme, r.Host, r.URL.String(), r.Proto,
    30  		float64(r.LeaveTime.Sub(r.EnterTime).Milliseconds())/1000,
    31  		r.GetClientIp(), r.Referer(), r.UserAgent(),
    32  	)
    33  	logger := instance.GetOrSetFuncLock(loggerInstanceKey, func() interface{} {
    34  		l := s.Logger().Clone()
    35  		l.SetFile(s.config.AccessLogPattern)
    36  		l.SetStdoutPrint(s.config.LogStdout)
    37  		l.SetLevelPrint(false)
    38  		return l
    39  	}).(*glog.Logger)
    40  	logger.Print(r.Context(), content)
    41  }
    42  
    43  // handleErrorLog handles the error logging for server.
    44  func (s *Server) handleErrorLog(err error, r *Request) {
    45  	// It does nothing if error logging is custom disabled.
    46  	if !s.IsErrorLogEnabled() {
    47  		return
    48  	}
    49  	var (
    50  		code              = gerror.Code(err)
    51  		scheme            = r.GetSchema()
    52  		codeDetail        = code.Detail()
    53  		loggerInstanceKey = fmt.Sprintf(`Error Logger Of Server:%s`, s.instance)
    54  		codeDetailStr     string
    55  	)
    56  	if codeDetail != nil {
    57  		codeDetailStr = gstr.Replace(fmt.Sprintf(`%+v`, codeDetail), "\n", " ")
    58  	}
    59  	content := fmt.Sprintf(
    60  		`%d "%s %s %s %s %s" %.3f, %s, "%s", "%s", %d, "%s", "%+v"`,
    61  		r.Response.Status, r.Method, scheme, r.Host, r.URL.String(), r.Proto,
    62  		float64(r.LeaveTime.Sub(r.EnterTime))/1000,
    63  		r.GetClientIp(), r.Referer(), r.UserAgent(),
    64  		code.Code(), code.Message(), codeDetailStr,
    65  	)
    66  	if s.config.ErrorStack {
    67  		if stack := gerror.Stack(err); stack != "" {
    68  			content += "\nStack:\n" + stack
    69  		} else {
    70  			content += ", " + err.Error()
    71  		}
    72  	} else {
    73  		content += ", " + err.Error()
    74  	}
    75  	logger := instance.GetOrSetFuncLock(loggerInstanceKey, func() interface{} {
    76  		l := s.Logger().Clone()
    77  		l.SetStack(false)
    78  		l.SetFile(s.config.ErrorLogPattern)
    79  		l.SetStdoutPrint(s.config.LogStdout)
    80  		l.SetLevelPrint(false)
    81  		return l
    82  	}).(*glog.Logger)
    83  	logger.Error(r.Context(), content)
    84  }