func NewLogrusLogger(ctx *cli.Context) Logger { logFile := ctx.GlobalString("log-file") if logFile != "" { if err := os.MkdirAll(path.Dir(logFile), 0755); err != nil { logrus.Errorf("Failed to create path %s: %s", path.Dir(logFile), err.Error()) } else { file, err := os.OpenFile(logFile, syscall.O_CREAT|syscall.O_APPEND|syscall.O_WRONLY, 0644) if err != nil { logrus.Errorf("Failed to open log file %s: %s", logFile, err.Error()) } else { logrus.SetOutput(file) } } } switch ctx.GlobalString("log-format") { case "json": logrus.SetFormatter(&logrus.JSONFormatter{}) case "logstash": logrus.SetFormatter(&logstash.LogstashFormatter{}) default: logrus.SetFormatter(&logrus.TextFormatter{}) } if ctx.GlobalBool("debug") { logrus.SetLevel(logrus.DebugLevel) } return &logrusLogger{ logger: logrus.StandardLogger(), } }
func ConfigureLogging(level, format string, output io.Writer) error { switch level { case "debug": log.SetLevel(log.DebugLevel) case "info": log.SetLevel(log.InfoLevel) case "warn": log.SetLevel(log.WarnLevel) case "error": log.SetLevel(log.ErrorLevel) case "fatal": log.SetLevel(log.FatalLevel) case "panic": log.SetLevel(log.PanicLevel) default: return errors.New("Invalid log level: " + level) } switch format { case "text": log.SetFormatter(&log.TextFormatter{ TimestampFormat: time.RFC3339, FullTimestamp: true, }) case "json": log.SetFormatter(&log.JSONFormatter{ TimestampFormat: time.RFC3339, }) default: return errors.New("Invalid format type: " + format) } log.SetOutput(output) return nil }
func (service *Service) Manage() (string, error) { usage := "Usage: metronome install | remove | start | stop | status | agent\n" if flag.NArg() > 0 { switch flag.Args()[0] { case "install": return service.Install() case "remove": return service.Remove() case "start": return service.Start() case "stop": return service.Stop() case "status": return service.Status() case "agent": return agent() case "push": return scheduler.Push() case "dispatch": return dispatch(flag.Args()[1]) case "version": log.SetFormatter(&util.SimpleFormatter{}) return fmt.Sprintf("metronome %s\n", Version), nil default: log.SetFormatter(&util.SimpleFormatter{}) return usage, nil } } return agent() }
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) } }
func run() { fmtlog.SetFlags(fmtlog.Lshortfile | fmtlog.LstdFlags) // load global configuration globalConfiguration := LoadConfiguration() loggerMiddleware := middlewares.NewLogger(globalConfiguration.AccessLogsFile) defer loggerMiddleware.Close() // logging level, err := log.ParseLevel(strings.ToLower(globalConfiguration.LogLevel)) if err != nil { log.Fatal("Error getting level", err) } log.SetLevel(level) if len(globalConfiguration.TraefikLogsFile) > 0 { fi, err := os.OpenFile(globalConfiguration.TraefikLogsFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) defer fi.Close() if err != nil { log.Fatal("Error opening file", err) } else { log.SetOutput(fi) log.SetFormatter(&log.TextFormatter{DisableColors: true, FullTimestamp: true, DisableSorting: true}) } } else { log.SetFormatter(&log.TextFormatter{FullTimestamp: true, DisableSorting: true}) } log.Debugf("Global configuration loaded %+v", globalConfiguration) server := NewServer(*globalConfiguration) server.Start() defer server.Close() log.Info("Shutting down") }
// 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 }
// ResponseLogAndError would log the error before call ResponseError() func ResponseLogAndError(v interface{}) { if e, ok := v.(*logrus.Entry); ok { e.Error(e.Message) oldFormatter := e.Logger.Formatter logrus.SetFormatter(&logrus.JSONFormatter{}) s, err := e.String() logrus.SetFormatter(oldFormatter) if err != nil { ResponseError(err.Error()) return } // Cosmetic since " would be escaped ResponseError(strings.Replace(s, "\"", "'", -1)) } else { e, isErr := v.(error) _, isRuntimeErr := e.(runtime.Error) if isErr && !isRuntimeErr { logrus.Errorf(fmt.Sprint(e)) ResponseError(fmt.Sprint(e)) } else { logrus.Errorf("Caught FATAL error: %s", v) debug.PrintStack() ResponseError("Caught FATAL error: %s", v) } } }
func NewCustomizedLogger(level string, developerModeFlag bool) (*log.Logger, error) { logger := log.New() lv, err := log.ParseLevel(level) if err != nil { return nil, err } logger.Level = lv if developerModeFlag { customFormatter := new(log.TextFormatter) customFormatter.FullTimestamp = true customFormatter.TimestampFormat = "2006-01-02 15:04:05" log.SetFormatter(customFormatter) log.SetLevel(log.DebugLevel) logger.Level = lv logger.Formatter = customFormatter } else { customFormatter := new(log.JSONFormatter) customFormatter.TimestampFormat = "2006-01-02 15:04:05" log.SetFormatter(customFormatter) customFormatter.DisableTimestamp = false log.SetLevel(log.InfoLevel) logger.Level = lv logger.Formatter = customFormatter } logger.Out = os.Stdout return logger, nil }
func InitializeLogger(developerModeFlag bool) { logger = log.New() if developerModeFlag { customFormatter := new(log.TextFormatter) customFormatter.FullTimestamp = true customFormatter.TimestampFormat = "2006-01-02 15:04:05" log.SetFormatter(customFormatter) log.SetLevel(log.DebugLevel) logger.Level = log.DebugLevel logger.Formatter = customFormatter } else { customFormatter := new(log.JSONFormatter) customFormatter.TimestampFormat = "2006-01-02 15:04:05" log.SetFormatter(customFormatter) customFormatter.DisableTimestamp = false log.SetLevel(log.InfoLevel) logger.Level = log.InfoLevel logger.Formatter = customFormatter } logger.Out = os.Stdout }
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 main() { // configure logging logrus.SetLevel(logrus.InfoLevel) logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true}) // options to change log level globalOptions.Quiet = func() { logrus.SetLevel(logrus.WarnLevel) } globalOptions.Verbose = func() { logrus.SetLevel(logrus.DebugLevel) } globalOptions.LogJSON = func() { logrus.SetFormatter(&logrus.JSONFormatter{}) } globalOptions.LogSyslog = func() { setupSyslog() } originalArgs = os.Args if _, err := parser.Parse(); err != nil { os.Exit(1) } }
func setupLogging() { switch viper.GetString("log-level") { case "debug": log.SetLevel(log.DebugLevel) case "info": log.SetLevel(log.InfoLevel) case "warn": log.SetLevel(log.WarnLevel) case "error": log.SetLevel(log.ErrorLevel) case "fatal": log.SetLevel(log.FatalLevel) default: log.WithField("log-level", viper.GetString("log-level")).Warning("invalid log level. defaulting to info.") log.SetLevel(log.InfoLevel) } switch viper.GetString("log-format") { case "text": log.SetFormatter(new(log.TextFormatter)) case "json": log.SetFormatter(new(log.JSONFormatter)) default: log.WithField("log-format", viper.GetString("log-format")).Warning("invalid log format. defaulting to text.") log.SetFormatter(new(log.TextFormatter)) } }
// UpdateLoggingSettings initializes logging based on config params. func UpdateLoggingSettings() { format := LoggingFormatFlag.Get() switch format { case "colortext": logrus.SetFormatter(&prefixed.TextFormatter{ ShortTimestamp: true, ForceColors: true, }) case "text": logrus.SetFormatter(&logrus.TextFormatter{}) case "json": logrus.SetFormatter(&logrus.JSONFormatter{}) default: panic(fmt.Errorf("Invalid logging format %v", format)) } level := LoggingLevelFlag.Get() switch level { case "debug": logrus.SetLevel(logrus.DebugLevel) case "info": logrus.SetLevel(logrus.InfoLevel) case "warning": logrus.SetLevel(logrus.WarnLevel) case "error": logrus.SetLevel(logrus.ErrorLevel) case "fatal": logrus.SetLevel(logrus.FatalLevel) case "panic": logrus.SetLevel(logrus.PanicLevel) default: panic(fmt.Errorf("Invalid logging level %v", level)) } }
func initLogger() { if strings.ToUpper(config.OutputFormat) == "JSON" { log.SetFormatter(&log.JSONFormatter{}) } else { // The TextFormatter is default, you don't actually have to do this. log.SetFormatter(&log.TextFormatter{}) } }
func ConfigureLogger() { switch viper.GetString("ENV") { case "DEVELOPMENT": log.SetFormatter(&log.TextFormatter{ForceColors: true}) case "PRODUCTION": log.SetFormatter(&log.JSONFormatter{}) default: log.SetFormatter(&log.TextFormatter{}) } }
// configureLogging prepares the context with a logger using the // configuration. func configureLogging(ctx context.Context, config *configuration.Configuration) (context.Context, error) { if config.Log.Level == "" && config.Log.Formatter == "" { // If no config for logging is set, fallback to deprecated "Loglevel". log.SetLevel(logLevel(config.Loglevel)) ctx = context.WithLogger(ctx, context.GetLogger(ctx, "version")) return ctx, nil } log.SetLevel(logLevel(config.Log.Level)) formatter := config.Log.Formatter if formatter == "" { formatter = "text" // default formatter } switch formatter { case "json": log.SetFormatter(&log.JSONFormatter{ TimestampFormat: time.RFC3339Nano, }) case "text": log.SetFormatter(&log.TextFormatter{ TimestampFormat: time.RFC3339Nano, }) case "logstash": log.SetFormatter(&logstash.LogstashFormatter{ TimestampFormat: time.RFC3339Nano, }) default: // just let the library use default on empty string. if config.Log.Formatter != "" { return ctx, fmt.Errorf("unsupported logging formatter: %q", config.Log.Formatter) } } if config.Log.Formatter != "" { log.Debugf("using %q logging formatter", config.Log.Formatter) } // log the application version with messages ctx = context.WithLogger(ctx, context.GetLogger(ctx, "version")) if len(config.Log.Fields) > 0 { // build up the static fields, if present. var fields []interface{} for k := range config.Log.Fields { fields = append(fields, k) } ctx = context.WithValues(ctx, config.Log.Fields) ctx = context.WithLogger(ctx, context.GetLogger(ctx, fields...)) } return ctx, nil }
func init() { // set this if environment is production log.SetFormatter(&log.JSONFormatter{}) // set this for non prod log.SetFormatter(&log.TextFormatter{}) // Output to stderr instead of stdout, could also be a file. log.SetOutput(os.Stderr) // Only log the warning severity or above. log.SetLevel(log.DebugLevel) }
func main() { // Output to stderr instead of stdout, could also be a file. log.SetOutput(os.Stderr) // getting configuration file, err := os.Open("conf.json") if err != nil { log.Panic("Failed to open configuration file, quiting server.") } decoder := json.NewDecoder(file) StuboConfig = Configuration{} err = decoder.Decode(&StuboConfig) if err != nil { log.WithFields(log.Fields{"Error": err.Error()}).Panic("Failed to read configuration") } // configuring logger based on environment settings if StuboConfig.Environment == "production" { // Log as JSON instead of the default ASCII formatter. log.SetFormatter(&log.JSONFormatter{}) // TODO: also, write to file, probably log file path should also be configurable } else { // The TextFormatter is default log.SetFormatter(&log.TextFormatter{}) } if StuboConfig.Debug == true { log.SetLevel(log.DebugLevel) log.Info("Starting server with debug mode initiated...") } // looking for option args when starting App // like ./lgc -port=":3000" would start on port 3000 var port = flag.String("port", ":3000", "Server port") flag.Parse() // parse the flag // assign StuboURI StuboURI = StuboConfig.StuboProtocol + "://" + StuboConfig.StuboHost + ":" + StuboConfig.StuboPort log.WithFields(log.Fields{ "StuboHost": StuboConfig.StuboHost, "StuboPort": StuboConfig.StuboPort, "StuboURI": StuboURI, "ProxyPort": port, }).Info("LGC is starting") client := &Client{&http.Client{}} mux := getRouter(HandlerHTTPClient{*client}) n := negroni.Classic() n.Use(negronilogrus.NewMiddleware()) n.UseHandler(mux) n.Run(*port) }
func Initialize() { log.SetOutput(os.Stderr) log.SetLevel(log.WarnLevel) if boolVerbose { log.SetFormatter(&LogFormatter{}) log.SetLevel(log.InfoLevel) } if boolDebug { log.SetFormatter(&log.TextFormatter{}) log.SetLevel(log.DebugLevel) } }
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 main() { flag.BoolVar(&debug, "debug", false, "") flag.StringVar(&pathToConfig, "c", "", "") flag.Usage = func() { fmt.Println(usage) os.Exit(0) } flag.Parse() if log.IsTerminal() { log.SetFormatter(&log.TextFormatter{ FullTimestamp: true, TimestampFormat: "2006-01-02 15:04:05 MST", }) } else { log.SetFormatter(&log.JSONFormatter{ TimestampFormat: "2006-01-02 15:04:05 MST", }) } config, err := common.ReadConfig(pathToConfig) if err != nil { log.Fatalf("Error reading configuration file: %v", err) } if debug { log.SetLevel(log.DebugLevel) } else { log.SetLevel(config.LogLevel()) } app := app.New(config) if debug { app.SetDebugMode() } app.Run() }
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 initLogFormatter() { log.SetFormatter(&log.TextFormatter{ FullTimestamp: true, ForceColors: true, TimestampFormat: "15:04:05", }) }
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) } } }
//SetEnv parses the command line args and sets the necessary variables func SetEnv() { flag.Parse() if *debug { log.SetLevel(log.DebugLevel) } if *logFile != "" { if output, err := os.OpenFile(*logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err != nil { log.Fatalf("Failed to log to file %s: %v", *logFile, err) } else { log.SetOutput(output) } } textFormatter := &log.TextFormatter{ FullTimestamp: true, } log.SetFormatter(textFormatter) if *catalogURL == "" { err := "Halting Catalog service, Catalog github repo url not provided" log.Fatal(err) _ = fmt.Errorf(err) } }
// Init the test context with the database func Init(t *testing.T) { if initiliazed { return } log.SetLevel(log.DebugLevel) log.SetFormatter(&log.TextFormatter{ ForceColors: true, DisableTimestamp: true, }) flag.StringVar(&dbAddr, "db-addr", "127.0.0.1:27017", "Address of the mongodb server") flag.StringVar(&dbUser, "db-user", "", "User to authenticate with the mongodb server") flag.StringVar(&dbPassword, "db-password", "", "Password to authenticate with the mongodb server") flag.StringVar(&redisAddr, "redis-addr", "127.0.0.1:6379", "Address of the redis server") flag.StringVar(&redisPassword, "redis-password", "", "Password to authenticate with the redis server") flag.Parse() viper.Set("db_addr", dbAddr) viper.Set("db_user", dbUser) viper.Set("db_password", dbPassword) viper.Set("redis_hosts", redisAddr) viper.Set("redis_password", redisPassword) viper.Set("header_trust_username", "X-Remote-User") if err := store.NewStore(); err != nil { t.Errorf("Error initializing test context : %s", err) t.Fail() return } log.Infof("Connected to database %s", dbAddr) initiliazed = true }
func SetEnv() { flag.Parse() if *debug { log.SetLevel(log.DebugLevel) } if *logFile != "" { if output, err := os.OpenFile(*logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err != nil { log.Fatalf("Failed to log to file %s: %v", *logFile, err) } else { log.SetOutput(output) } } textFormatter := &log.TextFormatter{ FullTimestamp: true, } log.SetFormatter(textFormatter) if *catalogUrl == "" { err := "Halting Catalog service, Catalog github repo url not provided" log.Fatal(err) fmt.Errorf(err) } // Shutdown when parent dies if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, syscall.PR_SET_PDEATHSIG, uintptr(syscall.SIGTERM), 0); err != 0 { log.Fatal("Failed to set parent death sinal, err") } }
func init() { log.SetFormatter(&log.TextFormatter{ FullTimestamp: true, TimestampFormat: "15:04:05", }) log.SetOutput(os.Stdout) }
//setLogger initializes logrus logger with some defaults func setLogger() { logrus.SetFormatter(&logrus.TextFormatter{}) logrus.SetOutput(os.Stderr) if gin.Mode() == gin.DebugMode { logrus.SetLevel(logrus.InfoLevel) } }
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 }