update cmd root.

This commit is contained in:
Miroslav Šedivý 2021-09-26 21:29:56 +02:00
parent d91c0a9f7f
commit e623a15771

View File

@ -34,28 +34,26 @@ func init() {
////// //////
// logs // logs
////// //////
zerolog.TimeFieldFormat = "" console := zerolog.ConsoleWriter{Out: os.Stdout}
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.SetGlobalLevel(zerolog.InfoLevel) zerolog.SetGlobalLevel(zerolog.InfoLevel)
console := zerolog.ConsoleWriter{Out: os.Stdout} logs := viper.GetBool("logs")
if !logs {
if !viper.GetBool("logs") {
log.Logger = log.Output(console) log.Logger = log.Output(console)
} else { } else {
logs := filepath.Join(".", "logs") logsPath := filepath.Join(".", "logs")
if runtime.GOOS == "linux" { if runtime.GOOS == "linux" {
logs = "/var/log/neko" logsPath = "/var/log/neko"
} }
if _, err := os.Stat(logs); os.IsNotExist(err) { if _, err := os.Stat(logsPath); os.IsNotExist(err) {
//nolint _ = os.Mkdir(logsPath, os.ModePerm)
os.Mkdir(logs, os.ModePerm)
} }
latest := filepath.Join(logs, "neko-latest.log") latest := filepath.Join(logsPath, "neko-latest.log")
_, err := os.Stat(latest) if _, err := os.Stat(latest); err == nil {
if err == nil { err = os.Rename(latest, filepath.Join(logsPath, "neko."+time.Now().Format("2006-01-02T15-04-05Z07-00")+".log"))
err = os.Rename(latest, filepath.Join(logs, "neko."+time.Now().Format("2006-01-02T15-04-05Z07-00")+".log"))
if err != nil { if err != nil {
log.Panic().Err(err).Msg("failed to rotate log file") log.Panic().Err(err).Msg("failed to rotate log file")
} }
@ -96,14 +94,13 @@ func init() {
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv() // read in environment variables that match viper.AutomaticEnv() // read in environment variables that match
if err := viper.ReadInConfig(); err != nil { err := viper.ReadInConfig()
if _, ok := err.(viper.ConfigFileNotFoundError); !ok { if err != nil && config != "" {
log.Err(err) log.Err(err)
} }
if config != "" {
log.Err(err) // get full config file path
} config = viper.ConfigFileUsed()
}
////// //////
// debug // debug
@ -113,20 +110,19 @@ func init() {
zerolog.SetGlobalLevel(zerolog.DebugLevel) zerolog.SetGlobalLevel(zerolog.DebugLevel)
} }
file := viper.ConfigFileUsed()
logger := log.With(). logger := log.With().
Bool("debug", debug). Bool("debug", debug).
Str("logging", viper.GetString("logs")). Bool("logs", logs).
Str("config", file). Str("config", config).
Logger() Logger()
if file == "" { if config == "" {
logger.Warn().Msg("preflight complete without config file") logger.Warn().Msg("preflight complete without config file")
} else { } else {
if _, err := os.Stat(file); os.IsNotExist(err) { if _, err := os.Stat(config); os.IsNotExist(err) {
logger.Err(err).Msg("preflight complete with nonexistent config file") logger.Err(err).Msg("preflight complete with nonexistent config file")
} else { } else {
logger.Info().Msg("preflight complete") logger.Info().Msg("preflight complete with config file")
} }
} }