Example #1
0
func main() {
	flag.Parse()
	log := logrus.New()
	conf := et.ParseConfig(*configFile)
	avro := et.NewAvroInst(log, conf.Avro)

	record, err := avro.NewRecord()
	if err != nil {
		log.Fatalln("New record err:", err)
	}
	record.Set("id", "auction_id")
	record.Set("event", "anwo_postback")
	record.Set("timestamp", "time")
	buf := new(bytes.Buffer)
	if err = avro.Encode(buf, record); err != nil {
		log.Fatalln("Encode err:", err)
	}
	decode, err := avro.Decode(buf)
	if err != nil {
		log.Fatalln("Err decode:", err)
	}
	log.Println(decode)
}
Example #2
0
func init() {
	// Parse flags
	flag.Parse()
	// open config
	conf = et.ParseConfig(*configFile)
	// Init log system
	log = logrus.New()
	log.Formatter = new(logrus.TextFormatter)
	// Init log to file
	switch conf.Main.Log_file_formatter {
	case "json":
		log.Hooks.Add(lfshook.NewHook(lfshook.PathMap{
			logrus.InfoLevel:  conf.Main.Log_file,
			logrus.ErrorLevel: conf.Main.Log_file,
			logrus.DebugLevel: conf.Main.Log_file,
			logrus.PanicLevel: conf.Main.Log_file,
			logrus.FatalLevel: conf.Main.Log_file,
			logrus.WarnLevel:  conf.Main.Log_file,
		}, new(logrus.JSONFormatter)))
	case "text":
		formatter := new(logrus.TextFormatter) // default
		formatter.ForceUnColored = true
		log.Hooks.Add(lfshook.NewHook(lfshook.PathMap{
			logrus.InfoLevel:  conf.Main.Log_file,
			logrus.ErrorLevel: conf.Main.Log_file,
			logrus.DebugLevel: conf.Main.Log_file,
			logrus.PanicLevel: conf.Main.Log_file,
			logrus.FatalLevel: conf.Main.Log_file,
			logrus.WarnLevel:  conf.Main.Log_file,
		}, formatter))
	default:
		log.WithFields(logrus.Fields{
			"module": "front",
		}).Fatalln("Unrecognized log file formatter:", conf.Main.Log_file_formatter)
	}
	switch conf.Main.Log_level {
	case "debug":
		log.Level = logrus.DebugLevel
	case "info":
		log.Level = logrus.InfoLevel
	case "warn":
		log.Level = logrus.WarnLevel
	case "error":
		log.Level = logrus.ErrorLevel
	case "fatal":
		log.Level = logrus.FatalLevel
	case "panic":
		log.Level = logrus.PanicLevel
	default:
		log.WithFields(logrus.Fields{
			"module": "front",
		}).Fatalln("Unrecognized log level:", conf.Main.Log_level)
	}
	if !conf.Front.Enabled {
		log.WithFields(logrus.Fields{
			"module": "front",
		}).Fatalln("Front service is not enabled.")
	}
	log.WithFields(logrus.Fields{
		"module": "front",
	}).Infoln("Front service initialization complete.")
}
Example #3
0
package main

import (
	"github.com/lixin9311/lfshook"
	"github.com/lixin9311/logrus"
	"time"
)

var log = logrus.New()

func init() {
	log.Formatter = new(logrus.JSONFormatter)
	tmp := new(logrus.TextFormatter) // default
	tmp.ForceUnColored = false
	tmp.DisableTimestamp = true
	log.Formatter = tmp
	log.Level = logrus.DebugLevel
	tmp2 := new(logrus.TextFormatter) // default
	tmp2.ForceUnColored = true
	tmp2.DisableTimestamp = true
	log.Hooks.Add(lfshook.NewHook(lfshook.PathMap{
		logrus.InfoLevel:  "log.log",
		logrus.ErrorLevel: "log.log",
		logrus.DebugLevel: "log.log",
		logrus.PanicLevel: "log.log",
		logrus.FatalLevel: "log.log",
		logrus.WarnLevel:  "log.log",
	}, new(logrus.JSONFormatter)))
}

func main() {
Example #4
0
func init() {
	// Parse flags
	flag.Parse()
	// open config
	conf = et.ParseConfig(*configFile)
	// Init log system
	log = logrus.New()
	log.Formatter = new(logrus.TextFormatter)
	// Init log to file
	switch conf.Main.Log_file_formatter {
	case "json":
		log.Hooks.Add(lfshook.NewHook(lfshook.PathMap{
			logrus.InfoLevel:  conf.Main.Log_file,
			logrus.ErrorLevel: conf.Main.Log_file,
			logrus.DebugLevel: conf.Main.Log_file,
			logrus.PanicLevel: conf.Main.Log_file,
			logrus.FatalLevel: conf.Main.Log_file,
			logrus.WarnLevel:  conf.Main.Log_file,
		}, new(logrus.JSONFormatter)))
	case "text":
		formatter := new(logrus.TextFormatter) // default
		formatter.ForceUnColored = true
		log.Hooks.Add(lfshook.NewHook(lfshook.PathMap{
			logrus.InfoLevel:  conf.Main.Log_file,
			logrus.ErrorLevel: conf.Main.Log_file,
			logrus.DebugLevel: conf.Main.Log_file,
			logrus.PanicLevel: conf.Main.Log_file,
			logrus.FatalLevel: conf.Main.Log_file,
			logrus.WarnLevel:  conf.Main.Log_file,
		}, formatter))
	default:
		log.Fatalln("Unrecognized log file formatter:", conf.Main.Log_file_formatter)
	}
	switch conf.Main.Log_level {
	case "debug":
		log.Level = logrus.DebugLevel
	case "info":
		log.Level = logrus.InfoLevel
	case "warn":
		log.Level = logrus.WarnLevel
	case "error":
		log.Level = logrus.ErrorLevel
	case "fatal":
		log.Level = logrus.FatalLevel
	case "panic":
		log.Level = logrus.PanicLevel
	default:
		log.Fatalln("Unrecognized log level:", conf.Main.Log_level)
	}
	// setup backup fi.e
	safe_file, err := os.OpenFile(conf.Main.Backup_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		log.WithFields(logrus.Fields{
			"module": "main",
		}).Fatalln("Failed to open backup file:", err)
	}
	fail_safe := golog.New(safe_file, "", golog.LstdFlags)
	// init avro
	avro := et.NewAvroInst(log, conf.Avro)
	// init kafka
	kafka = et.NewKafkaInst(log, conf.Kafka)
	defaultHandler = et.NewDefaultHandler(log, fail_safe, kafka, avro)
	log.WithFields(logrus.Fields{
		"module": "main",
	}).Infoln("Initialization done.")
}