I'm working in a project using Gin and recently started implementing a logger through the project. I saw a couple libraries but decided to go with uber's Zap.
I've got controllers that look like this:
package controllers
func ListAllArtists(c *gin.Context) {
db := c.MustGet("db").(*gorm.DB)
service := services.NewArtistService(db)
pagination := entities.PaginationFromRequest(c)
err := service.FindAll(pagination)
if err != nil {
err := c.AbortWithError(http.StatusBadRequest, err)
if err != nil {
fmt.Print(err)
}
return
}
responses.RespondPaginated(c, pagination, service.Artists)
}
as you can see they call a service which performs a DB call. So the only parameter the service receives (in this case) is the db connection.
Routes are defined as following:
func ArtistRoutes(router *gin.RouterGroup) {
artistRouter := router.Group("/artists")
{
artistRouter.POST("", controllers.CreateNewArtist)
artistRouter.GET("", controllers.ListAllArtists)
artistRouter.GET("/search", controllers.SearchArtists)
artistRouter.GET("/slug/:slug", controllers.GetArtistBySlug)
artistRouter.GET("/:id", controllers.GetArtistById)
artistRouter.DELETE("/:id", controllers.DeleteArtistById)
artistRouter.PATCH("/:id", controllers.UpdateArtist)
}
}
My main file looks like:
app := core.NewApp()
if app == nil {
log.Fatal("app could not be initialized. Shutting down")
}
// initialize middlewares.
app.Router.Use(func(c *gin.Context) {
c.Set("db", app.Db)
})
// serve static files. All static files (images, videos, css, js, etc) will be served from this location.
app.Router.Static("/assets", "./assets")
// Initialize all api routes
routes.InitializeRoutes(app.Router.Group("/api/v1"))
My question is: is there a way to pass down the logger? can I make it part of c *gin.Context so it's widely available?
I'm aware of an option that zap offers in which you can make the logger global, but that's not recommended (although my project is pretty small) so I wouldn't want to go that route.
Not sure if this is the best way but it is what I ended doing (at the moment, waiting for a more suitable response to implement):
Created a
logger.gofile in myutilspackage:then in my main I initialized the logger:
and using the logger throughout the project like: