From f533756d77bc585ed2d4258de687212aa7377f3e Mon Sep 17 00:00:00 2001 From: Nikolay Eskov Date: Thu, 21 Sep 2023 15:28:39 +0300 Subject: [PATCH] Use 'development' flag for HTTP request logger middleware. (#212) --- cmd/bots/discord/discord.go | 4 +++- cmd/bots/internal/common/api/api.go | 6 +++++- cmd/bots/telegram/telegram.go | 4 +++- cmd/nodemon/nodemon.go | 2 +- pkg/api/api.go | 6 +++++- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cmd/bots/discord/discord.go b/cmd/bots/discord/discord.go index 368ac931..51d83649 100644 --- a/cmd/bots/discord/discord.go +++ b/cmd/bots/discord/discord.go @@ -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 diff --git a/cmd/bots/internal/common/api/api.go b/cmd/bots/internal/common/api/api.go index 8fd4f834..1721d7bf 100644 --- a/cmd/bots/internal/common/api/api.go +++ b/cmd/bots/internal/common/api/api.go @@ -40,6 +40,7 @@ func NewBotAPI( apiReadTimeout time.Duration, logger *zap.Logger, atom *zap.AtomicLevel, + development bool, ) (*BotAPI, error) { a := &BotAPI{ zap: logger, @@ -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()) diff --git a/cmd/bots/telegram/telegram.go b/cmd/bots/telegram/telegram.go index 1fb176ad..d1c5be06 100644 --- a/cmd/bots/telegram/telegram.go +++ b/cmd/bots/telegram/telegram.go @@ -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 diff --git a/cmd/nodemon/nodemon.go b/cmd/nodemon/nodemon.go index ae2876fc..a5039e2a 100644 --- a/cmd/nodemon/nodemon.go +++ b/cmd/nodemon/nodemon.go @@ -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)) diff --git a/pkg/api/api.go b/pkg/api/api.go index 482ace71..2e4da153 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -49,6 +49,7 @@ func NewAPI( logger *zap.Logger, privateNodesEvents specific.PrivateNodesEventsWriter, atom *zap.AtomicLevel, + development bool, ) (*API, error) { a := &API{ nodesStorage: nodesStorage, @@ -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())