@@ -11,8 +11,8 @@ import (
1111 "syscall"
1212
1313 "github.com/google/uuid"
14+ "github.com/rs/zerolog"
1415 "github.com/savsgio/gotils/strconv"
15- "github.com/sirupsen/logrus"
1616 "github.com/valyala/fasthttp"
1717
1818 "github.com/wallarm/api-firewall/internal/platform/router"
3131// data/logic on this App struct
3232type App struct {
3333 Routers map [int ]* router.Mux
34- Log * logrus .Logger
34+ Log zerolog .Logger
3535 passOPTIONS bool
3636 shutdown chan os.Signal
3737 mw []web.Middleware
@@ -40,7 +40,7 @@ type App struct {
4040}
4141
4242// NewApp creates an App value that handle a set of routes for the set of application.
43- func NewApp (lock * sync.RWMutex , passOPTIONS bool , storedSpecs storage.DBOpenAPILoader , shutdown chan os.Signal , logger * logrus .Logger , mw ... web.Middleware ) * App {
43+ func NewApp (lock * sync.RWMutex , passOPTIONS bool , storedSpecs storage.DBOpenAPILoader , shutdown chan os.Signal , logger zerolog .Logger , mw ... web.Middleware ) * App {
4444
4545 schemaIDs := storedSpecs .SchemaIDs ()
4646
@@ -136,10 +136,10 @@ func (a *App) APIModeMainHandler(ctx *fasthttp.RequestCtx) {
136136 // handle panic
137137 defer func () {
138138 if r := recover (); r != nil {
139- a .Log .Errorf ("panic: %v" , r )
139+ a .Log .Error (). Msgf ("panic: %v" , r )
140140
141141 // Log the Go stack trace for this panic'd goroutine.
142- a .Log .Debugf ("%s" , debug .Stack ())
142+ a .Log .Debug (). Msgf ("%s" , debug .Stack ())
143143 return
144144 }
145145 }()
@@ -151,22 +151,22 @@ func (a *App) APIModeMainHandler(ctx *fasthttp.RequestCtx) {
151151 if err != nil {
152152 defer web .LogRequestResponseAtTraceLevel (ctx , a .Log )
153153
154- a .Log .WithFields (logrus. Fields {
155- "error" : err ,
156- "host" : strconv . B2S ( ctx .Request .Header .Host ()),
157- "path" : strconv . B2S ( ctx .Path ()),
158- "method" : strconv . B2S ( ctx .Request .Header .Method ()),
159- "request_id" : ctx .UserValue (web .RequestID ),
160- }). Error ("error while getting schema ID" )
154+ a .Log .Error ().
155+ Err ( err ).
156+ Bytes ( "host" , ctx .Request .Header .Host ()).
157+ Bytes ( "path" , ctx .Path ()).
158+ Bytes ( "method" , ctx .Request .Header .Method ()).
159+ Interface ( "request_id" , ctx .UserValue (web .RequestID )).
160+ Msg ("error while getting schema ID" )
161161
162162 if err := web .RespondError (ctx , fasthttp .StatusInternalServerError , "" ); err != nil {
163- a .Log .WithFields (logrus. Fields {
164- "error" : err ,
165- "host" : strconv . B2S ( ctx .Request .Header .Host ()),
166- "path" : strconv . B2S ( ctx .Path ()),
167- "method" : strconv . B2S ( ctx .Request .Header .Method ()),
168- "request_id" : ctx .UserValue (web .RequestID ),
169- }). Error ("error while sending response" )
163+ a .Log .Error ().
164+ Err ( err ).
165+ Bytes ( "host" , ctx .Request .Header .Host ()).
166+ Bytes ( "path" , ctx .Path ()).
167+ Bytes ( "method" , ctx .Request .Header .Method ()).
168+ Interface ( "request_id" , ctx .UserValue (web .RequestID )).
169+ Msg ("error while sending response" )
170170 }
171171
172172 return
@@ -196,22 +196,23 @@ func (a *App) APIModeMainHandler(ctx *fasthttp.RequestCtx) {
196196 // OPTIONS methods are passed if the passOPTIONS is set to true
197197 if a .passOPTIONS && strconv .B2S (ctx .Method ()) == fasthttp .MethodOptions {
198198 ctx .SetUserValue (keyStatusCode , fasthttp .StatusOK )
199- a .Log .WithFields (logrus. Fields {
200- "host" : strconv . B2S ( ctx .Request .Header .Host ()),
201- "path" : strconv . B2S ( ctx .Path ()),
202- "method" : strconv . B2S ( ctx .Request .Header .Method ()),
203- "request_id" : ctx .UserValue (web .RequestID ),
204- }). Debug ( "Pass request with OPTIONS method" )
199+ a .Log .Debug ().
200+ Bytes ( "host" , ctx .Request .Header .Host ()).
201+ Bytes ( "path" , ctx .Path ()).
202+ Bytes ( "method" , ctx .Request .Header .Method ()).
203+ Interface ( "request_id" , ctx .UserValue (web .RequestID )).
204+ Msg ( "pass request with OPTIONS method" )
205205 continue
206206 }
207207
208208 // Method or Path were not found
209- a .Log .WithFields (logrus.Fields {
210- "host" : strconv .B2S (ctx .Request .Header .Host ()),
211- "path" : strconv .B2S (ctx .Path ()),
212- "method" : strconv .B2S (ctx .Request .Header .Method ()),
213- "request_id" : ctx .UserValue (web .RequestID ),
214- }).Debug ("Method or path were not found" )
209+ a .Log .Debug ().
210+ Bytes ("host" , ctx .Request .Header .Host ()).
211+ Bytes ("path" , ctx .Path ()).
212+ Bytes ("method" , ctx .Request .Header .Method ()).
213+ Interface ("request_id" , ctx .UserValue (web .RequestID )).
214+ Msg ("method or path were not found" )
215+
215216 ctx .SetUserValue (keyValidationErrors , []* validator.ValidationError {{Message : validator .ErrMethodAndPathNotFound .Error (), Code : validator .ErrCodeMethodAndPathNotFound , SchemaID : & schemaID }})
216217 ctx .SetUserValue (keyStatusCode , fasthttp .StatusForbidden )
217218 continue
@@ -221,13 +222,13 @@ func (a *App) APIModeMainHandler(ctx *fasthttp.RequestCtx) {
221222 ctx .SetUserValue (router .RouteCtxKey , rctx )
222223
223224 if err := handler (ctx ); err != nil {
224- a .Log .WithFields (logrus. Fields {
225- "error" : err ,
226- "host" : strconv . B2S ( ctx .Request .Header .Host ()),
227- "path" : strconv . B2S ( ctx .Path ()),
228- "method" : strconv . B2S ( ctx .Request .Header .Method ()),
229- "request_id" : ctx .UserValue (web .RequestID ),
230- }). Error ("error in the request handler" )
225+ a .Log .Error ().
226+ Err ( err ).
227+ Bytes ( "host" , ctx .Request .Header .Host ()).
228+ Bytes ( "path" , ctx .Path ()).
229+ Bytes ( "method" , ctx .Request .Header .Method ()).
230+ Interface ( "request_id" , ctx .UserValue (web .RequestID )).
231+ Msg ("error in the request handler" )
231232 }
232233 }
233234
@@ -286,13 +287,13 @@ func (a *App) APIModeMainHandler(ctx *fasthttp.RequestCtx) {
286287 }
287288
288289 if err := web .Respond (ctx , validator.ValidationResponse {Summary : responseSummary , Errors : responseErrors }, fasthttp .StatusOK ); err != nil {
289- a .Log .WithFields (logrus. Fields {
290- "request_id" : ctx . UserValue ( web . RequestID ),
291- "host" : strconv . B2S ( ctx .Request .Header .Host ()),
292- "path" : strconv . B2S ( ctx .Path ()),
293- "method" : strconv . B2S ( ctx .Request .Header .Method ()),
294- "error" : err ,
295- }). Error ("respond error" )
290+ a .Log .Error ().
291+ Err ( err ).
292+ Bytes ( "host" , ctx .Request .Header .Host ()).
293+ Bytes ( "path" , ctx .Path ()).
294+ Bytes ( "method" , ctx .Request .Header .Method ()).
295+ Interface ( "request_id" , ctx . UserValue ( web . RequestID )).
296+ Msg ("respond error" )
296297 }
297298}
298299
0 commit comments