package main

import (
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"gopkg.in/natefinch/lumberjack.v2"
)

func main() {
	// 1. 配置日志轮转
	logRotate := &lumberjack.Logger{
		Filename:   "./logs/app.log", // 日志文件路径
		MaxSize:    10,               // 每个日志文件最大 10MB
		MaxBackups: 5,                // 保留最近 5 个备份
		MaxAge:     30,               // 保留 30 天
		Compress:   true,             // 压缩旧日志
	}

	// 2. 创建 WriteSyncer
	writeSyncer := zapcore.AddSync(logRotate)

	// 3. 配置 Encoder
	encoderConfig := zap.NewProductionEncoderConfig()
	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
	encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder

	// 4. 创建 Core
	core := zapcore.NewCore(
		zapcore.NewJSONEncoder(encoderConfig),
		writeSyncer,
		zap.InfoLevel,
	)

	// 5. 创建 Logger
	logger := zap.New(core, zap.AddCaller())
	defer logger.Sync()

	// 6. 使用 Logger
	for i := 0; i < 100; i++ {
		logger.Info("Processing request",
			zap.Int("request_id", i),
			zap.String("method", "GET"),
			zap.String("path", "/api/users"),
		)
	}

	logger.Info("Log rotation configured successfully")
}
