-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace logrus with zerolog for structured logging (#23)
### TL;DR Replaced logrus with zerolog for improved logging and implemented custom logger initialization. ### What changed? - Replaced logrus with zerolog across the codebase - Added a new `log` package with `InitLogger` and `NewLogger` functions - Updated logging calls to use zerolog's structured logging - Implemented log level configuration via environment variable - Added option for prettified console output ### How to test? 1. Set the `LOG_LEVEL` environment variable to control log verbosity (e.g., "debug", "info", "warn") 2. Set `LOG_PRETTIFY=true` for human-readable console output 3. Run the application and verify that logs are correctly formatted and filtered based on the set log level ### Why make this change? - Zerolog offers better performance and more flexible structured logging - Custom logger initialization allows for consistent logging across the application - Environment variable configuration enables easy log level adjustment without code changes - Prettified output option improves readability during development and debugging
- Loading branch information
Showing
12 changed files
with
109 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/joho/godotenv" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/rs/zerolog/log" | ||
"github.com/thirdweb-dev/indexer/internal/handlers" | ||
customLogger "github.com/thirdweb-dev/indexer/internal/log" | ||
) | ||
|
||
func main() { | ||
err := godotenv.Load() | ||
if err != nil { | ||
log.Errorf("error loading .env file: %w", err) | ||
log.Error().Err(err).Msg("error loading .env file") | ||
} | ||
customLogger.InitLogger() | ||
|
||
log.SetReportCaller(true) | ||
var r *chi.Mux = chi.NewRouter() | ||
handlers.Handler(r) | ||
|
||
fmt.Println("Starting Server on port 3000") | ||
log.Info().Msg("Starting Server on port 3000") | ||
err = http.ListenAndServe("localhost:3000", r) | ||
if err != nil { | ||
log.Error(err) | ||
log.Error().Err(err).Msg("Error starting server") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package log | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
"github.com/rs/zerolog/pkgerrors" | ||
) | ||
|
||
func InitLogger() { | ||
// overrides zerolog global logger | ||
log.Logger = NewLogger("default") | ||
} | ||
|
||
func NewLogger(name string) zerolog.Logger { | ||
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack | ||
|
||
level := zerolog.WarnLevel | ||
if lvl, err := zerolog.ParseLevel(os.Getenv("LOG_LEVEL")); err == nil && lvl != zerolog.NoLevel { | ||
level = lvl | ||
} | ||
zerolog.SetGlobalLevel(level) | ||
|
||
prettify := os.Getenv("LOG_PRETTIFY") == "true" | ||
logger := zerolog.New(os.Stderr).With().Timestamp().Str("component", name).Logger() | ||
logger = logger.With().Caller().Logger() | ||
if prettify { | ||
logger = logger.Output(zerolog.ConsoleWriter{Out: os.Stderr}) | ||
} | ||
return logger | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.