@@ -68,7 +68,11 @@ func SetWithConfig(level string, config *LogConfig) error {
6868
6969// Access logs an access entry with call duration and status code
7070func Access (r * http.Request , start time.Time , statusCode int ) {
71- e := access (r , start , statusCode , nil )
71+ access (logrus .InfoLevel , r , start , statusCode )
72+ }
73+
74+ func access (level logrus.Level , r * http.Request , start time.Time , statusCode int ) {
75+ e := createAccessEntry (r , start , statusCode , nil )
7276
7377 var msg string
7478 if len (r .URL .RawQuery ) == 0 {
@@ -77,17 +81,19 @@ func Access(r *http.Request, start time.Time, statusCode int) {
7781 msg = fmt .Sprintf ("%v ->%v %v?%s" , statusCode , r .Method , r .URL .Path , r .URL .RawQuery )
7882 }
7983
84+ e .Log (accessLogLevelFor (level , r , statusCode ), msg )
85+ }
86+
87+ func accessLogLevelFor (level logrus.Level , r * http.Request , statusCode int ) logrus.Level {
8088 if statusCode >= 200 && statusCode <= 399 {
8189 if isHealthRequest (r ) {
82- e .Debug (msg )
83- } else {
84- e .Info (msg )
90+ return logrus .DebugLevel
8591 }
92+ return level
8693 } else if statusCode >= 400 && statusCode <= 499 {
87- e .Warn (msg )
88- } else {
89- e .Error (msg )
94+ return logrus .WarnLevel
9095 }
96+ return logrus .ErrorLevel
9197}
9298
9399func isHealthRequest (r * http.Request ) bool {
@@ -96,16 +102,16 @@ func isHealthRequest(r *http.Request) bool {
96102
97103// AccessError logs an error while accessing
98104func AccessError (r * http.Request , start time.Time , err error ) {
99- e := access (r , start , 0 , err )
105+ e := createAccessEntry (r , start , 0 , err )
100106 e .Errorf ("ERROR ->%v %v" , r .Method , r .URL .Path )
101107}
102108
103109func AccessAborted (r * http.Request , start time.Time ) {
104- e := access (r , start , 0 , nil )
110+ e := createAccessEntry (r , start , 0 , nil )
105111 e .Infof ("ABORTED ->%v %v" , r .Method , r .URL .Path )
106112}
107113
108- func access (r * http.Request , start time.Time , statusCode int , err error ) * Entry {
114+ func createAccessEntry (r * http.Request , start time.Time , statusCode int , err error ) * Entry {
109115 url := r .URL .Path
110116 if r .URL .RawQuery != "" {
111117 url += "?" + r .URL .RawQuery
0 commit comments