feat: add configurable loglevel
This commit is contained in:
+9
-6
@@ -9,23 +9,26 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// grab the environment variables from the runtime environment
|
||||||
|
env := utility.GrabEnvironment()
|
||||||
// configure the logger so we have nice json
|
// configure the logger so we have nice json
|
||||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
level := utility.ParseSlogLevel(env.LogLevel) // defaults to Info
|
||||||
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
||||||
|
Level: &level,
|
||||||
|
}))
|
||||||
slog.SetDefault(logger)
|
slog.SetDefault(logger)
|
||||||
|
|
||||||
// grab the environment variables from the runtime environment
|
// print our running environment variable set
|
||||||
slog.Info("grabbing environment variables")
|
slog.Debug("environment", "env", env)
|
||||||
env := utility.GrabEnvironment()
|
|
||||||
|
|
||||||
// TO DO, allow cmd args parsing
|
// TO DO, allow cmd args parsing
|
||||||
|
|
||||||
// checking directories to ensure its expected environment is ready
|
// checking directories to ensure its expected environment is ready
|
||||||
slog.Info("auditing operating environment")
|
slog.Debug("checking if directories are present")
|
||||||
if err := utility.EnsureOperation(env.DataDirectory); err != nil {
|
if err := utility.EnsureOperation(env.DataDirectory); err != nil {
|
||||||
slog.Error("failed to ensure the operating environment", "error", err)
|
slog.Error("failed to ensure the operating environment", "error", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
slog.Info("finished audit of environment")
|
|
||||||
|
|
||||||
// initiating the database connection for which we safe things
|
// initiating the database connection for which we safe things
|
||||||
slog.Info("kicking off database connection")
|
slog.Info("kicking off database connection")
|
||||||
|
|||||||
+1
-1
@@ -29,7 +29,7 @@ func KickoffApi(logger *slog.Logger, env utility.Environment, db *gorm.DB) {
|
|||||||
// r := gin.Default()
|
// r := gin.Default()
|
||||||
// JSON logger: https://gin-gonic.com/en/docs/logging/structured-logging/
|
// JSON logger: https://gin-gonic.com/en/docs/logging/structured-logging/
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
r.Use(slogMiddleware(logger))
|
r.Use(slogGinMiddleware(logger))
|
||||||
r.Use(gin.Recovery())
|
r.Use(gin.Recovery())
|
||||||
|
|
||||||
api := r.Group("/api")
|
api := r.Group("/api")
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func slogMiddleware(logger *slog.Logger) gin.HandlerFunc {
|
func slogGinMiddleware(logger *slog.Logger) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
path := c.Request.URL.Path
|
path := c.Request.URL.Path
|
||||||
@@ -15,7 +15,7 @@ func slogMiddleware(logger *slog.Logger) gin.HandlerFunc {
|
|||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|
||||||
logger.Info("request",
|
logger.Debug("request",
|
||||||
slog.String("method", c.Request.Method),
|
slog.String("method", c.Request.Method),
|
||||||
slog.String("path", path),
|
slog.String("path", path),
|
||||||
slog.String("query", query),
|
slog.String("query", query),
|
||||||
|
|||||||
@@ -72,6 +72,7 @@ func spawnFileRoutes(file *gin.RouterGroup, env utility.Environment, db *gorm.DB
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Info("saved file to local filesystem and database")
|
||||||
c.JSON(http.StatusCreated, RespObj{
|
c.JSON(http.StatusCreated, RespObj{
|
||||||
Msg: "file has succesfully been uploaded",
|
Msg: "file has succesfully been uploaded",
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func CategorizeMediaType(ext string) (MediaType, bool) {
|
|||||||
case ".pptx", ".ppt", ".key", ".odp":
|
case ".pptx", ".ppt", ".key", ".odp":
|
||||||
return Presentation, true
|
return Presentation, true
|
||||||
default:
|
default:
|
||||||
slog.Debug("marking file as invalid undefined extension")
|
slog.Debug("marking file as invalid due to its undefined extension")
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func databaseGather(db *gorm.DB) (map[string]File, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func watchdog(env utility.Environment, db *gorm.DB) {
|
func watchdog(env utility.Environment, db *gorm.DB) {
|
||||||
slog.Info("performing the watchdog cycle")
|
slog.Debug("performing the watchdog cycle")
|
||||||
|
|
||||||
fsSet, err := filesystemGather(env)
|
fsSet, err := filesystemGather(env)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -102,7 +102,7 @@ func watchdog(env utility.Environment, db *gorm.DB) {
|
|||||||
case "strict":
|
case "strict":
|
||||||
err := utility.RemoveFile(fp)
|
err := utility.RemoveFile(fp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Warn("failed to remove local file from the filesystem", "error", err)
|
slog.Error("failed to remove local file from the filesystem", "error", err)
|
||||||
}
|
}
|
||||||
case "dry":
|
case "dry":
|
||||||
slog.Debug("dry mode enabled, not purging", "filepath", fp)
|
slog.Debug("dry mode enabled, not purging", "filepath", fp)
|
||||||
|
|||||||
@@ -6,15 +6,19 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Environment struct {
|
type Environment struct {
|
||||||
Version string
|
Version string
|
||||||
Codename string
|
Codename string
|
||||||
|
LogLevel string
|
||||||
|
|
||||||
DataDirectory string
|
DataDirectory string
|
||||||
ContentDirectory string
|
ContentDirectory string
|
||||||
Hostname string
|
Hostname string
|
||||||
Port int
|
Port int
|
||||||
|
|
||||||
WatchdogEnabled bool
|
WatchdogEnabled bool
|
||||||
WatchdogInterval int
|
WatchdogInterval int
|
||||||
WatchdogSyncMode string
|
WatchdogSyncMode string
|
||||||
@@ -33,7 +37,6 @@ func safeStringGrab(key, fallback string) string {
|
|||||||
if v, ok := os.LookupEnv(key); ok {
|
if v, ok := os.LookupEnv(key); ok {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
slog.Debug("using fallback", "key", key, "fallback", fallback)
|
|
||||||
return fallback
|
return fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +46,6 @@ func safeIntGrab(key string, fallback int) int {
|
|||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
slog.Debug("using fallback", "key", key, "fallback", fallback)
|
|
||||||
return fallback
|
return fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +64,6 @@ func safeSyncModeGrab(key, fallback string) string {
|
|||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
slog.Debug("using fallback", "key", key, "fallback", fallback)
|
|
||||||
return fallback
|
return fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,6 +80,7 @@ func GrabEnvironment() Environment {
|
|||||||
// Basic server configuration
|
// Basic server configuration
|
||||||
Version: safeStringGrab("VERSION", "0.0.1"),
|
Version: safeStringGrab("VERSION", "0.0.1"),
|
||||||
Codename: safeStringGrab("CODENAME", "Magical Anomaly"),
|
Codename: safeStringGrab("CODENAME", "Magical Anomaly"),
|
||||||
|
LogLevel: safeStringGrab("LOG_LEVEL", "info"),
|
||||||
|
|
||||||
// GIN API configuration
|
// GIN API configuration
|
||||||
DataDirectory: safeStringGrab("DATA_DIR", fbBase),
|
DataDirectory: safeStringGrab("DATA_DIR", fbBase),
|
||||||
@@ -96,3 +98,18 @@ func GrabEnvironment() Environment {
|
|||||||
WatchdogSyncMode: safeStringGrab("WATCHDOG_SYNC_MODE", "strict"),
|
WatchdogSyncMode: safeStringGrab("WATCHDOG_SYNC_MODE", "strict"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ParseSlogLevel(s string) slog.Level {
|
||||||
|
switch strings.ToLower(s) {
|
||||||
|
case "debug":
|
||||||
|
return slog.LevelDebug
|
||||||
|
case "warn", "warning":
|
||||||
|
return slog.LevelWarn
|
||||||
|
case "error":
|
||||||
|
return slog.LevelError
|
||||||
|
case "info":
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
return slog.LevelInfo
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user