func loadLoggerAndConfig(c *cli.Context, component string) (logger.Logger, *gosteno.Logger, *config.Config) { configPath := c.String("config") if configPath == "" { fmt.Printf("Config path required") os.Exit(1) } conf, err := config.FromFile(configPath) if err != nil { fmt.Printf("Failed to load config: %s", err.Error()) os.Exit(1) } stenoConf := &gosteno.Config{ Sinks: []gosteno.Sink{ gosteno.NewIOSink(os.Stdout), gosteno.NewSyslogSink("vcap.hm9000." + component), }, Level: conf.LogLevel(), Codec: gosteno.NewJsonCodec(), } gosteno.Init(stenoConf) steno := gosteno.NewLogger("vcap.hm9000." + component) hmLogger := logger.NewRealLogger(steno) return hmLogger, steno, conf }
func main() { l := logger.NewRealLogger() app := cli.NewApp() app.Name = "HM9000" app.Usage = "Start the various HM9000 components" app.Version = "0.0.9000" app.Commands = []cli.Command{ cli.Command{ Name: "fetch_desired", Description: "Fetches desired state", Usage: "hm fetch_desired --config=/path/to/config", Flags: []cli.Flag{ cli.StringFlag{"config", "", "Path to config file"}, }, Action: func(c *cli.Context) { hm.FetchDesiredState(l, c) }, }, cli.Command{ Name: "listen", Description: "Listens over the NATS for the actual state", Usage: "hm listen --config=/path/to/config", Flags: []cli.Flag{ cli.StringFlag{"config", "", "Path to config file"}, }, Action: func(c *cli.Context) { hm.StartListeningForActual(l, c) }, }, cli.Command{ Name: "dump", Description: "Dumps contents of the data store", Usage: "hm dump --config=/path/to/config", Flags: []cli.Flag{ cli.StringFlag{"config", "", "Path to config file"}, }, Action: func(c *cli.Context) { hm.Dump(l, c) }, }, } app.Run(os.Args) }
func loadLoggerAndConfig(c *cli.Context, component string) (logger.Logger, *gosteno.Logger, *config.Config) { configPath := c.String("config") if configPath == "" { fmt.Printf("Config path required") os.Exit(1) } conf, err := config.FromFile(configPath) if err != nil { fmt.Printf("Failed to load config: %s", err.Error()) os.Exit(1) } sinks := make([]gosteno.Sink, 0, 3) if conf.LogDirectory != "" { logName := fmt.Sprintf("hm9000_%s.log", component) logFile := path.Join(conf.LogDirectory, logName) sinks = append(sinks, gosteno.NewFileSink(logFile)) } else { sinks = append(sinks, gosteno.NewIOSink(os.Stdout)) } sinks = append(sinks, gosteno.NewSyslogSink("vcap.hm9000."+component)) stenoConf := &gosteno.Config{ Sinks: sinks, Level: conf.LogLevel(), Codec: gosteno.NewJsonCodec(), } gosteno.Init(stenoConf) steno := gosteno.NewLogger("vcap.hm9000." + component) hmLogger := logger.NewRealLogger(steno) debugAddr := c.String("debugAddr") if debugAddr != "" { _, err = cf_debug_server.Run(debugAddr, nil) if err != nil { hmLogger.Error("Failed to start debug server", err) os.Exit(1) } } return hmLogger, steno, conf }