本文整理汇总了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)
}
}
示例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()
}
示例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
}
示例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:"`)
}
}
示例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)
}
示例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)
}
示例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)
}
示例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)
}
}
示例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
}
示例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)
}
}
}
示例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)
}
示例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
}
示例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)})
}
}
示例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")
}
}
示例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
}