fix organizer diagnostics and site integrations

This commit is contained in:
ShukeBta
2026-06-21 09:44:12 +08:00
parent 6e61c8b545
commit 7b881d031c
37 changed files with 2012 additions and 164 deletions
+43 -4
View File
@@ -11,7 +11,7 @@ import (
"github.com/ShukeBta/MediaStationGo/internal/config"
)
func TestProductionLoggerSplitsWarnAndErrorAndDropsInfo(t *testing.T) {
func TestProductionLoggerWritesConfiguredInfoToAppLogAndSplitsWarnError(t *testing.T) {
dir := t.TempDir()
cfg := &config.Config{}
cfg.App.DataDir = dir
@@ -26,11 +26,15 @@ func TestProductionLoggerSplitsWarnAndErrorAndDropsInfo(t *testing.T) {
if err != nil {
t.Fatal(err)
}
log.Info("info should be dropped")
log.Info("info should be stored")
log.Warn("warning only", zap.String("kind", "warn"))
log.Error("error only", zap.String("kind", "error"))
_ = log.Sync()
appBytes, err := os.ReadFile(filepath.Join(dir, "logs", "app.log"))
if err != nil {
t.Fatal(err)
}
warnBytes, err := os.ReadFile(filepath.Join(dir, "logs", "warn.log"))
if err != nil {
t.Fatal(err)
@@ -39,10 +43,16 @@ func TestProductionLoggerSplitsWarnAndErrorAndDropsInfo(t *testing.T) {
if err != nil {
t.Fatal(err)
}
appLog := string(appBytes)
warnLog := string(warnBytes)
errorLog := string(errorBytes)
if strings.Contains(warnLog, "info should be dropped") || strings.Contains(errorLog, "info should be dropped") {
t.Fatal("info log should not be written in production")
if !strings.Contains(appLog, "info should be stored") ||
!strings.Contains(appLog, "warning only") ||
!strings.Contains(appLog, "error only") {
t.Fatalf("app log should contain all enabled levels: %s", appLog)
}
if strings.Contains(warnLog, "info should be stored") || strings.Contains(errorLog, "info should be stored") {
t.Fatal("split warn/error logs should not contain info")
}
if !strings.Contains(warnLog, "warning only") || strings.Contains(warnLog, "error only") {
t.Fatalf("warn log not isolated: %s", warnLog)
@@ -52,6 +62,35 @@ func TestProductionLoggerSplitsWarnAndErrorAndDropsInfo(t *testing.T) {
}
}
func TestProductionLoggerDefaultsToWarnInAppLog(t *testing.T) {
dir := t.TempDir()
cfg := &config.Config{}
cfg.App.DataDir = dir
cfg.Logging.Format = "json"
cfg.Logging.OutputPath = filepath.Join(dir, "logs")
cfg.Logging.EnableRotation = true
log, err := newLogger(cfg)
if err != nil {
t.Fatal(err)
}
log.Info("info should stay quiet by default")
log.Warn("warning should be stored")
_ = log.Sync()
appBytes, err := os.ReadFile(filepath.Join(dir, "logs", "app.log"))
if err != nil {
t.Fatal(err)
}
appLog := string(appBytes)
if strings.Contains(appLog, "info should stay quiet by default") {
t.Fatalf("default logger should not store info: %s", appLog)
}
if !strings.Contains(appLog, "warning should be stored") {
t.Fatalf("default logger should store warn: %s", appLog)
}
}
func TestRotatingFileWriterCapsFileSize(t *testing.T) {
path := filepath.Join(t.TempDir(), "app.log")
writer, err := newRotatingFileWriter(path, config.LoggingConfig{
+15 -14
View File
@@ -329,15 +329,12 @@ func isFrontendLibraryRoute(path string) bool {
return true
}
// newLogger 根据 cfg.Logging 构建 Zap。此前 logging.level / logging.format
// 配置完全没有生效(固定 NewProduction),用户无法在生产环境降低日志量;
// 配合每请求一条 INFO 访问日志,几小时即可产生几十 MB 日志,在 Docker
// json-file 驱动下持续消耗磁盘 IO。
// newLogger 根据 cfg.Logging 构建 Zap。
func newLogger(cfg *config.Config) (*zap.Logger, error) {
if cfg.App.Debug {
return zap.NewDevelopment()
}
level := productionLogLevel(cfg.Logging.Level)
level := configuredLogLevel(cfg.Logging.Level)
encoderCfg := zap.NewProductionEncoderConfig()
encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
var encoder zapcore.Encoder
@@ -349,7 +346,14 @@ func newLogger(cfg *config.Config) (*zap.Logger, error) {
cores := []zapcore.Core{
zapcore.NewCore(encoder, zapcore.Lock(os.Stdout), level),
}
warnPath, errorPath := logFilePaths(cfg)
appPath, warnPath, errorPath := logFilePaths(cfg)
if appPath != "" {
appWriter, err := newRotatingFileWriter(appPath, cfg.Logging)
if err != nil {
return nil, err
}
cores = append(cores, zapcore.NewCore(encoder, appWriter, level))
}
if warnPath != "" {
warnWriter, err := newRotatingFileWriter(warnPath, cfg.Logging)
if err != nil {
@@ -371,7 +375,7 @@ func newLogger(cfg *config.Config) (*zap.Logger, error) {
return zap.New(zapcore.NewTee(cores...), zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel), zap.ErrorOutput(zapcore.Lock(os.Stderr))), nil
}
func productionLogLevel(raw string) zapcore.Level {
func configuredLogLevel(raw string) zapcore.Level {
level := zapcore.WarnLevel
raw = strings.TrimSpace(raw)
if raw != "" {
@@ -380,25 +384,22 @@ func productionLogLevel(raw string) zapcore.Level {
level = parsed
}
}
if level < zapcore.WarnLevel {
return zapcore.WarnLevel
}
return level
}
func logFilePaths(cfg *config.Config) (string, string) {
func logFilePaths(cfg *config.Config) (string, string, string) {
out := strings.TrimSpace(cfg.Logging.OutputPath)
if strings.EqualFold(out, "stdout") || strings.EqualFold(out, "stderr") {
return "", ""
return "", "", ""
}
if out == "" {
out = filepath.Join(cfg.App.DataDir, "logs")
}
if ext := filepath.Ext(out); ext != "" {
base := strings.TrimSuffix(out, ext)
return base + ".warn" + ext, base + ".error" + ext
return out, base + ".warn" + ext, base + ".error" + ext
}
return filepath.Join(out, "warn.log"), filepath.Join(out, "error.log")
return filepath.Join(out, "app.log"), filepath.Join(out, "warn.log"), filepath.Join(out, "error.log")
}
// getLocalIP returns the first non-loopback IPv4 address of the machine.