package logger

import (
	"fmt"
	"gitlab.dianchu.cc/Taotie/log"
	"gitlab.dianchu.cc/Taotie/log/field"
	"gitlab.dianchu.cc/Taotie/log/level"
	"gitlab.dianchu.cc/Taotie/log/writer"
	"os"
)

var LOG_STDOUT bool

func NewLogger(appName string) (*log.Logger, error) {

	lv := level.DebugLevel

	writeSyncer := make([]writer.WriteSyncer, 0)
	LOG_STDOUT = true
	if LOG_STDOUT {
		writeSyncer = append(writeSyncer, writer.AddSync(os.Stdout))
	}

	cfg := log.NewProductionConfig(writeSyncer...)
	cfg.Name = appName
	cfg.EncoderConfig.TimeKey = "generated_time"
	cfg.Level = level.NewAtomicLevelAt(lv)

	logger, err := log.New(cfg)
	if err != nil {
		return nil, err
	}

	logger = logger.With(field.String("@fluentd_tag", appName))
	logger.Info(fmt.Sprintf("日志初始化完成，日志等级[%s]", cfg.Level))
	return logger, nil
}
