func main() { ctx.LoadFromHome() log4go.AddFilter("stdout", log4go.INFO, log4go.NewConsoleLogWriter()) app := os.Args[0] args := os.Args[1:] for _, arg := range args { if arg == "-v" || arg == "--version" { newArgs := make([]string, len(args)+1) newArgs[0] = "version" copy(newArgs[1:], args) args = newArgs break } } c := cli.NewCLI(app, gafka.Version+"-"+gafka.BuildId) c.Args = os.Args[1:] c.Commands = commands c.HelpFunc = func(m map[string]cli.CommandFactory) string { var buf bytes.Buffer buf.WriteString(fmt.Sprintf("A MySQL syncer tool\n\n")) buf.WriteString(cli.BasicHelpFunc(app)(m)) return buf.String() } exitCode, err := c.Run() if err != nil { fmt.Fprintf(os.Stderr, "%+v\n", err) os.Exit(1) } else if c.IsVersion() { os.Exit(0) } os.Exit(exitCode) }
func setupLogging() { log.SetOutput(ioutil.Discard) level := log4go.DEBUG switch ctx.LogLevel() { case "info": level = log4go.INFO case "warn": level = log4go.WARNING case "error": level = log4go.ERROR case "debug": level = log4go.DEBUG case "trace": level = log4go.TRACE case "alarm": level = log4go.ALARM } for _, filter := range log4go.Global { filter.Level = level } log4go.AddFilter("stdout", level, log4go.NewConsoleLogWriter()) }
func setupLogging(loggingLevel, logFile string) { level := log.DEBUG switch loggingLevel { case "info": level = log.INFO case "warn": level = log.WARNING case "error": level = log.ERROR } for _, filter := range log.Global { filter.Level = level } if logFile == "stdout" { log.AddFilter("stdout", level, log.NewConsoleLogWriter()) } else { logDir := filepath.Dir(logFile) if err := os.MkdirAll(logDir, 0744); err != nil { panic(err) } writer := log.NewFileLogWriter(logFile, false) log.AddFilter("file", level, writer) writer.SetFormat("[%d %T] [%L] (%S) %M") writer.SetRotate(true) writer.SetRotateSize(0) writer.SetRotateLines(0) writer.SetRotateDaily(true) } // thrift lib use "log", so we also need to customize its behavior _log.SetFlags(_log.Ldate | _log.Ltime | _log.Lshortfile) }
func init() { flag.StringVar(&host, "host", "localhost", "host name of faed") flag.StringVar(&port, "port", "9001", "fae port") flag.IntVar(&interval, "interval", 5, "sleep between conn") flag.Parse() log.AddFilter("stdout", log.INFO, log.NewConsoleLogWriter()) }
func init() { flag.StringVar(&host, "host", "", "host name of faed") flag.StringVar(&port, "port", "", "fae port") flag.IntVar(&loops, "loops", 1, "ping how many times") flag.StringVar(&zk, "zk", "localhost:2181", "zookeeper server addr") flag.Parse() log.AddFilter("stdout", log.INFO, log.NewConsoleLogWriter()) }
func (this *Start) setupLogging(logFile, logLevel, crashLogFile string) { level := log.DEBUG switch logLevel { case "info": level = log.INFO case "warn": level = log.WARNING case "error": level = log.ERROR case "debug": level = log.DEBUG case "trace": level = log.TRACE case "alarm": level = log.ALARM } for _, filter := range log.Global { filter.Level = level } log.LogBufferLength = 32 // default 32, chan cap if logFile == "stdout" { log.AddFilter("stdout", level, log.NewConsoleLogWriter()) } else { log.DeleteFilter("stdout") filer := log.NewFileLogWriter(logFile, true, true, 0) filer.SetFormat("[%d %T] [%L] (%S) %M") filer.SetRotateSize(0) filer.SetRotateLines(0) filer.SetRotateDaily(true) log.AddFilter("file", level, filer) } if crashLogFile != "" { f, err := os.OpenFile(crashLogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) if err != nil { panic(err) } syscall.Dup2(int(f.Fd()), int(os.Stdout.Fd())) syscall.Dup2(int(f.Fd()), int(os.Stderr.Fd())) fmt.Fprintf(os.Stderr, "\n%s %s (build: %s)\n===================\n", time.Now().String(), gafka.Version, gafka.BuildId) } }
func (this *Monitor) Init() { var logFile, zone string flag.StringVar(&logFile, "log", "stdout", "log filename") flag.StringVar(&zone, "z", "", "zone, required") flag.StringVar(&this.apiAddr, "http", ":10025", "api http server addr") flag.StringVar(&this.influxdbAddr, "influxAddr", "", "influxdb addr, required") flag.StringVar(&this.influxdbDbName, "db", "", "influxdb db name, required") flag.StringVar(&this.externalDir, "confd", "", "external script config dir") flag.Parse() if zone == "" || this.influxdbDbName == "" || this.influxdbAddr == "" { panic("zone or influxdb empty, run help ") } ctx.LoadFromHome() this.zkzone = zk.NewZkZone(zk.DefaultConfig(zone, ctx.ZoneZkAddrs(zone))) this.watchers = make([]Watcher, 0, 10) this.quit = make(chan struct{}) // export RESTful api this.setupRoutes() if logFile == "stdout" { log.AddFilter("stdout", log.TRACE, log.NewConsoleLogWriter()) } else { log.DeleteFilter("stdout") filer := log.NewFileLogWriter(logFile, true, false, 0644) filer.SetRotateDaily(true) filer.SetFormat("[%d %T] [%L] (%S) %M") log.AddFilter("file", log.TRACE, filer) } rc, err := influxdb.NewConfig(this.influxdbAddr, this.influxdbDbName, "", "", time.Minute) if err != nil { panic(err) } telemetry.Default = influxdb.New(metrics.DefaultRegistry, rc) }
func setupLogging(logFile, level, crashLogFile string) { logLevel := toLogLevel(level) for _, filter := range log.Global { filter.Level = logLevel } log.LogBufferLength = 32 // default 32, chan cap if logFile == "stdout" { log.AddFilter("stdout", logLevel, log.NewConsoleLogWriter()) } else { log.DeleteFilter("stdout") filer := log.NewFileLogWriter(logFile, true, false, 0) filer.SetFormat("[%d %T] [%L] (%S) %M") filer.SetRotateSize(0) filer.SetRotateLines(0) filer.SetRotateDaily(true) log.AddFilter("file", logLevel, filer) } if crashLogFile != "" { f, err := os.OpenFile(crashLogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) if err != nil { panic(err) } syscall.Dup2(int(f.Fd()), int(os.Stdout.Fd())) syscall.Dup2(int(f.Fd()), int(os.Stderr.Fd())) fmt.Fprintf(os.Stderr, "\n%s %s (build: %s)\n===================\n", time.Now().String(), gafka.Version, gafka.BuildId) } }
func init() { gafka.BuildId = "test" log.AddFilter("stdout", log.ERROR, log.NewConsoleLogWriter()) }
func init() { ctx.LoadFromHome() log.AddFilter("stdout", log.INFO, log.NewConsoleLogWriter()) }
func SetupLogging(logFile, logLevel, crashLogFile, alarmSockPath, alarmTag string) { level := log.DEBUG switch logLevel { case "info": level = log.INFO case "warn": level = log.WARNING case "error": level = log.ERROR case "debug": level = log.DEBUG case "trace": level = log.TRACE case "alarm": level = log.ALARM } for _, filter := range log.Global { filter.Level = level } // TODO log.LogBufferLength = 2 << 10 // default 32, chan cap if logFile == "stdout" { log.AddFilter("stdout", level, log.NewConsoleLogWriter()) } else { log.DeleteFilter("stdout") filer := log.NewFileLogWriter(logFile, false) filer.SetFormat("[%d %T] [%L] (%S) %M") filer.SetRotate(true) filer.SetRotateSize(0) filer.SetRotateLines(0) filer.SetRotateDaily(true) log.AddFilter("file", level, filer) if alarmTag != "" && alarmSockPath != "" { if alarmer, err := log.NewSyslogNgWriter(alarmSockPath, alarmTag); err != nil { log.Error("syslogng writer: %s", err.Error()) } else { log.AddFilter("alarm", log.ALARM, alarmer) } } } if crashLogFile != "" { f, err := os.OpenFile(crashLogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) if err != nil { panic(err) } syscall.Dup2(int(f.Fd()), 2) fmt.Fprintf(os.Stderr, "\n%s %s (build: %s)\n===================\n", time.Now().String(), Version, BuildId) } }
func main() { ctx.LoadFromHome() log.SetOutput(ioutil.Discard) log4go.AddFilter("stdout", log4go.INFO, log4go.NewConsoleLogWriter()) app := os.Args[0] args := os.Args[1:] for _, arg := range args { if arg == "-v" || arg == "--version" { newArgs := make([]string, len(args)+1) newArgs[0] = "version" copy(newArgs[1:], args) args = newArgs break } if arg == "--generate-bash-completion" { if len(args) > 1 { // contextual auto complete lastArg := args[len(args)-2] switch lastArg { case "-z": // zone for _, zone := range ctx.SortedZones() { fmt.Println(zone) } return } } for name, _ := range commands { fmt.Println(name) } return } } c := cli.NewCLI(app, gafka.Version+"-"+gafka.BuildId) c.Args = os.Args[1:] if len(os.Args) > 1 { // command given, convert alias if alias, present := ctx.Alias(os.Args[1]); present { var cargs []string cargs = append(cargs, strings.Split(alias, " ")...) if len(os.Args) > 2 { cargs = append(cargs, os.Args[2:]...) } c.Args = cargs } } c.Commands = commands c.HelpFunc = func(m map[string]cli.CommandFactory) string { var buf bytes.Buffer buf.WriteString(fmt.Sprintf("A CLI tool for Zookeeper\n\n")) buf.WriteString(cli.BasicHelpFunc(app)(m)) return buf.String() } exitCode, err := c.Run() if err != nil { fmt.Fprintf(os.Stderr, "%+v\n", err) os.Exit(1) } else if c.IsVersion() { os.Exit(0) } os.Exit(exitCode) }