Skip to content

Commit

Permalink
feat(deps): use logger from context if available
Browse files Browse the repository at this point in the history
  • Loading branch information
Pitasi committed Mar 2, 2022
1 parent dfd8bd1 commit fc25f38
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions api/router/deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func GetDeps(c *gin.Context) *Deps {
panic(fmt.Sprintf("deps not of the expected type, found %T", deps))
}

// override logger with the one from the gin.context
deps.Logger = GetLogger(c)

return deps
}

Expand Down Expand Up @@ -66,3 +69,18 @@ func (d *Deps) LogError(logMessage string, keyAndValues ...interface{}) {
)
}
}

// GetLogger returns the logger from the gin context, or nil if there isn't.
func GetLogger(c *gin.Context) *zap.SugaredLogger {
loggerValue, ok := c.Get("logger")
if !ok {
return nil
}

logger, ok := loggerValue.(*zap.SugaredLogger)
if !ok {
return nil
}

return logger
}

0 comments on commit fc25f38

Please sign in to comment.