feat: added more utilities functions

This commit is contained in:
2024-11-03 20:57:03 +01:00
parent c90eedf430
commit 16b74bb80d
10 changed files with 542 additions and 1 deletions
+59
View File
@@ -0,0 +1,59 @@
package config
import (
"fmt"
"gopher-toolbox/token"
"io"
"log/slog"
"os"
"time"
)
type App struct {
DataSource string
UseCache bool
Security Security
AppInfo AppInfo
}
type AppInfo struct {
GinMode string
Version string
}
type Security struct {
Token *token.Paseto
StripeKey string
Duration time.Duration
}
func NewLogger(level slog.Level) {
now := time.Now().Format("2006-01-02")
if _, err := os.Stat("logs"); os.IsNotExist(err) {
os.Mkdir("logs", 0755)
}
f, _ := os.OpenFile(fmt.Sprintf("logs/log%s.log", now), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
mw := io.MultiWriter(os.Stdout, f)
logger := slog.New(slog.NewTextHandler(mw, &slog.HandlerOptions{
AddSource: true,
Level: level,
}))
slog.SetDefault(logger)
}
func LogLevel(level string) slog.Level {
switch level {
case "debug":
return slog.LevelDebug
case "info":
return slog.LevelInfo
case "warn":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
return slog.LevelDebug
}
}