package core

import (
	"fmt"
	"os"
	"tgk-touch/internal/core/internal"
	"tgk-touch/internal/global"
	"tgk-touch/utility"

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

// Zap 获取 zap.Logger
// Author [SliverHorn](https://github.com/SliverHorn)
func Zap() (logger *zap.Logger) {
	z := g.GVA_Config.Zap
	if ok, _ := utility.PathExists(z.Director); !ok { // 判断是否有Director文件夹
		fmt.Printf("create %v directory\n", z.Director)
		_ = os.Mkdir(z.Director, os.ModePerm)
	}
	levels := z.Levels()
	length := len(levels)
	cores := make([]zapcore.Core, 0, length)
	for i := 0; i < length; i++ {
		core := internal.NewZapCore(levels[i])

		cores = append(cores, core)
	}
	logger = zap.New(
		zapcore.NewTee(cores...),
		zap.AddStacktrace(zapcore.ErrorLevel))

	if z.ShowLine {
		logger = logger.WithOptions(zap.AddCaller())
	}
	return logger
}
