package internal

import (
	"fmt"
	"gorm.io/gorm/logger"
	"sundynix-go/config"
	"sundynix-go/global"
)

type Writer struct {
	config config.GeneralDB
	writer logger.Writer
}

// NewWriter 创建一个Writer
func NewWriter(config config.GeneralDB) *Writer {
	return &Writer{config: config}
}

// Printf 格式化打印日志
func (w *Writer) Printf(message string, data ...any) {
	fmt.Printf(message, data)
	//当开启了zap的情况下，会打印到日志记录中
	if w.config.LogZap {
		switch w.config.LogLevel() {
		case logger.Silent:
			global.Logger.Debug(fmt.Sprintf(message, data))
		case logger.Error:
			global.Logger.Error(fmt.Sprintf(message, data))
		case logger.Warn:
			global.Logger.Warn(fmt.Sprintf(message, data))
		case logger.Info:
			global.Logger.Info(fmt.Sprintf(message, data))
		default:
			global.Logger.Info(fmt.Sprintf(message, data))
		}
		return
	}
}
