This commit is contained in:
Anatoly Prohacky 2023-04-25 20:14:01 +10:00
parent 20d0a2bddd
commit d596cab3fd
3 changed files with 83 additions and 2 deletions

2
.gitignore vendored
View File

@ -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

37
cmd/apisirius/config.go Normal file
View File

@ -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
}

46
cmd/apisirius/logger.go Normal file
View File

@ -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
}