From d596cab3fd56bae1dc8e540e1ee3c234dee7bf46 Mon Sep 17 00:00:00 2001 From: Anatoly Prohacky Date: Tue, 25 Apr 2023 20:14:01 +1000 Subject: [PATCH] config --- .gitignore | 2 -- cmd/apisirius/config.go | 37 +++++++++++++++++++++++++++++++++ cmd/apisirius/logger.go | 46 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 cmd/apisirius/config.go create mode 100644 cmd/apisirius/logger.go diff --git a/.gitignore b/.gitignore index 389e9de..9c4d582 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,6 @@ !.vscode/launch.json !.vscode/extensions.json !.vscode/*.code-snippets -!main.go # Local History for Visual Studio Code .history/ @@ -19,7 +18,6 @@ log/* *.dll *.so *.dylib -apisirius # Test binary, built with `go test -c` *.test diff --git a/cmd/apisirius/config.go b/cmd/apisirius/config.go new file mode 100644 index 0000000..ef3b815 --- /dev/null +++ b/cmd/apisirius/config.go @@ -0,0 +1,37 @@ +package main + +import ( + "flag" + + "github.com/spf13/viper" + "gitstore.ru/tolikproh/sirius/internal/model" +) + +var ( + configPath string + configFile string = "config" +) + +func init() { + flag.StringVar(&configPath, "conf-path", "configs", "path to config file 'config.yml'") + flag.StringVar(&configFile, "conf-file", "config", "name file to config") +} + +func initConfig() (*model.Config, error) { + cnf := model.NewConfig() + + viper.SetConfigName(configFile) + viper.SetConfigType("yaml") + viper.AddConfigPath(configPath) + viper.AddConfigPath("./") + + if err := viper.ReadInConfig(); err != nil { + return cnf, err + } else { + if err := viper.Unmarshal(&cnf); err != nil { + return cnf, err + } + } + + return cnf, nil +} diff --git a/cmd/apisirius/logger.go b/cmd/apisirius/logger.go new file mode 100644 index 0000000..99fcc68 --- /dev/null +++ b/cmd/apisirius/logger.go @@ -0,0 +1,46 @@ +package main + +import ( + "io" + + "github.com/sirupsen/logrus" + "github.com/snowzach/rotatefilehook" + "gitstore.ru/tolikproh/sirius/internal/model" +) + +func NewLogger(cfg *model.Config) *logrus.Logger { + + logFile := "sirius_server.log" + + log := logrus.New() + + logLevel := model.ValidLogLevel(cfg) + + log.SetLevel(logLevel) + + rotateFileHook, err := rotatefilehook.NewRotateFileHook(rotatefilehook.RotateFileConfig{ + Filename: cfg.Srv.LogPath + "/" + logFile, + MaxSize: 1, // megabytes + MaxBackups: 7, // amouts + MaxAge: 1, //days + Level: logLevel, + Formatter: &logrus.JSONFormatter{}, + }) + + if err != nil { + logrus.Fatalf("Failed to initialize file rotate hook: %v", err) + } + + //log.SetOutput(colorable.NewColorableStdout()) + log.SetOutput(io.Discard) + // log.SetFormatter(&logrus.TextFormatter{ + // PadLevelText: true, + // ForceColors: true, + // FullTimestamp: true, + // TimestampFormat: "2006-01-02 15:04:05", + // }) + + log.AddHook(rotateFileHook) + + return log +}