func InitializeLogging(config *LoggingConfig, useStd bool, level string) { var lvl = log.DebugLevel var e error if level != "" { lvl, e = log.ParseLevel(level) } else if config.Level != "" { lvl, e = log.ParseLevel(config.Level) } if nil != e { lvl = log.DebugLevel } if useStd { log.SetOutput(os.Stdout) OUTPUT_FILE = nil } else { f, e := os.OpenFile(config.Filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) if nil != e { fmt.Errorf("Open file <%s> for logging failed<%v>!\n", config.Filename, e) } else { log.SetOutput(f) OUTPUT_FILE = f } } if strings.ToLower(config.Format) == "json" { log.SetFormatter(&log.JSONFormatter{}) } else { log.SetFormatter(&PlainFormatter{}) } log.SetLevel(lvl) //log.Info("Logging Initialized.") }
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 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) } } } }
// This is a minimal commandline client to connect through a websocket func main() { kingpin.Parse() // set log level level, err := log.ParseLevel(*logLevel) if err != nil { logger.WithField("error", err).Fatal("Invalid log level") } log.SetLevel(level) origin := "http://localhost/" url := fmt.Sprintf("%v/user/%v", removeTrailingSlash(*url), *user) client, err := client.Open(url, origin, 100, true) if err != nil { log.Fatal(err) } go writeLoop(client) go readLoop(client) for _, cmd := range *commands { client.WriteRawMessage([]byte(cmd)) } if *exit { return } waitForTermination(func() {}) }
// NewLogger returns a log configured with the input parameters. func NewLogger(level, writer string, maxSize, maxAge, maxBackups int) *logrus.Entry { base := logrus.New() switch writer { case "stdout": base.Out = os.Stdout case "stderr": base.Out = os.Stderr case "": base.Out = ioutil.Discard default: base.Out = &lumberjack.Logger{ Filename: writer, MaxSize: maxSize, MaxAge: maxAge, MaxBackups: maxBackups, } } logrusLevel, err := logrus.ParseLevel(level) // if provided level is not supported, default to Info level if err != nil { base.Error(err) logrusLevel = logrus.InfoLevel } base.Level = logrusLevel log := logrus.NewEntry(base) return log }
func before(c *cli.Context) error { initLogFormatter() initHelpAndVersionFlags() initAppHelpTemplate() // Log level if logLevel, err := log.ParseLevel(c.String(LogLevelKey)); err != nil { log.Fatal("[BITRISE_CLI] - Failed to parse log level:", err) } else { log.SetLevel(logLevel) } // Befor parsing cli, and running command // we need to decide wich path will be used by envman envman.CurrentEnvStoreFilePath = c.String(PathKey) if envman.CurrentEnvStoreFilePath == "" { if path, err := envStorePathInCurrentDir(); err != nil { log.Fatal("[ENVMAN] - Failed to set envman work path in current dir:", err) } else { envman.CurrentEnvStoreFilePath = path } } envman.ToolMode = c.Bool(ToolKey) if envman.ToolMode { log.Info("[ENVMAN] - Tool mode on") } if _, err := envman.GetConfigs(); err != nil { log.Fatal("[ENVMAN] - Failed to init configs:", err) } return nil }
func initLogger() error { dirPath, _ := filepath.Abs(filepath.Dir(pConfig.Log.Path)) if _, err := os.Stat(dirPath); os.IsNotExist(err) { os.Mkdir(dirPath, 0775) } file, err := os.OpenFile(pConfig.Log.Path, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666) if err != nil { return err } level, err := logrus.ParseLevel(pConfig.Log.Level) if err != nil { return err } pLog = &logrus.Logger{ Out: file, Level: level, Formatter: new(logrus.JSONFormatter), } pLog.Infof("InitLogger: path: %s, level: %s, formatter: json", pConfig.Log.Path, level) return nil }
func Execute() { config.GetConfig().Load() checkFleetVersion() var logLevel string var rootCmd = &cobra.Command{ Use: "green-garden", PersistentPreRun: func(cmd *cobra.Command, args []string) { level, err := log.ParseLevel(logLevel) if err != nil { fmt.Printf("Unknown log level : %s\n", logLevel) os.Exit(1) } log.SetLevel(level) }, } rootCmd.PersistentFlags().StringVarP(&logLevel, "loglevel", "L", "info", "Set log level") rootCmd.AddCommand(versionCmd, generateCmd) loadEnvCommands(rootCmd) err := rootCmd.Execute() if err != nil { os.Exit(1) } log.Info("Victory !") }
func SetLogLevel(levelname string) { level, err := logrus.ParseLevel(levelname) if err != nil { Log.Fatal(err) } Log.Level = level }
func (a *appConfig) getAndValidateConfig(cmd *cobra.Command) error { var err error a.port = viper.GetString("port") a.debug = viper.GetBool("debug") a.cpu = viper.GetInt("cpu") a.allowDegradedMode = viper.GetBool("allow-degraded-mode") // check logLevel values and set initLogLevel a.logLevel = viper.GetString("log-level") a.initLogLevel, err = log.ParseLevel(a.logLevel) if err != nil { err = fmt.Errorf("Invalid log level: \"%s\"\n%s", a.logLevel, cmd.Flags().Lookup("log-level").Usage) return err } // check db name a.db = viper.GetString("db") if a.db == "" { err = fmt.Errorf("Db name can't be empty\n%s", cmd.Flags().Lookup("db").Usage) return err } // Kafka brokers a.kafkaBrokerList = strings.Split(viper.GetString("kafka-brokers"), ",") if len(a.kafkaBrokerList) == 0 { err = fmt.Errorf("Broker list is empty or invalid format (%s)\n%s", a.kafkaBrokers, cmd.Flags().Lookup("kafka-brokers").Usage) return err } return nil }
// SetLevel sets the log level. Valid levels are panic, fatal, error, warn, info and debug. func SetLevel(level string) { lvl, err := log.ParseLevel(level) if err != nil { Fatal(fmt.Sprintf(`not a valid level: "%s"`, level)) } log.SetLevel(lvl) }
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 Configure(loglevel string) { if level, err := logrus.ParseLevel(loglevel); err != nil { Logger.Fatal(err) } else { Logger.Level = level } }
func enableFileLogger() { flogger := serverConfig.GetFileLogger() if !flogger.Enable || flogger.Filename == "" { return } // Creates the named file with mode 0666, honors system umask. file, err := os.OpenFile(flogger.Filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666) fatalIf(err, "Unable to open log file.") fileLogger := logrus.New() // Add a local file hook. fileLogger.Hooks.Add(&localFile{file}) lvl, err := logrus.ParseLevel(flogger.Level) fatalIf(err, "Unknown log level found in the config file.") // Set default JSON formatter. fileLogger.Out = ioutil.Discard fileLogger.Formatter = new(logrus.JSONFormatter) fileLogger.Level = lvl // Minimum log level. log.mu.Lock() log.loggers = append(log.loggers, fileLogger) log.mu.Unlock() }
func (config *Config) setLogLevel() { level, err := log.ParseLevel(config.LogLevel) if err != nil { log.WithField("level", config.LogLevel).Fatal("bad level") } log.SetLevel(level) }
func NewSlackHook(options logrus_mate.Options) (hook logrus.Hook, err error) { conf := SlackHookConfig{} if err = options.ToObject(&conf); err != nil { return } levels := []logrus.Level{} if conf.Levels != nil { for _, level := range conf.Levels { if lv, e := logrus.ParseLevel(level); e != nil { err = e return } else { levels = append(levels, lv) } } } if len(levels) == 0 && conf.Levels != nil { levels = append(levels, logrus.ErrorLevel, logrus.PanicLevel, logrus.FatalLevel) } hook = &slackrus.SlackrusHook{ HookURL: conf.URL, AcceptedLevels: levels, Channel: conf.Channel, IconEmoji: conf.Emoji, Username: conf.Username, } return }
func main() { app := cli.NewApp() app.Name = path.Base(os.Args[0]) app.Usage = "An entropy and failure injection management API for Docker platforms." app.Version = VERSION app.Authors = []cli.Author{{Name: "Jeff Nickoloff", Email: "*****@*****.**"}} app.Flags = flags app.Commands = commands app.Before = func(c *cli.Context) error { log.SetOutput(os.Stderr) level, err := log.ParseLevel(c.String("log-level")) if err != nil { log.Fatalf(err.Error()) } log.SetLevel(level) // If a log level wasn't specified and we are running in debug mode, // enforce log-level=debug. if !c.IsSet("log-level") && !c.IsSet("l") && c.Bool("debug") { log.SetLevel(log.DebugLevel) } return nil } if err := app.Run(os.Args); err != nil { log.Fatal(err) } }
func initLogrus(ctx *cli.Context) { logrus.SetFormatter(&logrus.TextFormatter{ TimestampFormat: time.RFC822, FullTimestamp: true, }) if level, err := logrus.ParseLevel(ctx.String("log_level")); err == nil { logrus.SetLevel(level) } else { log.Error(err) logrus.SetLevel(logrus.InfoLevel) } filename := ctx.String("log_file") logrus.SetOutput(os.Stderr) if filename != "" { var f *os.File _, err := os.Stat(filename) if !os.IsNotExist(err) { os.Rename(filename, filename+".prev") } f, err = os.Create(filename) if err != nil { log.Error("Cannot create log file ", err) log.Warning("Continuing to log to stderr") } else { logrus.SetOutput(f) } } }
func before(c *cli.Context) error { // Log level if logLevel, err := log.ParseLevel(c.String(LogLevelKey)); err != nil { log.Fatal("Failed to parse log level:", err) } else { log.SetLevel(logLevel) } if len(c.Args()) != 0 && c.Args().First() != "version" && !c.Bool(HelpKey) && !c.Bool(VersionKey) { if err := MachineWorkdir.Set(c.String(WorkdirKey)); err != nil { log.Fatalf("Failed to set MachineWorkdir: %s", err) } if MachineWorkdir.String() == "" { log.Fatalln("No Workdir specified!") } } MachineWorkdir.Freeze() if err := MachineConfigTypeID.Set(c.String(ConfigTypeIDParamKey)); err != nil { log.Fatalf("Failed to set MachineConfigTypeID: %s", err) } log.Debugf("MachineConfigTypeID: %s", MachineConfigTypeID) if err := MachineParamsAdditionalEnvs.Set(c.StringSlice(EnvironmentParamKey)); err != nil { log.Fatalf("Failed to set MachineParamsAdditionalEnvs: %s", err) } log.Debugf("MachineParamsAdditionalEnvs: %s", MachineParamsAdditionalEnvs) MachineParamsAdditionalEnvs.Freeze() return nil }
// Erect the application. Once completed, the application should have // all the services installed and all the necessary configurations done // before invoking the deploy sequence. Basically, this method will do // everything to get the application configured and be ready to launch. // Method itself however will not launch the app; see Deploy for that. func (app *App) Boot(env, level, root string) { const eenv = "environment name must be 1 word" const estat = "could not open the specified root" pattern := regexp.MustCompile("^[a-zA-Z0-9]+$") parsedLevel, err := logrus.ParseLevel(level) if err != nil { panic("wrong logging level") } if !pattern.MatchString(env) { panic(eenv) } if _, e := os.Stat(root); e != nil { panic(estat) } app.RootDirectory = filepath.Clean(root) app.Journal = app.makeJournal(parsedLevel) app.Env = strings.ToLower(strings.TrimSpace(env)) app.Config = app.loadConfig(app.Env, "config") app.Booted = time.Now() // mark app as booted for _, p := range app.Providers { // setups if p.Available[env] { // env available? p.Invoked = time.Now() p.Setup(app) } // setup provider only if availale } // all the providers have been invoked for _, s := range app.Services { s.Up(app) } log := app.Journal.WithField("env", app.Env) log = log.WithField("root", app.RootDirectory) log = log.WithField("level", parsedLevel) log.Info("application has been booted") app.CronEngine.Start() // launch CRON app.routers = app.assembleRouters() }
func initLog(name string, level string) error { if Log == nil { // Log as JSON instead of the default ASCII formatter. logrus.SetFormatter(&logrus.JSONFormatter{}) // Output to stderr instead of stdout, could also be a file. // logrus.SetOutput(os.Stderr) // logging level lvl, err := logrus.ParseLevel(level) if err != nil { return err } logrus.SetLevel(lvl) // default fields Log = logrus.WithFields(logrus.Fields{ "service": name, "ip": InternalIP, }) } return nil }
// InitLog initializes the logrus logger func InitLog(logLevel, formatter string) error { switch formatter { case LogStashFormatter: logrus.SetFormatter(&logstash.LogstashFormatter{ TimestampFormat: time.RFC3339, }) default: logrus.SetFormatter(&logrus.TextFormatter{ ForceColors: true, FullTimestamp: true, }) } logrus.SetOutput(os.Stdout) level, err := logrus.ParseLevel(logLevel) if err != nil { logrus.SetLevel(logrus.DebugLevel) return err } logrus.SetLevel(level) return nil }
// Load configuration settings from the environment, apply defaults, and validate them. func (c *Context) Load() error { if err := envconfig.Process("PIPE", &c.Settings); err != nil { return err } if c.Port == 0 { c.Port = 8000 } if c.LogLevel == "" { c.LogLevel = "info" } if c.MongoURL == "" { c.MongoURL = "mongo" } if c.Poll == 0 { c.Poll = 500 } if c.DockerHost == "" { if host := os.Getenv("DOCKER_HOST"); host != "" { c.DockerHost = host } else { c.DockerHost = "unix:///var/run/docker.sock" } } certRoot := os.Getenv("DOCKER_CERT_PATH") if certRoot == "" { certRoot = "/certificates" } if c.CACert == "" { c.CACert = path.Join(certRoot, "ca.pem") } if c.Cert == "" { c.Cert = path.Join(certRoot, "cloudpipe-cert.pem") } if c.Key == "" { c.Key = path.Join(certRoot, "cloudpipe-key.pem") } if c.DefaultImage == "" { c.DefaultImage = "cloudpipe/runner-py2" } if c.Settings.AuthService == "" { c.Settings.AuthService = "https://authstore:9001/v1" } if _, err := log.ParseLevel(c.LogLevel); err != nil { return err } return nil }
func configureLogging(v *viper.Viper) { level, err := log.ParseLevel(v.GetString("log_level")) if err != nil { log.Fatalln(err) } log.SetLevel(level) if v.GetString("log_format") == "text" { log.SetFormatter(&log.TextFormatter{DisableColors: true, FullTimestamp: true}) } else if v.GetString("log_format") == "json" { log.SetFormatter(&log.JSONFormatter{}) } else { log.Errorln("Error: log_type invalid, defaulting to text") log.SetFormatter(&log.TextFormatter{}) } switch v.GetString("log_target") { case "stdout": log.SetOutput(os.Stdout) case "stderr": log.SetOutput(os.Stderr) default: log.Errorln("Error: log_target invalid, defaulting to Stdout") log.SetOutput(os.Stdout) } }
// New creates a new *logrus.Logger instnace using the provided config object. A closer function is returned // that should be used to close the logger and any open file descriptors before the application terminates. func New(config Config) (*logrus.Logger, func() error) { level, err := logrus.ParseLevel(config.Level) if err != nil { level = logrus.DebugLevel } out := os.Stdout closer := func() error { return nil } if config.File != "" { f, err := os.OpenFile(config.File, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0755) if err == nil { out = f closer = func() error { return f.Close() } } } return &logrus.Logger{ Out: out, Formatter: &config.Format, Hooks: make(logrus.LevelHooks), Level: level, }, closer }
func NewLogger() *Logger { outfile := GetConfig().GetLogFile() max_back_up := 10 max_log_size := 1024 * 1024 events := make(chan []byte, 40960) outdir := filepath.Dir(outfile) buf := make([]byte, MAX_LOG_BUFFER) buffer := bytes.NewBuffer(buf) buffer.Reset() file, fsize := openAndLockFile(outfile) regexp := outfile + ".*" writer := NewLogWriter(events) logrloger := logrus.New() loglevel, err := logrus.ParseLevel(GetConfig().GetLogLevel()) if err != nil { panic(err) } logrloger.Level = loglevel logrloger.Out = writer logrloger.Formatter = &logrus.TextFormatter{DisableColors: true, TimestampFormat: "2006-01-02T15:04:05"} t := time.NewTicker(MAX_LOG_INTERVAL) LOG = logrloger return &Logger{events, outfile, file, buffer, max_back_up, outdir, regexp, writer, logrloger, max_log_size, int(fsize), t} }
func setLogLevel(levelname string) { level, err := log.ParseLevel(levelname) if err != nil { log.Fatal(err) } log.SetLevel(level) }
// SetLogLevel will set the appropriate logrus log level // given the server config. func SetLogLevel(scfg *Config) { if lvl, err := logrus.ParseLevel(scfg.LogLevel); err != nil { Log.Level = logrus.InfoLevel } else { Log.Level = lvl } }
func main() { flag.Parse() if flLogLevel != "" { lvl, err := logrus.ParseLevel(flLogLevel) if err != nil { fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", flLogLevel) os.Exit(1) } logrus.SetLevel(lvl) } else { logrus.SetLevel(logrus.InfoLevel) } if flDebug { logrus.SetLevel(logrus.DebugLevel) } d, err := rbd.NewRbdDriver(root, graphOptions) if err != nil { logrus.Errorf("Create rbd driver failed: %v", err) os.Exit(1) } h := degraph.NewHandler(d) logrus.Infof("listening on %s\n", socketAddress) fmt.Println(h.ServeUnix("root", socketAddress)) }
// UpdateLogLevel updates the log level based on the config. func UpdateLogLevel(config gofig.Config) { ll, err := log.ParseLevel(config.GetString(types.ConfigLogLevel)) if err != nil { return } log.SetLevel(ll) }