Skip to content

Commit

Permalink
feat(logger): support settings
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Aug 5, 2022
1 parent 0a573c2 commit 5d259b8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
2 changes: 2 additions & 0 deletions app/bot/run/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"github.com/creasty/defaults"
"github.com/iyear/searchx/pkg/logger"
"github.com/iyear/searchx/pkg/storage"
"github.com/iyear/searchx/pkg/validator"
"github.com/spf13/viper"
Expand Down Expand Up @@ -51,5 +52,6 @@ type config struct {
Search struct {
PageSize int `mapstructure:"page_size" validate:"gte=1,lte=20" default:"10"`
} `mapstructure:"search"`
Log logger.Config `mapstructure:"log"`
} `mapstructure:"ctrl"`
}
19 changes: 10 additions & 9 deletions app/bot/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,41 @@ import (
"github.com/iyear/searchx/pkg/logger"
"github.com/iyear/searchx/pkg/storage"
tele "gopkg.in/telebot.v3"
"log"
"time"
)

func Run(cfg string) {
color.Blue(global.Logo)
color.Blue("Initializing...")

log := logger.Init()

if err := config.Init(cfg); err != nil {
log.Fatalw("init config failed", "err", err)
log.Fatalf("init config failed: %v", err)
}
color.Blue("Config loaded")

slog := logger.New(config.C.Ctrl.Log.Enable, "log/bot/latest.log", config.C.Ctrl.Log.Level)

if err := i18n.Init(config.C.Ctrl.I18N); err != nil {
log.Fatalw("init i18n templates failed", "err", err)
slog.Fatalw("init i18n templates failed", "err", err)
}
color.Blue("I18n templates loaded")

_kv, err := storage.NewKV(config.C.Storage.KV.Driver, config.C.Storage.KV.Options)
if err != nil {
log.Fatalw("init kv database failed", "err", err, "options", config.C.Storage.KV.Options)
slog.Fatalw("init kv database failed", "err", err, "options", config.C.Storage.KV.Options)
}
color.Blue("KV database initialized")

_search, err := storage.NewSearch(config.C.Storage.Search.Driver, config.C.Storage.Search.Options)
if err != nil {
log.Fatalw("init search engine database failed", "err", err, "options", config.C.Storage.Search.Options)
slog.Fatalw("init search engine database failed", "err", err, "options", config.C.Storage.Search.Options)
}
color.Blue("Search engine initialized")

_cache, err := storage.New(config.C.Storage.Cache.Driver, config.C.Storage.Cache.Options)
if err != nil {
log.Fatalw("init cache failed", "err", err, "options", config.C.Storage.Cache.Options)
slog.Fatalw("init cache failed", "err", err, "options", config.C.Storage.Cache.Options)
}
color.Blue("Cache initialized")

Expand All @@ -57,7 +58,7 @@ func Run(cfg string) {

bot, err := tele.NewBot(settings)
if err != nil {
log.Fatalw("create bot failed", "err", err)
slog.Fatalw("create bot failed", "err", err)
}

color.Blue("Bot: %s", bot.Me.Username)
Expand All @@ -68,7 +69,7 @@ func Run(cfg string) {
Search: _search,
Cache: _cache,
},
Log: log.Named("bot"),
Log: slog.Named("bot"),
}

bot.Use(middleware.SetScope(scope), middleware.AutoResponder())
Expand Down
6 changes: 6 additions & 0 deletions config/bot/config.full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ ctrl:
[YOUR_NOTICE_MESSAGE](https://www.google.com)
i18n: config/bot/i18n # i18n files directory, default: config/bot/i18n
default_language: zh-cn # default language, default: zh-cn
log:
enable: true # enable log, default: true
# log level
# one of: debug, info, warn, error, fatal
# default: info
level: info
search:
page_size: 10 # number of results per page(1-20), default: 10
storage:
Expand Down
6 changes: 6 additions & 0 deletions pkg/logger/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package logger

type Config struct {
Enable bool `mapstructure:"enable" default:"true"`
Level string `mapstructure:"level" validate:"oneof=debug info warn error fatal" default:"info"`
}
21 changes: 16 additions & 5 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"os"
)

func getLogWriter() zapcore.WriteSyncer {
func getLogWriter(filename string) zapcore.WriteSyncer {
lumberJackLogger := &lumberjack.Logger{
Filename: "./log/latest.log",
Filename: filename,
MaxSize: 5,
MaxBackups: 5,
MaxAge: 30,
Expand All @@ -25,9 +25,20 @@ func getEncoder() zapcore.Encoder {
return zapcore.NewConsoleEncoder(encoderConfig)
}

func Init() *zap.SugaredLogger {
writeSyncer := getLogWriter()
func New(enable bool, filename, level string) *zap.SugaredLogger {
if !enable {
return zap.NewNop().Sugar()
}
writeSyncer := getLogWriter(filename)
encoder := getEncoder()
core := zapcore.NewCore(encoder, writeSyncer, zap.DebugLevel)

levels := map[string]zapcore.Level{
"debug": zapcore.DebugLevel,
"info": zapcore.InfoLevel,
"warn": zapcore.WarnLevel,
"error": zapcore.ErrorLevel,
"fatal": zapcore.FatalLevel,
}
core := zapcore.NewCore(encoder, writeSyncer, levels[level])
return zap.New(core, zap.AddCaller()).Sugar()
}

0 comments on commit 5d259b8

Please sign in to comment.