func getConfig() (*types.Config, error) {
	app.Validate(validateParams)
	kingpin.HelpFlag.Short('h')
	app.Flag("version", "Print application version and exit").PreAction(printVersion).Short('v').Bool()
	kingpin.MustParse(app.Parse(os.Args[1:]))

	c := &types.Config{
		Consul:         *consul,
		Marathon:       *marathon,
		ResyncInterval: *resyncInterval,
		DryRun:         *enableDryRun,
	}

	// Setup the logging.
	level, err := log.ParseLevel(*logLevel)
	if err != nil {
		return nil, err
	}

	log.SetFormatter(&prefixed.TextFormatter{
		ForceColors: *forceColors,
	})
	log.SetLevel(level)

	if *enableSyslog {
		hook, err := logrusSyslog.NewSyslogHook("", "", syslog.LOG_DEBUG, app.Name)
		if err != nil {
			return nil, err
		}

		log.AddHook(hook)
	}

	return c, nil
}
Example #2
0
func New(config *conf.Log) (Logger, error) {
	logger := logrus.New()
	if strings.ToLower(config.Local) == "stderr" {
		logger.Out = os.Stderr
	} else if strings.ToLower(config.Local) == "stdout" {
		logger.Out = os.Stdout
	} else {
		file, err := os.OpenFile(config.Local, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
		if err != nil {
			return nil, err
		}
		logger.Out = file
	}
	logger.Level = levels[config.Level]

	if config.Syslog.Protocol != "" {
		level := syslogLevels[config.Syslog.Level]
		hook, err := logrus_syslog.NewSyslogHook(config.Syslog.Protocol, config.Syslog.Host, level, config.Syslog.Tag)
		if err != nil {
			return nil, err
		}
		logger.Hooks.Add(hook)
	}
	return logger, nil
}
Example #3
0
func init() {
	log.Formatter = new(logrus.TextFormatter) // default
	hook, err := logrus_syslog.NewSyslogHook("udp", "127.0.0.1:514", syslog.LOG_INFO, "")

	if err != nil {
		log.Hooks.Add(hook)
	}
}
Example #4
0
func setupSyslog(logger *logrus.Logger) error {
	hook, err := logrus_syslog.NewSyslogHook("", "", getSyslogPriority(logger.Level), "taskcluster-worker")
	if err != nil {
		return err
	}
	logger.Hooks.Add(hook)
	return nil
}
Example #5
0
func setupSyslogLogging(syslogAddr string, syslogProgramName string) {
	log.Printf("Logging to syslog addr=[%s], program_name=[%s]", syslogAddr, syslogProgramName)
	logrusSysLogHook, err := logrus_syslog.NewSyslogHook("udp", syslogAddr, syslog.LOG_INFO, syslogProgramName)
	if err != nil {
		log.Fatalf("Failed to add syslog logrus hook - %s", err)
	}
	log.AddHook(logrusSysLogHook)
}
Example #6
0
func initLogger() {
	once.Do(func() {
		var (
			level syslog.Priority
			hook  *logrus_syslog.SyslogHook
		)

		proto, err := config.Instance().String("syslog", "proto")
		cli.CheckFatalError(err)

		addr, err := config.Instance().String("syslog", "addr")
		cli.CheckFatalError(err)

		levelName, err := config.Instance().String("syslog", "level")
		cli.CheckFatalError(err)

		switch levelName {
		case "debug":
			level = syslog.LOG_DEBUG
		case "info":
			level = syslog.LOG_INFO
		case "notice":
			level = syslog.LOG_NOTICE
		case "warning":
			level = syslog.LOG_WARNING
		case "err":
			level = syslog.LOG_ERR
		case "crit":
			level = syslog.LOG_CRIT
		case "alert":
			level = syslog.LOG_ALERT
		case "emerg":
			level = syslog.LOG_EMERG
		default:
			level = syslog.LOG_WARNING
		}

		if len(proto) == 0 || len(addr) == 0 {
			writer, err := syslog.New(level, "recause")
			if err != nil {
				cli.CheckFatalError(err)
			}

			hook = &logrus_syslog.SyslogHook{
				Writer: writer,
			}
		} else {
			hook, err = logrus_syslog.NewSyslogHook(proto, addr, level, "recause")
			cli.CheckFatalError(err)
		}

		logger = log.New()
		logger.Hooks.Add(hook)

		log.SetOutput(logger.Writer())
	})
}
Example #7
0
// Function Home - that handles a web server request for a page
func Home(w http.ResponseWriter, req *http.Request) {
	io.WriteString(w, "Hello World!")
	log := logrus.New()
	hook, err := logrus_syslog.NewSyslogHook("udp", os.Getenv("SYSLOG_SERVER")+":"+os.Getenv("SYSLOG_PORT"), syslog.LOG_INFO, "")
	if err == nil {
		log.Hooks.Add(hook)
	}
	log.Info(req, "\n")
}
Example #8
0
func setupSyslog() {
	hook, err := logrus_syslog.NewSyslogHook("", "", syslog.LOG_INFO, "pmb")

	if err == nil {
		logrus.SetFormatter(&SyslogFormatter{})
		// discard all output
		logrus.SetOutput(ioutil.Discard)
		logrus.AddHook(hook)
	}
}
Example #9
0
func init() {
	conf := config.GetConfig()
	if conf == nil {
		conf = &config.Config{}
	}
	// Output to stderr instead of stdout, could also be a file.
	var output io.Writer
	switch conf.Log.Output.Path {
	case "stderr":
		output = os.Stderr
	case "stdout":
		output = os.Stdout
	default:
		logrus.Warn("No log output defined, using stderr")
		output = os.Stderr
	}
	log.Out = output

	// Only log the warning severity or above.
	level, err := logrus.ParseLevel(conf.Log.Output.Level)
	if err != nil {
		logrus.Warn("Cannot parse conf level, default to INFO")
	} else {
		log.Level = level
	}
	for _, hook := range conf.Log.Hook {
		switch hook.Type {
		case "syslog":
			priority := syslog.LOG_INFO
			if err != nil {
				logrus.Errorf("%v is not a valid priority, defaulting to Info", hook.Level)
			}
			h, err := logrus_syslog.NewSyslogHook(hook.Protocol, hook.URL, priority, "")
			if err != nil {
				logrus.Errorf("Unable to connect to syslog daemon %v:%v", hook.Protocol, hook.URL)
			} else {
				log.Hooks.Add(h)
			}
		case "logstash":
			level, err := logrus.ParseLevel(hook.Level)
			if err != nil {
				logrus.Warn("Cannot parse conf level, default to INFO")
			} else {
				log.Level = level
			}
			h, err := logstash_hook.NewLogstashHook(hook.Protocol, hook.URL, "application", level)
			if err != nil {
				logrus.Errorf("Unable to connect to logstash at %v:%v", hook.Protocol, hook.URL)

			} else {
				log.Hooks.Add(h)
			}
		}
	}
}
Example #10
0
func NewSyslogHook(options logrus_mate.Options) (hook logrus.Hook, err error) {
	conf := SyslogHookConfig{}
	if err = options.ToObject(&conf); err != nil {
		return
	}

	return logrus_syslog.NewSyslogHook(
		conf.Network,
		conf.Address,
		toPriority(conf.Priority),
		conf.Tag)
}
Example #11
0
// setLog configure the log
func (ld *LogData) setLog() error {
	logLevel, syslogPriority, err := ld.parseLogLevel()
	if err != nil {
		return fmt.Errorf("The logLevel must be one of the following: EMERGENCY, ALERT, CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG")
	}
	log.SetLevel(logLevel)
	hook, err := logrus_syslog.NewSyslogHook(ld.Network, ld.Address, syslogPriority, "")
	if err == nil {
		log.AddHook(hook)
	}
	return nil
}
Example #12
0
func (app *Application) Configure() error {
	var err error

	// Set config
	if err = app.ReadConfigFile(app.Options.Config); err != nil {
		return err
	}

	// Read config
	if err = app.BindConfig(&app.Config); err != nil {
		return err
	}

	// Set log level
	var level = log.InfoLevel
	if app.Options.Debug {
		level = log.DebugLevel
	} else if app.Config.Log.Level != "" {
		level, err = log.ParseLevel(app.Config.Log.Level)
		if err != nil {
			log.Warnf("Failed to parse log level: %v", err)
		}
	}
	log.SetLevel(level)

	// Set log formatter
	var formatter = "text"
	if app.Config.Log.Formatter != "" {
		formatter = app.Config.Log.Formatter
	}
	logFormatter, err := ParseLogFormatter(formatter)
	if err != nil {
		return err
	}
	log.SetFormatter(logFormatter)

	// Add DebugLoggerHook if we are in debug mode
	if log.GetLevel() == log.DebugLevel {
		log.AddHook(new(DebugLoggerHook))
	}

	// Add syslog
	if app.Config.Log.SyslogName != "" {
		syslog_hook, err := logrus_syslog.NewSyslogHook("", "", syslog.LOG_INFO, app.Config.Log.SyslogName)
		if err == nil {
			log.AddHook(syslog_hook)
		} else {
			log.Warnf("Failed to use syslog: %v", err)
		}
	}

	return nil
}
Example #13
0
File: logger.go Project: x5u/goofys
func InitLoggers(logToSyslog bool) {
	if logToSyslog {
		var err error
		syslogHook, err = logrus_syslog.NewSyslogHook("", "", syslog.LOG_DEBUG, "")
		if err != nil {
			panic("Unable to connect to local syslog daemon")
		}

		for _, l := range loggers {
			l.Hooks.Add(syslogHook)
		}
	}
}
Example #14
0
func initLogging() {
	// level
	level, err := logrus.ParseLevel(viper.GetString("log-level"))
	if err != nil {
		logrus.WithError(err).Warn(`invalid log level. Defaulting to "info"`)
		level = logrus.InfoLevel
	}
	logrus.SetLevel(level)

	// format
	switch viper.GetString("log-format") {
	case "text":
		logrus.SetFormatter(new(logrus.TextFormatter))
	case "json":
		logrus.SetFormatter(new(logrus.JSONFormatter))
	default:
		logrus.SetFormatter(new(logrus.TextFormatter))
		logrus.WithField("format", viper.GetString("log-format")).Warn(`invalid log format. Defaulting to "text"`)
	}

	// output
	dest, err := url.Parse(viper.GetString("log-destination"))
	if err != nil {
		logrus.WithError(err).WithField("destination", viper.GetString("log-destination")).Error(`invalid log destination. Defaulting to "stdout:"`)
		dest.Scheme = "stdout"
	}

	switch dest.Scheme {
	case "stdout":
		// default, we don't need to do anything
	case "file":
		logrus.AddHook(lfshook.NewHook(lfshook.PathMap{
			logrus.DebugLevel: dest.Opaque,
			logrus.InfoLevel:  dest.Opaque,
			logrus.WarnLevel:  dest.Opaque,
			logrus.ErrorLevel: dest.Opaque,
			logrus.FatalLevel: dest.Opaque,
		}))
	case "journald":
		journalhook.Enable()
	case "syslog":
		hook, err := logrus_syslog.NewSyslogHook(dest.Fragment, dest.Host, syslog.LOG_DEBUG, dest.User.String())
		if err != nil {
			logrus.WithError(err).Error("could not configure syslog hook")
		} else {
			logrus.AddHook(hook)
		}
	default:
		logrus.WithField("destination", viper.GetString("log-destination")).Warn(`invalid log destination. Defaulting to "stdout:"`)
	}
}
Example #15
0
// init inits the auditor logger
func (b *basicAuditor) init() error {

	if b.settings == nil {
		return fmt.Errorf("Settings is not defeined")
	}

	if b.logger != nil {
		return nil
	}

	b.logger = logrus.New()
	b.logger.Formatter = &logrus.JSONFormatter{}

	switch b.settings.LogHook {
	case AuditHookSyslog:
		{
			hook, err := logrus_syslog.NewSyslogHook("", "", syslog.LOG_ERR, "authz")
			if err != nil {
				return err
			}
			b.logger.Hooks.Add(hook)
		}
	case AuditHookFile:
		{
			logPath := b.settings.LogPath
			if logPath == "" {
				logrus.Infof("Using default log file path '%s'", logPath)
				logPath = defaultAuditLogPath
			}

			os.MkdirAll(path.Dir(logPath), 0700)
			f, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0750)
			if err != nil {
				return err
			}
			b.logger.Out = f
		}
	case AuditHookStdout:
		{
			// Default - stdout
		}
	default:
		return fmt.Errorf("Wrong log hook value '%s'", b.settings.LogHook)
	}

	return nil
}
Example #16
0
func pickHook(out logoutput) log.Hook {
	switch out {
	case sysLog:
		hook, err := logrus_syslog.NewSyslogHook("", "", syslog.LOG_DAEMON, "awslogs")
		if err != nil {
			log.Fatal("Unable to connect to local syslog daemon")
		}
		return hook
	case stdOut:
		return &writerHook{out: os.Stdout}
	case stdErr:
		return &writerHook{out: os.Stderr}
	case null:
		return &writerHook{out: ioutil.Discard}
	default:
		return &writerHook{out: os.Stderr}
	}
}
Example #17
0
func main() {
	flag.Parse()
	if *printVersion {
		fmt.Printf("%s\n", version)
		os.Exit(0)
	}
	d := new(daemon.Daemon)
	if *debug {
		log.SetLevel(log.DebugLevel)
	}
	if *logToSyslog {
		hook, err := logrus_syslog.NewSyslogHook("", "", syslog.LOG_INFO, "")
		if err == nil {
			log.AddHook(hook)
		}
	}
	d.Debug = *debug
	d.ConfigFile = *f
	os.Exit(d.Run(*oneshot, *noop))
}
Example #18
0
// Add the syslog hook to the logger. This is better than adding it directly,
// for the reasons described in the loggingHookType comments.
func (l *Logger) AddSyslogHook() error {
	hook := loggingHookType{}
	hook.data = &loggingHookData{}
	hook.data.syslogLogger = logrus.New()
	hook.data.syslogLogger.Formatter = &logrus.TextFormatter{
		DisableColors:    true,
		DisableTimestamp: true,
	}

	var err error
	hook.data.syslogHook, err = logrus_syslog.NewSyslogHook("", "",
		syslog.LOG_DEBUG, "mender")
	if err != nil {
		return err
	}

	l.loggingHook = hook
	l.logHooks.Add(hook)

	return nil
}
Example #19
0
func (lcfg *LogConfig) Load() (err error) {
	// Set log level
	var level = log.InfoLevel
	if lcfg.Level != "" {
		level, err = log.ParseLevel(lcfg.Level)
		if err != nil {
			log.Warnf("Failed to parse log level: %v", err)
		}
	}
	log.SetLevel(level)

	// Set log formatter
	var formatter = "text"
	if lcfg.Formatter != "" {
		formatter = lcfg.Formatter
	}
	logFormatter, err := ParseLogFormatter(formatter)
	if err != nil {
		return err
	}
	log.SetFormatter(logFormatter)

	// Add DebugLoggerHook if we are in debug mode
	if log.GetLevel() == log.DebugLevel {
		log.AddHook(new(DebugLoggerHook))
	}

	// Add syslog
	if lcfg.SyslogName != "" {
		syslog_hook, err := logrus_syslog.NewSyslogHook("", "", syslog.LOG_INFO, lcfg.SyslogName)
		if err == nil {
			log.AddHook(syslog_hook)
		} else {
			log.Warnf("Failed to use syslog: %v", err)
		}
	}

	return nil
}
Example #20
0
func (s *Service) Start() error {
	// if .LogFormatter is set, it'll be used in log.SetFormatter() and .Log will be ignored.
	if s.options.LogFormatter != nil {
		log.SetFormatter(s.options.LogFormatter)
	} else {
		switch s.options.Log {
		case "console":
			{
				log.SetFormatter(&log.TextFormatter{})
			}
		case "json":
			{
				log.SetFormatter(&log.JSONFormatter{})
			}
		case "syslog":
			{
				hook, err := logrus_syslog.NewSyslogHook("", "", syslog.LOG_INFO, "")
				if err == nil {
					log.SetFormatter(&log.TextFormatter{DisableColors: true})
					log.AddHook(hook)
				} else {
					setFallbackLogFormatter(s.options)
				}
			}
		case "logstash":
			{
				log.SetFormatter(&logrus_logstash.LogstashFormatter{Type: "logs"})
			}
		default:
			setFallbackLogFormatter(s.options)
		}
	}
	log.SetOutput(os.Stdout)
	log.SetLevel(s.options.LogSeverity.S)

	log.Infof("Service starts with options: %#v", s.options)

	if s.options.PidPath != "" {
		ioutil.WriteFile(s.options.PidPath, []byte(fmt.Sprint(os.Getpid())), 0644)
	}

	if s.options.MetricsClient != nil {
		s.metricsClient = s.options.MetricsClient
	} else if s.options.StatsdAddr != "" {
		var err error
		s.metricsClient, err = metrics.NewWithOptions(s.options.StatsdAddr, s.options.StatsdPrefix, metrics.Options{UseBuffering: true})
		if err != nil {
			return err
		}
	}

	apiFile, muxFiles, err := s.getFiles()
	if err != nil {
		return err
	}

	if err := s.newEngine(); err != nil {
		return err
	}

	s.stapler = stapler.New()
	s.supervisor = supervisor.New(
		s.newProxy, s.ng, s.errorC, supervisor.Options{Files: muxFiles})

	// Tells configurator to perform initial proxy configuration and start watching changes
	if err := s.supervisor.Start(); err != nil {
		return err
	}

	if err := s.initApi(); err != nil {
		return err
	}

	go func() {
		s.errorC <- s.startApi(apiFile)
	}()

	if s.metricsClient != nil {
		go s.reportSystemMetrics()
	}
	signal.Notify(s.sigC, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGUSR2, syscall.SIGCHLD)

	// Block until a signal is received or we got an error
	for {
		select {
		case signal := <-s.sigC:
			switch signal {
			case syscall.SIGTERM, syscall.SIGINT:
				log.Infof("Got signal '%s', shutting down gracefully", signal)
				s.supervisor.Stop(true)
				log.Infof("All servers stopped")
				return nil
			case syscall.SIGKILL:
				log.Infof("Got signal '%s', exiting now without waiting", signal)
				s.supervisor.Stop(false)
				return nil
			case syscall.SIGUSR2:
				log.Infof("Got signal '%s', forking a new self", signal)
				if err := s.startChild(); err != nil {
					log.Infof("Failed to start self: %s", err)
				} else {
					log.Infof("Successfully started self")
				}
			case syscall.SIGCHLD:
				log.Warningf("Child exited, got '%s', collecting status", signal)
				var wait syscall.WaitStatus
				syscall.Wait4(-1, &wait, syscall.WNOHANG, nil)
				log.Warningf("Collected exit status from child")
			default:
				log.Infof("Ignoring '%s'", signal)
			}
		case err := <-s.errorC:
			log.Infof("Got request to shutdown with error: %s", err)
			return err
		}
	}
}
Example #21
0
		var formatter logrus.Formatter = new(logrus.JSONFormatter)
		switch lg.config.GetStringWithDefault("log.console.format", "json") {
		case "text":
			formatter = new(logrus.TextFormatter)
		case "logstash":
			formatter = &logrus_logstash.LogstashFormatter{Type: lg.config.Product}
		}
		lg.Formatter = formatter
	},
	"log.syslog": func(lg *Log) {
		cfg := lg.config
		t := cfg.GetString("log.syslog.transport")
		s := cfg.GetString("log.syslog.server")
		l := lg.parseSyslog(cfg.GetString("log.hooks.syslog.level"))
		tag := cfg.Product
		hook, err := logrus_syslog.NewSyslogHook(t, s, l, tag)
		if err != nil {
			log.Fatalf("Log: cannot add syslog hook: %s", err)
		}
		lg.Hooks.Add(hook)
	},
}

func (lg *Log) parseSyslog(level string) syslog.Priority {
	switch level {
	case "panic":
		return syslog.LOG_EMERG
	case "fatal":
		return syslog.LOG_CRIT
	case "error":
		return syslog.LOG_ERR
Example #22
0
func main() {
	var err error

	readConfig(true)
	err = validateConfig()
	if err != nil {
		fmt.Printf("\n%s\n\n", err)
		flag.Usage()
		os.Exit(1)
	}

	if Config.ShowVersion {
		fmt.Printf("statsdaemon v%s (built w/%s)\n", VERSION, runtime.Version())
		os.Exit(0)
	}

	if Config.PrintConfig {
		out, _ := yaml.Marshal(Config)
		fmt.Printf("# Default config file in YAML based on config options\n%s", string(out))
		os.Exit(0)
	}

	log.SetLevel(Config.InternalLogLevel)

	if Config.LogName == "stdout" {
		log.SetOutput(os.Stdout)
	} else {
		if len(Config.LogName) > 0 {
			logFile, err := os.OpenFile(Config.LogName, os.O_WRONLY|os.O_CREATE, 0640)
			if err != nil {
				fmt.Printf("Error opennig log file: %s\n", err)
				os.Exit(1)
			}
			log.SetFormatter(&log.TextFormatter{DisableColors: true})
			log.SetOutput(logFile)
			defer logFile.Close()
		}
	}

	if Config.LogToSyslog {
		// set syslog
		syslogNetwork := ""
		if len(Config.SyslogUDPAddress) > 0 {
			syslogNetwork = "udp"
		}
		hook, err := logrus_syslog.NewSyslogHook(syslogNetwork, Config.SyslogUDPAddress, syslog.LOG_DEBUG|syslog.LOG_LOCAL3, "statsdaemon")
		if err != nil {
			fmt.Printf("Unable to connect to syslog daemon: %s\n", err)
		} else {
			log.AddHook(hook)
			log.SetFormatter(&log.JSONFormatter{})
		}
	}

	if Config.LogName == "" {
		log.SetOutput(ioutil.Discard)
	}

	// if Config.LogLevel == "debug" {
	// 	go func() {
	// 		log.Println(http.ListenAndServe(":8082", nil))
	// 	}()
	// }

	// Stat
	Stat.Interval = Config.FlushInterval

	signalchan = make(chan os.Signal, 1)
	signal.Notify(signalchan, syscall.SIGTERM, syscall.SIGQUIT)

	dbHandle, err = bolt.Open(Config.StoreDb, 0644, &bolt.Options{Timeout: 1 * time.Second})
	if err != nil {
		log.WithFields(log.Fields{
			"in":    "main",
			"after": "Open",
			"ctx":   "Bolt DB open",
		}).Fatalf("%s", err)
	}
	// dbHandle.NoSync = true

	defer dbHandle.Close()

	go udpListener()
	if Config.TCPServiceAddress != "" {
		go tcpListener()
	}
	monitor()
}