Skip to content

Commit

Permalink
Use 'development' flag for HTTP request logger middleware. (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickeskov authored Sep 21, 2023
1 parent a25d1fc commit f533756
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
4 changes: 3 additions & 1 deletion cmd/bots/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ func runDiscordBot() error {
runMessagingClients(ctx, cfg, discordBotEnv, logger, requestChan, responseChan)

if cfg.bindAddress != "" {
botAPI, apiErr := api.NewBotAPI(cfg.bindAddress, requestChan, responseChan, defaultAPIReadTimeout, logger, atom)
botAPI, apiErr := api.NewBotAPI(cfg.bindAddress, requestChan, responseChan, defaultAPIReadTimeout,
logger, atom, cfg.development,
)
if apiErr != nil {
logger.Error("Failed to initialize bot API", zap.Error(apiErr))
return apiErr
Expand Down
6 changes: 5 additions & 1 deletion cmd/bots/internal/common/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func NewBotAPI(
apiReadTimeout time.Duration,
logger *zap.Logger,
atom *zap.AtomicLevel,
development bool,
) (*BotAPI, error) {
a := &BotAPI{
zap: logger,
Expand All @@ -50,7 +51,10 @@ func NewBotAPI(
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: mwLog{logger}}))
r.Use(middleware.RequestLogger(&middleware.DefaultLogFormatter{
Logger: mwLog{logger},
NoColor: !development,
}))
r.Use(middleware.Recoverer)
r.Use(middleware.SetHeader("Content-Type", "application/json"))
r.Mount("/", a.routes())
Expand Down
4 changes: 3 additions & 1 deletion cmd/bots/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ func runTelegramBot() error {
runMessagingClients(ctx, cfg, tgBotEnv, logger, requestChan, responseChan)

if cfg.bindAddress != "" {
botAPI, apiErr := api.NewBotAPI(cfg.bindAddress, requestChan, responseChan, defaultAPIReadTimeout, logger, atom)
botAPI, apiErr := api.NewBotAPI(cfg.bindAddress, requestChan, responseChan, defaultAPIReadTimeout,
logger, atom, cfg.development,
)
if apiErr != nil {
logger.Error("Failed to initialize bot API", zap.Error(apiErr))
return apiErr
Expand Down
2 changes: 1 addition & 1 deletion cmd/nodemon/nodemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func run() error {
notifications = privateNodesHandler.Run(notifications) // wraps scrapper's notification with private nodes handler

a, err := api.NewAPI(cfg.bindAddress, ns, es, cfg.apiReadTimeout, logger,
privateNodesHandler.PrivateNodesEventsWriter(), atom,
privateNodesHandler.PrivateNodesEventsWriter(), atom, cfg.development,
)
if err != nil {
logger.Error("failed to initialize API", zap.Error(err))
Expand Down
6 changes: 5 additions & 1 deletion pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func NewAPI(
logger *zap.Logger,
privateNodesEvents specific.PrivateNodesEventsWriter,
atom *zap.AtomicLevel,
development bool,
) (*API, error) {
a := &API{
nodesStorage: nodesStorage,
Expand All @@ -60,7 +61,10 @@ func NewAPI(
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: mwLog{logger}}))
r.Use(middleware.RequestLogger(&middleware.DefaultLogFormatter{
Logger: mwLog{logger},
NoColor: !development,
}))
r.Use(middleware.Recoverer)
r.Use(middleware.SetHeader("Content-Type", "application/json"))
r.Mount("/", a.routes())
Expand Down

0 comments on commit f533756

Please sign in to comment.