Kafka, Console, 파일(파일 구분, 백업 지원)으로 동시에 Uber 로그 zap 출력

2834 단어
두 말 없이 바로 코드에 올리다
 

var Logger *zap.Logger

type LogKafka struct {
	Producer sarama.SyncProducer
	Topic    string
}

func (lk *LogKafka) Write(p []byte) (n int, err error) {
	msg := &sarama.ProducerMessage{}
	msg.Topic = lk.Topic
	msg.Value = sarama.ByteEncoder(p)
	_, _, err = lk.Producer.SendMessage(msg)
	if err != nil {
		return
	}
	return

}
func InitLogger(mode string, fileName string, maxSize, maxBackups, maxAge int, compress bool, enableKafka bool, kafkaAddress []string) {
	//          
	highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
		return lvl >= zapcore.ErrorLevel
	})
	//          
	lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
		return lvl >= zapcore.DebugLevel
	})
	var allCore []zapcore.Core

	hook := lumberjack.Logger{
		Filename:   fileName,
		MaxSize:    maxSize, // megabytes
		MaxBackups: maxBackups,
		MaxAge:     maxAge,   //days
		Compress:   compress, // disabled by default
	}

	fileWriter := zapcore.AddSync(&hook)

	// High-priority output should also go to standard error, and low-priority
	// output should also go to standard out.
	consoleDebugging := zapcore.Lock(os.Stdout)

	// for human operators.
	consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())

	// Join the outputs, encoders, and level-handling functions into
	// zapcore.Cores, then tee the four cores together.
	// kafka
	if len(kafkaAddress) > 0 && enableKafka {
		var (
			kl  LogKafka
			err error
		)
		kl.Topic = "go_framework_log"
		//        Kafka   
		config := sarama.NewConfig()
		//                  
		config.Producer.RequiredAcks = sarama.WaitForAll
		//       
		config.Producer.Partitioner = sarama.NewRandomPartitioner
		//             ,     RequireAcks    NoReponse     .
		config.Producer.Return.Successes = true
		config.Producer.Return.Errors = true

		kl.Producer, err = sarama.NewSyncProducer(kafkaAddress, config)
		if err != nil {
			fmt.Printf("connect kafka failed: %+v
", err) os.Exit(-1) } topicErrors := zapcore.AddSync(&kl) // kafka kafkaEncoder := zapcore.NewJSONEncoder(zap.NewDevelopmentEncoderConfig()) var kafkaCore zapcore.Core if mode == "debug" { kafkaCore = zapcore.NewCore(kafkaEncoder, topicErrors, lowPriority) } else { kafkaCore = zapcore.NewCore(kafkaEncoder, topicErrors, highPriority) } allCore = append(allCore, kafkaCore) } if mode == "debug" { allCore = append(allCore, zapcore.NewCore(consoleEncoder, consoleDebugging, lowPriority)) } allCore = append(allCore, zapcore.NewCore(consoleEncoder, fileWriter, highPriority)) core := zapcore.NewTee(allCore...) // From a zapcore.Core, it's easy to construct a Logger. Logger = zap.New(core).WithOptions(zap.AddCaller()) }

좋은 웹페이지 즐겨찾기