Refactor logging to use slog package across the application

- Replaced standard log package with log/slog in httpclient, nginx manager, sync service, updater, and other components for structured logging.
- Introduced environment variable `LOG_LEVEL` to control logging levels (debug, info, warn, error).
- Updated documentation to reflect changes in logging configuration and requirements.
- Added a new logging setup function in the ats_agent internal package to initialize the slog logger.
This commit is contained in:
ryan
2026-03-13 14:53:51 +08:00
parent 06d4831d55
commit aeb7118b30
17 changed files with 321 additions and 209 deletions
+23 -23
View File
@@ -4,7 +4,7 @@ import (
"context"
"crypto/sha256"
"encoding/hex"
"log"
"log/slog"
"strings"
"atsflare-agent/internal/protocol"
@@ -70,13 +70,13 @@ func (s *Service) sync(ctx context.Context, startup bool, target *protocol.Activ
if target == nil || target.Version == "" || target.Checksum == "" {
if !startup {
log.Printf("skipping sync because heartbeat returned no active config summary: mode=%s", mode)
slog.Debug("skipping sync because heartbeat returned no active config summary", "mode", mode)
return nil
}
log.Printf("sync startup fallback: active config summary unavailable, fetching active config directly")
slog.Info("sync startup fallback: active config summary unavailable, fetching active config directly")
config, fetchErr := s.client.GetActiveConfig(ctx)
if fetchErr != nil {
log.Printf("fetch active config failed: mode=%s error=%v", mode, fetchErr)
slog.Error("fetch active config failed", "mode", mode, "error", fetchErr)
return fetchErr
}
target = &protocol.ActiveConfigMeta{
@@ -87,33 +87,33 @@ func (s *Service) sync(ctx context.Context, startup bool, target *protocol.Activ
}
if currentChecksum == target.Checksum {
log.Printf("local openresty config already up to date: mode=%s version=%s", mode, target.Version)
slog.Info("local openresty config already up to date", "mode", mode, "version", target.Version)
if startup {
log.Printf("ensuring openresty runtime on startup: version=%s", target.Version)
slog.Info("ensuring openresty runtime on startup", "version", target.Version)
if err = s.nginxManager.EnsureRuntime(ctx, true); err != nil {
snapshot.OpenrestyStatus = protocol.OpenrestyStatusUnhealthy
snapshot.OpenrestyMessage = err.Error()
_ = s.stateStore.Save(snapshot)
return err
}
log.Printf("openresty runtime ensured on startup: version=%s", target.Version)
slog.Info("openresty runtime ensured on startup", "version", target.Version)
snapshot.OpenrestyStatus = protocol.OpenrestyStatusHealthy
snapshot.OpenrestyMessage = ""
}
snapshot.CurrentVersion = target.Version
snapshot.CurrentChecksum = target.Checksum
snapshot.LastError = ""
log.Printf("sync finished without changes: mode=%s version=%s", mode, target.Version)
slog.Info("sync finished without changes", "mode", mode, "version", target.Version)
return s.stateStore.Save(snapshot)
}
if snapshot.CurrentVersion == target.Version && snapshot.CurrentChecksum == target.Checksum && !startup {
log.Printf("skipping config fetch because state already records target version/checksum: version=%s checksum=%s", target.Version, target.Checksum)
slog.Debug("skipping config fetch because state already records target version/checksum", "version", target.Version, "checksum", target.Checksum)
return nil
}
config, err := s.client.GetActiveConfig(ctx)
if err != nil {
log.Printf("fetch active config failed: mode=%s error=%v", mode, err)
slog.Error("fetch active config failed", "mode", mode, "error", err)
return err
}
return s.applyIfNeeded(ctx, mode, startup, snapshot, currentChecksum, target, config)
@@ -121,30 +121,30 @@ func (s *Service) sync(ctx context.Context, startup bool, target *protocol.Activ
func (s *Service) applyIfNeeded(ctx context.Context, mode string, startup bool, snapshot *state.Snapshot, currentChecksum string, target *protocol.ActiveConfigMeta, config *protocol.ActiveConfigResponse) error {
if currentChecksum == config.Checksum {
log.Printf("local openresty config already up to date: mode=%s version=%s", mode, config.Version)
slog.Info("local openresty config already up to date", "mode", mode, "version", config.Version)
if startup {
log.Printf("ensuring openresty runtime on startup: version=%s", config.Version)
slog.Info("ensuring openresty runtime on startup", "version", config.Version)
if err := s.nginxManager.EnsureRuntime(ctx, true); err != nil {
snapshot.OpenrestyStatus = protocol.OpenrestyStatusUnhealthy
snapshot.OpenrestyMessage = err.Error()
_ = s.stateStore.Save(snapshot)
return err
}
log.Printf("openresty runtime ensured on startup: version=%s", config.Version)
slog.Info("openresty runtime ensured on startup", "version", config.Version)
snapshot.OpenrestyStatus = protocol.OpenrestyStatusHealthy
snapshot.OpenrestyMessage = ""
}
snapshot.CurrentVersion = config.Version
snapshot.CurrentChecksum = config.Checksum
snapshot.LastError = ""
log.Printf("sync finished without changes: mode=%s version=%s", mode, config.Version)
slog.Info("sync finished without changes", "mode", mode, "version", config.Version)
return s.stateStore.Save(snapshot)
}
if target != nil && (target.Version != config.Version || target.Checksum != config.Checksum) {
log.Printf("active config changed between heartbeat and fetch: heartbeat_version=%s heartbeat_checksum=%s fetched_version=%s fetched_checksum=%s", target.Version, target.Checksum, config.Version, config.Checksum)
slog.Warn("active config changed between heartbeat and fetch", "heartbeat_version", target.Version, "heartbeat_checksum", target.Checksum, "fetched_version", config.Version, "fetched_checksum", config.Checksum)
}
if snapshot.CurrentVersion == config.Version && snapshot.CurrentChecksum == config.Checksum && !startup {
log.Printf("skipping apply because state already records target version/checksum: version=%s checksum=%s", config.Version, config.Checksum)
slog.Debug("skipping apply because state already records target version/checksum", "version", config.Version, "checksum", config.Checksum)
return nil
}
routeConfig := config.RouteConfig
@@ -153,9 +153,9 @@ func (s *Service) applyIfNeeded(ctx context.Context, mode string, startup bool,
}
mainConfigChecksum := checksumString(config.MainConfig)
routeConfigChecksum := checksumString(routeConfig)
log.Printf("applying new openresty config: mode=%s from_version=%s to_version=%s old_checksum=%s new_checksum=%s", mode, snapshot.CurrentVersion, config.Version, currentChecksum, config.Checksum)
slog.Info("applying new openresty config", "mode", mode, "from_version", snapshot.CurrentVersion, "to_version", config.Version, "old_checksum", currentChecksum, "new_checksum", config.Checksum)
if err := s.nginxManager.Apply(ctx, config.MainConfig, routeConfig, config.SupportFiles); err != nil {
log.Printf("apply openresty config failed: mode=%s version=%s error=%v", mode, config.Version, err)
slog.Error("apply openresty config failed", "mode", mode, "version", config.Version, "error", err)
snapshot.LastError = err.Error()
snapshot.OpenrestyStatus = protocol.OpenrestyStatusUnhealthy
snapshot.OpenrestyMessage = err.Error()
@@ -171,13 +171,13 @@ func (s *Service) applyIfNeeded(ctx context.Context, mode string, startup bool,
SupportFileCount: len(config.SupportFiles),
})
if reportErr != nil {
log.Printf("report failed apply log failed: version=%s error=%v", config.Version, reportErr)
slog.Error("report failed apply log failed", "version", config.Version, "error", reportErr)
return reportErr
}
log.Printf("failed apply log reported: version=%s", config.Version)
slog.Warn("failed apply log reported", "version", config.Version)
return err
}
log.Printf("openresty config applied successfully: mode=%s version=%s", mode, config.Version)
slog.Info("openresty config applied successfully", "mode", mode, "version", config.Version)
snapshot.CurrentVersion = config.Version
snapshot.CurrentChecksum = config.Checksum
snapshot.LastError = ""
@@ -196,10 +196,10 @@ func (s *Service) applyIfNeeded(ctx context.Context, mode string, startup bool,
RouteConfigChecksum: routeConfigChecksum,
SupportFileCount: len(config.SupportFiles),
}); err != nil {
log.Printf("report successful apply log failed: version=%s error=%v", config.Version, err)
slog.Error("report successful apply log failed", "version", config.Version, "error", err)
return err
}
log.Printf("successful apply log reported: version=%s", config.Version)
slog.Info("successful apply log reported", "version", config.Version)
return nil
}