當前位置: 首頁>>代碼示例>>Golang>>正文


Golang logrus.AddHook函數代碼示例

本文整理匯總了Golang中github.com/Sirupsen/logrus.AddHook函數的典型用法代碼示例。如果您正苦於以下問題:Golang AddHook函數的具體用法?Golang AddHook怎麽用?Golang AddHook使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了AddHook函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Execute

func (mr *RunCommand) Execute(context *cli.Context) {
	svcConfig := &service.Config{
		Name:        mr.ServiceName,
		DisplayName: mr.ServiceName,
		Description: defaultDescription,
		Arguments:   []string{"run"},
		Option: service.KeyValue{
			"RunWait": mr.runWait,
		},
	}

	service, err := service_helpers.New(mr, svcConfig)
	if err != nil {
		log.Fatalln(err)
	}

	if mr.Syslog {
		log.SetFormatter(new(log.TextFormatter))
		logger, err := service.SystemLogger(nil)
		if err == nil {
			log.AddHook(&ServiceLogHook{logger, log.InfoLevel})
		} else {
			log.Errorln(err)
		}
	}

	log.AddHook(&mr.sentryLogHook)

	err = service.Run()
	if err != nil {
		log.Fatalln(err)
	}
}
開發者ID:bssthu,項目名稱:gitlab-ci-multi-runner,代碼行數:33,代碼來源:multi.go

示例2: init

func init() {

	log.AddHook(&SourceFileHook{LogLevel: log.InfoLevel})
	log.AddHook(&SourceFileHook{LogLevel: log.WarnLevel})
	log.AddHook(&SourceFileHook{LogLevel: log.ErrorLevel})
	log.AddHook(&SourceFileHook{LogLevel: log.PanicLevel})
	// initialize values from flag
	flag.StringVar(&configDir, "config", "./assets/config/", "Path to the config dir ")
	flag.Parse()
}
開發者ID:tomyhero,項目名稱:billie,代碼行數:10,代碼來源:app.go

示例3: Configure

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
}
開發者ID:johnny-lai,項目名稱:bedrock,代碼行數:53,代碼來源:application.go

示例4: initLogging

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:"`)
	}
}
開發者ID:asteris-llc,項目名稱:vaultfs,代碼行數:51,代碼來源:utils.go

示例5: setupFileLogging

func setupFileLogging(logFile string) {
	log.Printf("Logging to file=[%s]", logFile)
	logrusFileHook := lfshook.NewHook(lfshook.PathMap{
		log.InfoLevel: logFile, log.WarnLevel: logFile, log.ErrorLevel: logFile, log.FatalLevel: logFile, log.PanicLevel: logFile,
	})
	log.AddHook(logrusFileHook)
}
開發者ID:ashmckenzie,項目名稱:sshoney,代碼行數:7,代碼來源:main.go

示例6: init

func init() {
	log.SetOutput(os.Stderr)
	log.SetLevel(log.DebugLevel)
	papertrailHost := os.Getenv("PAPERTRAIL_HOST")
	if papertrailHost == "" {
		return
	}
	papertrailPort := os.Getenv("PAPERTRAIL_PORT")
	port, err := strconv.Atoi(papertrailPort)
	if err != nil {
		log.Fatalf("Error getting Papertrail port: %v", err)
	}
	hostname, err := os.Hostname()
	if err != nil {
		log.Fatalf("Error getting hostname: %v", err)
	}
	hook, err := logrus_papertrail.NewPapertrailHook(
		&logrus_papertrail.Hook{
			Host:     papertrailHost,
			Port:     port,
			Hostname: hostname,
			Appname:  os.Getenv("APP_NAME"),
		},
	)
	if err != nil {
		log.Fatalf("Error building Papertrail hook: %v", err)
	}
	log.AddHook(hook)
}
開發者ID:codequest-eu,項目名稱:burnafterreading,代碼行數:29,代碼來源:server.go

示例7: configureSyslog

func configureSyslog(syslogParam string) {
	var err error
	var hook log.Hook

	// disable colors if we're writing to syslog *and* we're the default text
	// formatter, because the tty detection is useless here.
	if tf, ok := log.StandardLogger().Formatter.(*log.TextFormatter); ok {
		tf.DisableColors = true
	}

	if syslogParam == "kernel" {
		hook, err = logrus_syslog.NewSyslogHook("", "", syslog.LOG_INFO, "netplugin")
		if err != nil {
			log.Fatalf("Could not connect to kernel syslog")
		}
	} else {
		u, err := url.Parse(syslogParam)
		if err != nil {
			log.Fatalf("Could not parse syslog spec: %v", err)
		}

		hook, err = logrus_syslog.NewSyslogHook(u.Scheme, u.Host, syslog.LOG_INFO, "netplugin")
		if err != nil {
			log.Fatalf("Could not connect to syslog: %v", err)
		}
	}

	log.AddHook(hook)
}
開發者ID:karamsivia,項目名稱:netplugin,代碼行數:29,代碼來源:netd.go

示例8: Execute

func (c *RunCommand) Execute(context *cli.Context) {
	svcConfig := &service.Config{
		Name:        c.ServiceName,
		DisplayName: c.ServiceName,
		Description: defaultDescription,
		Arguments:   []string{"run"},
	}

	service, err := service_helpers.New(c, svcConfig)
	if err != nil {
		log.Fatalln(err)
	}

	if c.Syslog {
		logger, err := service.SystemLogger(nil)
		if err == nil {
			log.AddHook(&ServiceLogHook{logger})
		} else {
			log.Errorln(err)
		}
	}

	err = service.Run()
	if err != nil {
		log.Fatalln(err)
	}
}
開發者ID:zhangxq5012,項目名稱:gitlab-ci-multi-runner,代碼行數:27,代碼來源:multi.go

示例9: Connect

func Connect() bool {

	success := false

	logrus.SetFormatter(&logrus.JSONFormatter{})

	if !debugFlag {
		logrus.SetOutput(ioutil.Discard)
	} else {
		logrus.SetOutput(os.Stdout)
	}
	if fluentdServer != "" {
		host, port, err := net.SplitHostPort(fluentdServer)
		if err != nil {
			LogError(fmt.Sprintf("Unable to split to fluentd server [%s]!\n", fluentdServer), err.Error())
		}
		portInt, err := strconv.Atoi(port)

		if err != nil {
			LogError(fmt.Sprintf("Unable to convert port format [%s]!\n", port), err.Error())

		}
		hook := logrus_fluent.NewHook(host, portInt)
		LogStd(fmt.Sprintf("Received hook to fluentd server [%s]!\n", fluentdServer), false)
		logrus.AddHook(hook)
		success = true
	}

	return success
}
開發者ID:shinji62,項目名稱:firehose-to-fluentd,代碼行數:30,代碼來源:logging.go

示例10: setupLogging

func setupLogging() {
	if config.Settings.Moire.Debug {
		log.Info("Logging in DEBUG mode")
		log.SetLevel(log.DebugLevel)
	}

	// log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
	log.SetFormatter(&log.TextFormatter{})

	if config.Settings.Moire.SentryDSN != "" {
		levels := []log.Level{
			log.PanicLevel,
			log.FatalLevel,
			log.ErrorLevel,
		}

		hook, err := logrus_sentry.NewSentryHook(config.Settings.Moire.SentryDSN, levels)
		if err != nil {
			log.Error("Unable to connect to sentry")
		} else {
			log.Info("Adding Sentry Hook")
			log.AddHook(hook)
		}
	}
}
開發者ID:josjevv,項目名稱:moire,代碼行數:25,代碼來源:setup.go

示例11: loadConfiguration

// loadConfiguration loads the configuration of application
func loadConfiguration(app *AppConfig, rdis *RedisConfig, nats *NatsConfig) {
	err := envconfig.Process(ServiceName, app)
	if err != nil {
		log.Panicln(err)
	}
	err = envconfig.Process("redis", rdis)
	if err != nil {
		log.Panicln(err)
	}
	err = envconfig.Process("nats", nats)
	if err != nil {
		log.Panicln(err)
	}
	if len(os.Getenv(KeyLogly)) > 0 {
		log.Printf("Loading logly token %s \n", os.Getenv(KeyLogly))
		hook := logrusly.NewLogglyHook(os.Getenv(KeyLogly),
			app.Host,
			log.InfoLevel,
			app.Name)
		log.AddHook(hook)
	}

	log.Println("#### LOADED CONFIG #####")
	log.Printf("REDIS_URI: %s \n", rdis.URI)
	log.Printf("NATS_ENDPOINT: %s \n", nats.Endpoint)
}
開發者ID:suricatatalk,項目名稱:gate,代碼行數:27,代碼來源:gate.go

示例12: getConfig

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
}
開發者ID:x-cray,項目名稱:marathon-registrator,代碼行數:35,代碼來源:registrator.go

示例13: SetupLogging

// SetupLogging sets up logging. if token is not and empty string a rollbar
// hook is added with the environment set to env. The log formatter is set to a
// TextFormatter with timestamps disabled, which is suitable for use on Heroku.
func SetupLogging(token, env string) {
	log.SetFormatter(&log.TextFormatter{DisableTimestamp: true})

	if token != "" {
		log.AddHook(&Hook{Client: roll.New(token, env)})
	}
}
開發者ID:heroku,項目名稱:log-iss,代碼行數:10,代碼來源:rollrus.go

示例14: setupSentry

func (i *CLI) setupSentry() {
	if i.Config.SentryDSN == "" {
		return
	}

	levels := []logrus.Level{
		logrus.PanicLevel,
		logrus.FatalLevel,
	}

	if i.Config.SentryHookErrors {
		levels = append(levels, logrus.ErrorLevel)
	}

	sentryHook, err := NewSentryHook(i.Config.SentryDSN, levels)

	if err != nil {
		i.logger.WithField("err", err).Error("couldn't create sentry hook")
	}

	logrus.AddHook(sentryHook)

	err = raven.SetDSN(i.Config.SentryDSN)
	if err != nil {
		i.logger.WithField("err", err).Error("couldn't set DSN in raven")
	}
}
開發者ID:apjanke,項目名稱:worker,代碼行數:27,代碼來源:cli.go

示例15: Connect

func Connect() bool {

	success := false

	logrus.SetFormatter(&logrus.JSONFormatter{})

	if !debugFlag {
		logrus.SetOutput(ioutil.Discard)
	} else {
		logrus.SetOutput(os.Stdout)
	}
	if syslogServer != "" {
		hook, err := logrus_syslog.NewSyslogHook("tcp", syslogServer, syslog.LOG_INFO, "doppler")
		if err != nil {
			LogError(fmt.Sprintf("Unable to connect to syslog server [%s]!\n", syslogServer), err.Error())
		} else {
			LogStd(fmt.Sprintf("Received hook to syslog server [%s]!\n", syslogServer), false)
			logrus.AddHook(hook)

			success = true
		}
	}

	return success
}
開發者ID:msschwartz,項目名稱:firehose-to-syslog,代碼行數:25,代碼來源:logging.go


注:本文中的github.com/Sirupsen/logrus.AddHook函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。