Skip to content

Commit

Permalink
refactor(logger): minor tweaks
Browse files Browse the repository at this point in the history
- only write debug logs to file and not on the terminal
- remove stack trace for error logging
  • Loading branch information
EverythingSuckz committed Dec 13, 2023
1 parent 42a9c36 commit 5b5848b
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 8 deletions.
3 changes: 1 addition & 2 deletions cmd/fsb/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func runApp(cmd *cobra.Command, args []string) {

mainBot, err := bot.StartClient(log)
if err != nil {
log.Info(err.Error())
return
log.Panic("Failed to start main bot", zap.Error(err))
}
cache.InitCache(log)
workers, err := bot.StartWorkers(log)
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *config) loadFromEnvFile(log *zap.Logger) {
err := godotenv.Load(envPath)
if err != nil {
if os.IsNotExist(err) {
log.WithOptions(zap.AddStacktrace(zap.DPanicLevel)).Sugar().Errorf("ENV file not found: %s", envPath)
log.Sugar().Errorf("ENV file not found: %s", envPath)
log.Sugar().Info("Please create fsb.env file")
log.Sugar().Info("For more info, refer: https://github.com/EverythingSuckz/TG-FileStreamBot/tree/golang#setting-up-things")
log.Sugar().Info("Please ignore this message if you are hosting it in a service like Heroku or other alternatives.")
Expand Down
2 changes: 1 addition & 1 deletion internal/routes/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func getStreamRoute(ctx *gin.Context) {
}
lr, _ := utils.NewTelegramReader(ctx, worker.Client, file.Location, start, end, contentLength)
if _, err := io.CopyN(w, lr, contentLength); err != nil {
log.WithOptions(zap.AddStacktrace(zap.DPanicLevel)).Error(err.Error())
log.Error("Error while copying stream", zap.Error(err))
}
}
}
7 changes: 3 additions & 4 deletions internal/utils/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func InitLogger() {
consoleConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
consoleConfig.EncodeTime = customTimeEncoder
consoleEncoder := zapcore.NewConsoleEncoder(consoleConfig)
defaultLogLevel := zapcore.DebugLevel

fileEncoderConfig := zap.NewProductionEncoderConfig()
fileEncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
Expand All @@ -34,9 +33,9 @@ func InitLogger() {
})

core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), defaultLogLevel),
zapcore.NewCore(fileEncoder, fileWriter, defaultLogLevel),
zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), zapcore.InfoLevel),
zapcore.NewCore(fileEncoder, fileWriter, zapcore.DebugLevel),
)

Logger = zap.New(core, zap.AddStacktrace(zapcore.ErrorLevel))
Logger = zap.New(core, zap.AddStacktrace(zapcore.FatalLevel))
}

0 comments on commit 5b5848b

Please sign in to comment.