package main

import (
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	// 1. 自定义配置
	config := zap.Config{
		Level:            zap.NewAtomicLevelAt(zap.InfoLevel), // 日志级别
		Development:      false,                               // 生产模式
		Encoding:         "json",                              // 输出格式：json/console
		OutputPaths:      []string{"stdout", "app.log"},       // 输出到控制台和文件
		ErrorOutputPaths: []string{"stderr"},
		EncoderConfig: zapcore.EncoderConfig{
			TimeKey:        "time",
			LevelKey:       "level",
			NameKey:        "logger",
			CallerKey:      "caller",
			MessageKey:     "msg",
			StacktraceKey:  "stacktrace",
			LineEnding:     zapcore.DefaultLineEnding,
			EncodeLevel:    zapcore.LowercaseLevelEncoder, // 小写级别名
			EncodeTime:     zapcore.ISO8601TimeEncoder,    // ISO8601 时间格式
			EncodeDuration: zapcore.SecondsDurationEncoder,
			EncodeCaller:   zapcore.ShortCallerEncoder, // 短路径
		},
	}

	// 2. 创建 Logger
	logger, _ := config.Build()
	defer logger.Sync()

	// 3. 使用 Logger
	logger.Info("Custom logger initialized",
		zap.String("env", "production"),
		zap.Int("workers", 10),
	)

	logger.Warn("Low disk space",
		zap.Int64("available_mb", 500),
		zap.Int64("threshold_mb", 1000),
	)

	logger.Error("Database connection failed",
		zap.String("host", "localhost"),
		zap.Int("port", 3306),
		zap.Error(nil),
	)
}
