GoKit’sDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/AndresGT/GoKit/llms.txt
Use this file to discover all available pages before exploring further.
logger package ships first-class Gin middleware so that every HTTP request automatically gets a structured log entry with its request ID, client IP, HTTP method, path, and a level-appropriate completion message — all without writing a single line of logging code in your handlers. A request-scoped *Logger is stored in the Gin context and made available through helper functions that any handler can call.
GinMiddleware
GinMiddleware is the core logging middleware. Register it on your router once and every request flowing through that router will be logged automatically.
What it does
Assigns a Request ID
Reads the
X-Request-ID header from the incoming request. If the header is absent, a new UUID is generated. The ID is written back to the response as X-Request-ID.Creates a contextual logger
Forks the global logger with five fields pre-attached:
request_id, method, path, ip, and user_agent.Stores the logger in Gin's context
Calls
c.Set("logger", reqLogger) so handlers can retrieve it with logger.GetLogger(c).Logs request completion
After the handler chain returns, emits a level-appropriate log entry:
Info (“Request completed”) for 2xx/3xx responses, Warn (“Request warning”) for 4xx, and Error (“Request failed”) for 5xx. The contextual fields already attached (request_id, method, path, ip, user_agent) are included in the completion entry.Registering the middleware
Use
gin.New() instead of gin.Default(). gin.Default() registers Gin’s own logger and recovery middleware; using it alongside GoKit’s middleware will produce duplicate log output.GinRecovery
GinRecovery is a lightweight panic recovery middleware. When any handler in the chain panics, GinRecovery catches the error, logs it at ErrorLevel with the panic, method, path, and ip fields, then responds with HTTP 500.
For production deployments that require full stack traces in the log entry, consider using the
middleware.Recovery middleware from GoKit’s middleware package instead. GinRecovery is a lightweight alternative suitable for development and staging.Accessing the logger inside handlers
logger.GetLogger(c *gin.Context) *Logger
Returns the request-scoped *Logger stored in the Gin context by GinMiddleware. If the middleware was not registered (for example in a unit test), it falls back to the global logger so your handlers never have to guard against a nil logger.
GetLogger returns the request-scoped logger, all log entries from that handler automatically include the request_id, ip, method, and path fields set by the middleware.
Helper functions
Four convenience functions retrieve the request logger and call the appropriate method in a single call — useful when you only need to emit one entry and don’t need to hold a reference to the logger:| Function | Equivalent to |
|---|---|
logger.LogRequestInfo(c, format, args...) | GetLogger(c).Info(format, args...) |
logger.LogRequestWarn(c, format, args...) | GetLogger(c).Warn(format, args...) |
logger.LogRequestError(c, format, args...) | GetLogger(c).Error(format, args...) |
logger.LogRequestSecurity(c, format, args...) | GetLogger(c).Security(format, args...) |
Route registration helpers
GoKit provides three functions that register a Gin route and emit an[INFO]-level log entry describing it. Use them instead of calling group.GET(...) or group.POST(...) directly to get a printed route map every time your application starts.
logger.RegisterRoute
Registers a single route and logs it using the default RouteConfig.
logger.RegisterRouteWithConfig
Same as RegisterRoute but accepts a custom RouteConfig.
When
true, protected routes are prefixed with a 🔒 icon in the log output.When
true, a HH:MM:SS timestamp is prepended to each route log line.logger.RegisterRoutes
Batch-registers a slice of Route values.
Example
Full router setup example
The following snippet shows a complete, production-style router setup combiningInitGlobal, both middlewares, and batch route registration: