feat: integrate zerolog for structured logging and replace fmt with logger
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"s1d3sw1ped/SteamCache2/steamcache/avgcachestate"
|
||||
"s1d3sw1ped/SteamCache2/steamcache/logger"
|
||||
"s1d3sw1ped/SteamCache2/vfs"
|
||||
"s1d3sw1ped/SteamCache2/vfs/cache"
|
||||
"s1d3sw1ped/SteamCache2/vfs/cachestate"
|
||||
@@ -80,7 +81,7 @@ func New(address string, memorySize string, memoryMultiplier int, diskSize strin
|
||||
}
|
||||
|
||||
func (sc *SteamCache) Run() {
|
||||
fmt.Printf("SteamCache2 running on %s", sc.address)
|
||||
logger.Logger.Info().Str("address", sc.address).Msg("listening")
|
||||
|
||||
sc.mu.Lock()
|
||||
sc.dirty = true
|
||||
@@ -96,7 +97,11 @@ func (sc *SteamCache) Run() {
|
||||
|
||||
err := http.ListenAndServe(sc.address, sc)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
if err == http.ErrServerClosed {
|
||||
logger.Logger.Info().Msg("shutdown")
|
||||
return
|
||||
}
|
||||
logger.Logger.Error().Err(err).Msg("Failed to start SteamCache2")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -105,12 +110,11 @@ func (sc *SteamCache) LogStats() {
|
||||
sc.mu.Lock()
|
||||
defer sc.mu.Unlock()
|
||||
if sc.dirty {
|
||||
fmt.Printf(
|
||||
"%s: (%d) %s/%s %s: (%d) %s/%s Hitrate: %f%%",
|
||||
sc.memory.Name(), len(sc.memory.StatAll()), units.HumanSize(float64(sc.memory.Size())), units.HumanSize(float64(sc.memory.Capacity())),
|
||||
sc.disk.Name(), len(sc.disk.StatAll()), units.HumanSize(float64(sc.disk.Size())), units.HumanSize(float64(sc.disk.Capacity())),
|
||||
sc.hits.Avg()*100,
|
||||
)
|
||||
logger.Logger.Info().
|
||||
Str("memory", fmt.Sprintf("%s/%s", units.HumanSize(float64(sc.memory.Size())), units.HumanSize(float64(sc.memory.Capacity())))).Int("memory-files", len(sc.memory.StatAll())).
|
||||
Str("disk", fmt.Sprintf("%s/%s", units.HumanSize(float64(sc.disk.Size())), units.HumanSize(float64(sc.disk.Capacity())))).Int("disk-files", len(sc.disk.StatAll())).
|
||||
Str("hitrate", fmt.Sprintf("%.2f%%", sc.hits.Avg()*100)).
|
||||
Msg("stats")
|
||||
sc.dirty = false
|
||||
}
|
||||
}
|
||||
@@ -141,7 +145,7 @@ func (sc *SteamCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// tstart := time.Now()
|
||||
// defer func() {
|
||||
// fmt.Printf("%s %s %s took %s", r.Method, r.URL.String(), w.Header().Get("X-LanCache-Status"), time.Since(tstart))
|
||||
// logger.Logger.Info().Str("method", r.Method).Str("url", r.URL.String()).Str("status", w.Header().Get("X-LanCache-Status")).Dur("duration", time.Since(tstart)).Msg("Request")
|
||||
// }()
|
||||
|
||||
sc.mu.Lock()
|
||||
|
||||
Reference in New Issue
Block a user