// 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 main() { log := logrus.New() hook := logrusly.NewLogglyHook(logglyToken, "https://logs-01.loggly.com/bulk/", logrus.WarnLevel, "go", "logrus") log.Hooks.Add(hook) log.WithFields(logrus.Fields{ "name": "Slartibartfast", "age": 42, }).Error("Hello Dent, Arthur Dent!") // Flush is automatic for panic/fatal // Just make sure to Flush() before exiting or you may lose up to 5 seconds // worth of messages. hook.Flush() }
// InitLoggly will create a loggly hook and register it. It will also register // a lifecycle shutdown callback to flush any remaining logs, if you're using // the lifecycle package. func InitLoggly(key, applicationID string, tags ...string) { // No-op if no key is set. if key == "" { return } hook := logrusly.NewLogglyHook( key, applicationID, logrus.InfoLevel, tags..., ) Logger.Hooks.Add(hook) lifecycle.RegisterShutdownCallback("loggly", func() error { hook.Flush() return nil }) }
// loadConfiguration loads the configuration of application func loadConfiguration(app *AppConfig, mgo *MgoConfig, etcd *EtcdConfig) { err := envconfig.Process("core", app) if err != nil { log.Panicln(err) } err = envconfig.Process("mongodb", mgo) if err != nil { log.Panicln(err) } err = envconfig.Process("etcd", etcd) if err != nil { log.Panicln(err) } if len(os.Getenv(KeyLogly)) > 0 { hook := logrusly.NewLogglyHook(os.Getenv(KeyLogly), app.Host, logrus.InfoLevel, app.Name) logrus.AddHook(hook) } }
// loadConfiguration loads the configuration of application func loadConfiguration(app *AppConfig, mgo *MgoConfig, nats *NatsConfig) { err := envconfig.Process(ServiceName, app) if err != nil { log.Panicln(err) } err = envconfig.Process("mongodb", mgo) if err != nil { log.Panicln(err) } err = envconfig.Process("nats", nats) if err != nil { log.Panicln(err) } if len(os.Getenv(KeyLogly)) > 0 { log.Println("Loading logly token %s", os.Getenv(KeyLogly)) hook := logrusly.NewLogglyHook(os.Getenv(KeyLogly), app.Host, log.InfoLevel, app.Name) log.AddHook(hook) } }
// New creates a Logger // Log levels Warn, Error, and Fatal are always logged. // If debug, all calls will also log caller informaton. // // The default format of logs will be JSON. Also supports 'text' which is // a easier format for people to understand if you are logging to stdout. func New(settings Settings) (*Log, error) { // Setting up Log. log := &Log{} tracer = NilTracer{} // Let's fire up a new logrus text := logrus.New() text.Level = logrus.DebugLevel // Default is text, color me pretty if possible. text.Formatter = &logrus.TextFormatter{ForceColors: true} var level logrus.Level var err error var overrideDefaultLevel bool switch v := settings.Output.(type) { case LogglySettings: // Validate that the bare minimum of what is needed is present. if v.Token == "" && v.Domain == "" { return &Log{}, ErrLogLogglySetup } if v.Level != "" { level, err = logrus.ParseLevel(v.Level) if err != nil { return log, ErrLogInvalidLevel } overrideDefaultLevel = true } var token, domain string // Change the formatter to JSON, required here. text.Formatter = &logrus.JSONFormatter{} // initialize LogglyHook token = v.Token domain = v.Domain // Create new hook hook := logrusly.NewLogglyHook(token, domain, level) // Add hook tags for _, tag := range v.Tags { hook.Tag(tag) } // Finally, add the hook to the logrus text.Hooks.Add(hook) // Let's add information to the main log. log.hook = hook log.isLoggly = true case Stderr: text.Out = os.Stderr if v.Format == "json" { text.Formatter = &logrus.JSONFormatter{} } if v.Level != "" { level, err = logrus.ParseLevel(v.Level) if err != nil { return log, ErrLogInvalidLevel } overrideDefaultLevel = true } case Stdout: text.Out = os.Stdout if settings.Trace { tracer = StdOutTracer{} } if v.Format == "json" { text.Formatter = &logrus.JSONFormatter{} } if v.Level != "" { level, err = logrus.ParseLevel(v.Level) if err != nil { return log, ErrLogInvalidLevel } overrideDefaultLevel = true } case Disk: if v.Path == "" { return log, ErrLogInvalidPath } f, err := os.OpenFile(v.Path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { return log, ErrLogInvalidPath } text.Out = f text.Formatter = &logrus.JSONFormatter{} if v.Level != "" { level, err = logrus.ParseLevel(v.Level) if err != nil { return log, err } overrideDefaultLevel = true } case Stdiscard: text.Out = ioutil.Discard default: // We should assume text unless overriden otherwise. return log, ErrLogInvalidType } // All the error checking for level has been handled up above. if overrideDefaultLevel { text.Level = level } // Log debug / info evaluation if text.Level == logrus.DebugLevel { log.debug = true log.info = true } if text.Level == logrus.InfoLevel { log.debug = false log.info = true } // Let's attach text to the log. log.text = text return log, nil }