package log

import (
	"center-api/internal/conf"
	"center-api/internal/pkg/helper"
	"context"
	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/tracing"
	"os"
	"strings"
	"sync"
)

var businessLogger log.Logger
var businessLoggerOnce sync.Once

// NewBusinessLogger 获取业务日志logger
func NewBusinessLogger(c *conf.Bootstrap, id, name, version string) log.Logger {
	//默认的业务 logger
	businessLoggerOnce.Do(func() {
		filePath := ""
		if c.Logs != nil {
			filePath = strings.Trim(c.Logs.Business, " ")
		}
		writeIO := os.Stdout
		if filePath != "" {
			f, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
			if err != nil {
				panic("打开日志失败，注意读写权限：" + filePath)
			}
			writeIO = f
		}

		businessLogger = log.With(log.NewStdLogger(writeIO),
			"ts", log.DefaultTimestamp,
			"caller", log.DefaultCaller,
			"service.id", id,
			"service.name", name,
			"service.version", version,
			"trace.id", tracing.TraceID(),
			"span.id", tracing.SpanID(),
			"user.id", getLogTraceUserId(),
		)
	})

	return businessLogger
}

// GetLogTraceUserId 获取日志中跟踪的用户 id
func getLogTraceUserId() log.Valuer {
	return func(ctx context.Context) interface{} {
		return helper.GetLoginUserId(ctx)
	}
}
