Ejemplo n.º 1
0
Archivo: main.go Proyecto: cyflhn/codis
func setLogLevel(level string) {
	level = strings.ToLower(level)
	var l = log.LEVEL_INFO
	switch level {
	case "error":
		l = log.LEVEL_ERROR
	case "warn", "warning":
		l = log.LEVEL_WARN
	case "debug":
		l = log.LEVEL_DEBUG
	case "info":
		fallthrough
	default:
		level = "info"
		l = log.LEVEL_INFO
	}
	log.SetLevel(l)
	log.SetInfoLogLevel(l)
	log.Infof("set log level to <%s>", level)
}
Ejemplo n.º 2
0
Archivo: main.go Proyecto: cyflhn/codis
func main() {
	fmt.Print(banner)

	args, err := docopt.Parse(usage, nil, true, "codis proxy v0.1", true)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// set config file
	if args["-c"] != nil {
		configFile = args["-c"].(string)
	}

	/*
		var maxFileFrag = 10
		var maxFragSize int64 = bytesize.GB * 1
		if s, ok := args["--log-filesize"].(string); ok && s != "" {
			v, err := bytesize.Parse(s)
			if err != nil {
				log.PanicErrorf(err, "invalid max log file size = %s", s)
			}
			maxFragSize = v
		}
	*/
	// set output log file
	if s, ok := args["-L"].(string); ok && s != "" {
		//f, err := log.NewRollingFile(s, maxFileFrag, maxFragSize)
		f, err := log.GetLogFile(s)
		if err != nil {
			log.PanicErrorf(err, "open rolling log file failed: %s", s)
		} else {
			defer f.Close()
			log.StdLog = log.New(f, "")
		}
	}
	log.SetLevel(log.LEVEL_INFO)
	log.SetFlags(log.Flags() | log.Lshortfile)

	// set info log file

	if s, ok := args["--infoLog"].(string); ok && s != "" {
		//f, err := log.NewRollingFile(s, maxFileFrag, maxFragSize)
		f, err := log.GetLogFile(s)
		if err != nil {
			log.PanicErrorf(err, "open info log file failed: %s", s)
		} else {
			defer f.Close()
			log.InfoLog = log.New(f, "")
		}
		log.SetInfoLogLevel(log.LEVEL_INFO)
		log.SetInfoLogFlags(log.Flags() | log.Lshortfile)
	}

	// set log level
	if s, ok := args["--log-level"].(string); ok && s != "" {
		setLogLevel(s)
	}
	cpus = runtime.NumCPU()
	// set cpu
	if args["--cpu"] != nil {
		cpus, err = strconv.Atoi(args["--cpu"].(string))
		if err != nil {
			log.PanicErrorf(err, "parse cpu number failed")
		}
	}

	// set addr
	if args["--addr"] != nil {
		addr = args["--addr"].(string)
	}

	// set http addr
	if args["--http-addr"] != nil {
		httpAddr = args["--http-addr"].(string)
	}

	checkUlimit(1024)
	runtime.GOMAXPROCS(cpus)

	http.HandleFunc("/setloglevel", handleSetLogLevel)
	go http.ListenAndServe(httpAddr, nil)

	log.Info("running on ", addr)
	conf, err := proxy.LoadConf(configFile)
	if err != nil {
		log.PanicErrorf(err, "load config failed")
	}

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, os.Kill)

	s := proxy.New(addr, httpAddr, conf)
	defer s.Close()

	stats.PublishJSONFunc("router", func() string {
		var m = make(map[string]interface{})
		m["ops"] = router.OpCounts()
		m["cmds"] = router.GetAllOpStats()
		m["info"] = s.Info()
		m["build"] = map[string]interface{}{
			"version": utils.Version,
			"compile": utils.Compile,
		}
		m["slowop"] = router.GetSlowOps()
		b, _ := json.Marshal(m)
		return string(b)
	})

	go func() {
		<-c
		log.Info("ctrl-c or SIGTERM found, bye bye...")
		s.Close()
	}()

	time.Sleep(time.Second)
	if err := s.SetMyselfOnline(); err != nil {
		log.WarnError(err, "mark myself online fail, you need mark online manually by dashboard")
	}
	s.Join()
	log.Infof("proxy exit!! :(")
}