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) } }
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() }
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 }
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:"`) } }
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) }
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) }
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) }
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) } }
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 }
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) } } }
// 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) }
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 }
// 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)}) } }
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") } }
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 }
func setupLogging(token, env string, levels []log.Level) { log.SetFormatter(&log.TextFormatter{DisableTimestamp: true}) if token != "" { log.AddHook(&Hook{Client: roll.New(token, env), triggers: levels}) } }
func init() { // 初始化日志 logrus.SetFormatter(&logrus.JSONFormatter{}) log.SetOutput(logrus.StandardLogger().Out) // 初始化配置 Config = loadConfig() lv, err := logrus.ParseLevel(Config.LogLevel) if err != nil { logrus.WithError(err).Warn("解析日志等级出错") lv = logrus.DebugLevel } logrus.SetLevel(lv) if Config.ReportOn { hook, err := logrus_mail.NewMailAuthHook(Config.AppName, Config.MailerHost, Config.MailerPort, Config.MailerUser, Config.MailReceiver, Config.MailerUser, Config.MailerPass) if err == nil { logrus.AddHook(hook) } } exeRoot := filepath.Dir(os.Args[0]) f, err := os.OpenFile(filepath.Join(exeRoot, "retask.log"), os.O_APPEND|os.O_CREATE, os.ModePerm) if err != nil { logrus.WithError(err).Fatal("打开日志文件失败") } logrus.SetOutput(f) }
func init() { hook, err := logredis.NewHook("localhost", 6379, "my_redis_key", "v0", log.DebugLevel, false, 1000) if err == nil { log.AddHook(hook) } else { log.Error(err) } }
// Installs a test hook for the global logger. func NewGlobal() *Hook { hook := new(Hook) logrus.AddHook(hook) return hook }
func Init() { initSender() logrus.SetFormatter(&formatter{}) logrus.AddHook(&logHook{}) logrus.SetOutput(os.Stderr) logrus.SetLevel(logrus.InfoLevel) }
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) }
func main() { flag.Parse() // connect with the logging server if logger != "" && (amroot || debug) { // blocks until we can connect to the logger lh, err := logutils.NewLoggerHook(logger, hostname, app) if err != nil { log.WithFields(log.Fields{ "file": logutils.File(), }).Fatalln("ERROR SETTING UP LOGGING SERVER:", err) } log.AddHook(lh) } //log.Println("IN FORKEXEC") ////log.SetOutput(ioutil.Discard) ////log.Println("Log Test") ////fmt.Println("exiting logger block") //} // log.Println("IN FORK EXEC") // recombine the flags for exec to use args := []string{ "-failures=" + strconv.Itoa(failures), "-rfail=" + strconv.Itoa(rFail), "-ffail=" + strconv.Itoa(fFail), "-hostname=" + hostname, "-config=" + configFile, "-logger=" + logger, "-app=" + app, "-pprof=" + pprofaddr, "-physaddr=" + physaddr, "-rootwait=" + strconv.Itoa(rootwait), "-debug=" + strconv.FormatBool(debug), "-rounds=" + strconv.Itoa(rounds), "-amroot=" + strconv.FormatBool(amroot), "-test_connect=" + strconv.FormatBool(testConnect), "-suite=" + suite, } cmd := exec.Command("./exec", args...) cmd.Stdout = log.StandardLogger().Writer() cmd.Stderr = log.StandardLogger().Writer() // log.Println("running command:", cmd) err := cmd.Run() if err != nil { log.Errorln("cmd run:", err) } // get CPU usage stats st := cmd.ProcessState.SystemTime() ut := cmd.ProcessState.UserTime() log.WithFields(log.Fields{ "file": logutils.File(), "type": "forkexec", "systime": st, "usertime": ut, }).Info("") }
func init() { // ORDER MATTERs // configuration initConfig() // logging l := must(logrus.ParseLevel(config.Log.Level)).(logrus.Level) logrus.SetLevel(l) logrus.SetOutput(os.Stdout) // logging hooks graylogHookLevelThreshold := must(logrus.ParseLevel(config.Log.Graylog.Level)).(logrus.Level) graylogHook := must( grayloghook.New( config.Log.Graylog.Address, config.Log.Graylog.Facility, map[string]interface{}{ "go_version": goVersion, "build_time": buildTime, "git_commit": gitCommit, }, graylogHookLevelThreshold, ), ).(logrus.Hook) logrus.AddHook(graylogHook) // connection hub connsHub = list.New() // storage s := mysql.New(config.DB.DataSourceName) s.SetMaxOpenConns(config.DB.MaxOpenConns) s.SetMaxIdleConns(config.DB.MaxIdleConns) store = s // cache memoryCache = memory.New(config.Cache.NumCachedIncomes) total := must(store.GetLatestTotalReward()).(models.TotalReward) memoryCache.IncrementTotalReward(total.CreatedAt, total.Total) updateCache() // coin client initCoinClient(config.Coin.Type) // cronjob initCronjob(config.Coin.Type, config.CronjobSpec.CreateWithdrawal, config.CronjobSpec.ProcessWithdrawal) // mailer mailer = mandrill.New(config.Mandrill.Key, config.Mandrill.FromEmail, config.Mandrill.FromName) // geetest geetest = gt.New(config.Geetest.CaptchaID, config.Geetest.PrivateKey, false, time.Second*10, time.Second*10, 2048) // geo geo = must(geoip2.Open(config.Geo.Database)).(*geoip2.Reader) }
func initService(daemonCli *DaemonCli) (bool, error) { if *flUnregisterService { if *flRegisterService { return true, errors.New("--register-service and --unregister-service cannot be used together") } return true, unregisterService() } if *flRegisterService { return true, registerService() } if !*flRunService { return false, nil } interactive, err := svc.IsAnInteractiveSession() if err != nil { return false, err } h := &handler{ tosvc: make(chan bool), fromsvc: make(chan error), daemonCli: daemonCli, } var log *eventlog.Log if !interactive { log, err = eventlog.Open(*flServiceName) if err != nil { return false, err } } logrus.AddHook(&etwHook{log}) logrus.SetOutput(ioutil.Discard) service = h go func() { if interactive { err = debug.Run(*flServiceName, h) } else { err = svc.Run(*flServiceName, h) } h.fromsvc <- err }() // Wait for the first signal from the service handler. err = <-h.fromsvc if err != nil { return false, err } return false, nil }
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) } }
func AddLogFile(filename string) { log.AddHook(lfshook.NewHook(lfshook.PathMap{ log.DebugLevel: filename, log.InfoLevel: filename, log.WarnLevel: filename, log.ErrorLevel: filename, log.FatalLevel: filename, log.PanicLevel: filename, })) }
func (i *CLI) setupSentry() { if i.Config.SentryDSN != "" { sentryHook, err := NewSentryHook(i.Config.SentryDSN, []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel}) if err != nil { i.logger.WithField("err", err).Error("couldn't create sentry hook") } logrus.AddHook(sentryHook) } }
func TestSMPPTranceiver(t *testing.T) { // формируем параметры авторизации bindParams := smpp.Params{ smpp.SYSTEM_TYPE: "SMPP", smpp.SYSTEM_ID: "Zultys", smpp.PASSWORD: "******", } logrus.SetLevel(logrus.DebugLevel) logrus.AddHook(lfshook.NewHook(lfshook.PathMap{ logrus.InfoLevel: "info.log", logrus.ErrorLevel: "error.log", logrus.DebugLevel: "debug.log", logrus.WarnLevel: "warning.log", })) logrus.SetFormatter(new(prefixed.TextFormatter)) Logger := logrus.StandardLogger().WithField("smpp", addr) trx, err := NewTransceiver(addr, 0, bindParams, Logger) if err != nil { Logger.WithError(err).Error("Connect error") t.Fatal(err) } defer trx.Close() receive := make(chan interface{}, 10) // канал для получения SMS status := make(chan Status, 10) // канал для получения статусов доставки response := make(chan SendResponse, 10) // канал для получения подтверждений отправки go func() { for { select { case msg := <-receive: Logger.Debugln("Receive:", msg) case msg := <-status: Logger.Debugln("Status:", msg) case msg := <-response: Logger.Debugln("Response:", msg) } } }() go trx.reading(receive) // запускаем получение данных с сервера for _, text := range texts { time.Sleep(time.Second * 10) sms := &SendMessage{ From: from, To: to2, Text: text, } err := trx.Send(sms) if err != nil { Logger.WithError(err).Error("Send error") t.Error(err) } Logger.WithField("seqs", sms.Seq).Info("Sended") } time.Sleep(time.Minute * 5) }
func prepareFlags(c *cli.Context) error { if c.Bool("debug") { os.Setenv("DEBUG", "1") log.SetOutput(os.Stderr) log.SetLevel(log.DebugLevel) log.AddHook(&log_hook.SourceFileHook{LogLevel: log.InfoLevel}) } os.Setenv("TT_ORIGIN", filepath.Clean(c.String("origin"))) os.Setenv("TT_CONFIG", filepath.Clean(c.String("config"))) return nil }
func (s *Server) ConnectToLogger() { return if s.Logger == "" || s.Hostname == "" || s.App == "" { dbg.Lvl4("skipping connect to logger") return } dbg.Lvl4("Connecting to Logger") lh, _ := logutils.NewLoggerHook(s.Logger, s.Hostname, s.App) dbg.Lvl4("Connected to Logger") log.AddHook(lh) }