diff --git a/.gitignore b/.gitignore index 069abb2b..76a57e5a 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,8 @@ go.work sessons/ # build folder -dist/ \ No newline at end of file +dist/ + +# logs folder +logs/ +*.log \ No newline at end of file diff --git a/bot/client.go b/bot/client.go index 333d1c83..9a0c14ec 100644 --- a/bot/client.go +++ b/bot/client.go @@ -3,13 +3,14 @@ package bot import ( "EverythingSuckz/fsb/commands" "EverythingSuckz/fsb/config" - "log" + + "go.uber.org/zap" "github.com/celestix/gotgproto" "github.com/celestix/gotgproto/sessionMaker" ) -func StartClient() (*gotgproto.Client, error) { +func StartClient(log *zap.Logger) (*gotgproto.Client, error) { client, err := gotgproto.NewClient( int(config.ValueOf.ApiID), config.ValueOf.ApiHash, @@ -24,7 +25,7 @@ func StartClient() (*gotgproto.Client, error) { if err != nil { return nil, err } - commands.Load(client.Dispatcher) - log.Println("Client started") + commands.Load(log, client.Dispatcher) + log.Info("Client started", zap.String("username", client.Self.Username)) return client, nil } diff --git a/commands/commands.go b/commands/commands.go index 0d55fd62..1dcc5cf7 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -1,19 +1,20 @@ package commands import ( - "log" "reflect" "github.com/celestix/gotgproto/dispatcher" + "go.uber.org/zap" ) type command struct { + log *zap.Logger } -func Load(dispatcher dispatcher.Dispatcher) { - defer log.Println("Initialized") - Type := reflect.TypeOf(&command{}) - Value := reflect.ValueOf(&command{}) +func Load(log *zap.Logger, dispatcher dispatcher.Dispatcher) { + defer log.Info("Initialized all command handlers") + Type := reflect.TypeOf(&command{log}) + Value := reflect.ValueOf(&command{log}) for i := 0; i < Type.NumMethod(); i++ { Type.Method(i).Func.Call([]reflect.Value{Value, reflect.ValueOf(dispatcher)}) } diff --git a/commands/start.go b/commands/start.go index 0a1cf2f0..22058e58 100644 --- a/commands/start.go +++ b/commands/start.go @@ -1,15 +1,13 @@ package commands import ( - "log" - "github.com/celestix/gotgproto/dispatcher" "github.com/celestix/gotgproto/dispatcher/handlers" "github.com/celestix/gotgproto/ext" ) func (m *command) LoadStart(dispatcher dispatcher.Dispatcher) { - defer log.Println("Loaded start command") + defer m.log.Sugar().Info("Loaded start command") dispatcher.AddHandler(handlers.NewCommand("start", start)) } diff --git a/config/config.go b/config/config.go index 10d30c58..74b3d036 100644 --- a/config/config.go +++ b/config/config.go @@ -1,12 +1,12 @@ package config import ( - "log" "strconv" "strings" _ "github.com/joho/godotenv/autoload" "github.com/kelseyhightower/envconfig" + "go.uber.org/zap" ) var ValueOf = &config{} @@ -27,17 +27,18 @@ func (c *config) setupEnvVars() { } } -func Load() { +func Load(log *zap.Logger) { ValueOf.setupEnvVars() - ValueOf.LogChannelID = int64(stripInt(int(ValueOf.LogChannelID))) - log.Println("Loaded config") + ValueOf.LogChannelID = int64(stripInt(log, int(ValueOf.LogChannelID))) + log.Info("Loaded config") } -func stripInt(a int) int { + +func stripInt(log *zap.Logger, a int) int { strA := strconv.Itoa(abs(a)) lastDigits := strings.Replace(strA, "100", "", 1) result, err := strconv.Atoi(lastDigits) if err != nil { - log.Println(err) + log.Sugar().Fatalln(err) return 0 } return result diff --git a/go.mod b/go.mod index 7cc05599..94b305d5 100644 --- a/go.mod +++ b/go.mod @@ -60,5 +60,6 @@ require ( golang.org/x/sys v0.14.0 // indirect golang.org/x/text v0.14.0 // indirect google.golang.org/protobuf v1.30.0 // indirect + gopkg.in/natefinch/lumberjack.v2 v2.2.1 gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 5db0ee49..163c2ab5 100644 --- a/go.sum +++ b/go.sum @@ -138,6 +138,8 @@ google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 55653d3c..89657242 100644 --- a/main.go +++ b/main.go @@ -1,18 +1,20 @@ package main import ( + "EverythingSuckz/fsb/bot" "EverythingSuckz/fsb/config" "EverythingSuckz/fsb/routes" "EverythingSuckz/fsb/types" "EverythingSuckz/fsb/utils" - "EverythingSuckz/fsb/bot" "fmt" - "log" "net/http" + "os" "time" "github.com/gin-gonic/gin" "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "gopkg.in/natefinch/lumberjack.v2" ) const versionString = "v0.0.1" @@ -20,34 +22,58 @@ const versionString = "v0.0.1" var startTime time.Time = time.Now() func main() { - log.Println("Starting server...") - config.Load() - router := getRouter() + log := initLogger() + log.Info("Starting server...") + config.Load(log) + router := getRouter(log) - _, err := bot.StartClient() + _, err := bot.StartClient(log) if err != nil { - log.Println(err) + log.Info(err.Error()) return } - log.Printf("Server started at: http://localhost:%d\n", config.ValueOf.Port) + log.Info("Server started", zap.Int("port", config.ValueOf.Port)) + log.Info("File Stream Bot", zap.String("version", versionString)) err = router.Run(fmt.Sprintf(":%d", config.ValueOf.Port)) if err != nil { - log.Println(err) + log.Sugar().Fatalln(err) } } -// TODO: Use zap logger func initLogger() *zap.Logger { - logger, err := zap.NewProduction() - if err != nil { - log.Fatalf("can't initialize zap logger: %v", err) + customTimeEncoder := func(t time.Time, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString(t.Format("02/01/2006 03:04 PM")) } - defer logger.Sync() + consoleConfig := zap.NewDevelopmentEncoderConfig() + consoleConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder + consoleConfig.EncodeTime = customTimeEncoder + consoleEncoder := zapcore.NewConsoleEncoder(consoleConfig) + defaultLogLevel := zapcore.DebugLevel + + fileEncoderConfig := zap.NewProductionEncoderConfig() + fileEncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder + fileEncoder := zapcore.NewJSONEncoder(fileEncoderConfig) + + fileWriter := zapcore.AddSync(&lumberjack.Logger{ + Filename: "logs/app.log", + MaxSize: 10, + MaxBackups: 3, + MaxAge: 7, + Compress: true, + }) + + core := zapcore.NewTee( + zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), defaultLogLevel), + zapcore.NewCore(fileEncoder, fileWriter, defaultLogLevel), + ) + + logger := zap.New(core, zap.AddStacktrace(zapcore.ErrorLevel)) return logger + } -func getRouter() *gin.Engine { +func getRouter(log *zap.Logger) *gin.Engine { if config.ValueOf.Dev { gin.SetMode(gin.DebugMode) } else { @@ -63,6 +89,6 @@ func getRouter() *gin.Engine { Version: versionString, }) }) - routes.Load(router) + routes.Load(log, router) return router } diff --git a/routes/routes.go b/routes/routes.go index 610d1ebf..0e242cb8 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -1,10 +1,10 @@ package routes import ( - "log" "reflect" "github.com/gin-gonic/gin" + "go.uber.org/zap" ) type Route struct { @@ -17,14 +17,15 @@ func (r *Route) Init(engine *gin.Engine) { } type allRoutes struct { + log *zap.Logger } -func Load(r *gin.Engine) { - defer log.Println("Loaded all API Routes") +func Load(log *zap.Logger, r *gin.Engine) { + defer log.Sugar().Info("Loaded all API Routes") route := &Route{Name: "/", Engine: r} route.Init(r) - Type := reflect.TypeOf(&allRoutes{}) - Value := reflect.ValueOf(&allRoutes{}) + Type := reflect.TypeOf(&allRoutes{log}) + Value := reflect.ValueOf(&allRoutes{log}) for i := 0; i < Type.NumMethod(); i++ { Type.Method(i).Func.Call([]reflect.Value{Value, reflect.ValueOf(route)}) }