Example #1
0
func TestGinLogger(t *testing.T) {
	Convey("Given a buffered logrus", t, func() {
		log := logrus.New()
		buf := &bytes.Buffer{}
		log.Out = buf

		Convey("And a configured HTTP server", func() {
			gin.SetMode(gin.ReleaseMode)
			router := gin.New()

			router.Use(utils.GinLogger("API", log))

			router.GET("/", func(c *gin.Context) {
				c.JSON(200, "hello")
			})

			ts := httptest.NewServer(router)

			Convey("A message should be logged to the buffer upon a request", func() {
				resp, err := http.Get(ts.URL + "/")
				So(err, ShouldBeNil)

				body, err := ioutil.ReadAll(resp.Body)
				So(err, ShouldBeNil)
				So(string(body), ShouldEqual, "\"hello\"\n")

				So(buf.String(), ShouldContainSubstring, "API request finished")
				So(buf.String(), ShouldContainSubstring, "method=GET path=\"/\" status=200")
			})
		})
	})
}
Example #2
0
func TestNSQLogger(t *testing.T) {
	Convey("Given a buffered logrus", t, func() {
		log := logrus.New()
		buf := &bytes.Buffer{}
		log.Out = buf

		Convey("NSQLogger should print proper messages", func() {
			nsql := &utils.NSQLogger{
				Log: log,
			}

			So(nsql.Output(1, "Hello world"), ShouldBeNil)
			So(buf.String(), ShouldContainSubstring, "nsq_logger_test.go:24")
		})
	})
}
Example #3
0
File: api.go Project: pgpst/pgpst
func NewAPI(options *Options) *API {
	// Create a new logger
	log := logrus.New()
	log.Level = options.LogLevel

	// Connect to the database
	session, err := r.Connect(r.ConnectOpts{
		Address:  options.RethinkDBAddress,
		Database: options.RethinkDBDatabase,
	})
	if err != nil {
		log.WithField("err", err).Fatal("Unable to connect to RethinkDB")
	}

	// Create a new NSQ producer
	producer, err := nsq.NewProducer(options.NSQdAddress, nsq.NewConfig())
	if err != nil {
		log.WithField("err", err).Fatal("Unable to connect to NSQd")
	}
	producer.SetLogger(&utils.NSQLogger{
		Log: log,
	}, nsq.LogLevelWarning)

	// Connect to Raven
	var rc *raven.Client
	if options.RavenDSN != "" {
		rc, err = raven.NewClient(options.RavenDSN, nil)
		if err != nil {
			log.WithField("err", err).Fatal("Unable to connect to Sentry")
		}
	}

	// Return a new API struct
	return &API{
		Options:  options,
		Log:      log,
		Rethink:  session,
		Producer: producer,
		Raven:    rc,
	}
}
Example #4
0
func NewMailer(options *Options) *Mailer {
	// Create a new logger
	log := logrus.New()
	log.Level = options.LogLevel

	// Connect to the database
	session, err := r.Connect(options.RethinkOpts)
	if err != nil {
		log.WithField("err", err).Fatal("Unable to connect to RethinkDB")
	}

	// Create a new NSQ producer
	producer, err := nsq.NewProducer(options.NSQdAddress, nsq.NewConfig())
	if err != nil {
		log.WithField("err", err).Fatal("Unable to connect to NSQd")
	}
	nsqlog := &utils.NSQLogger{
		Log: log,
	}
	producer.SetLogger(nsqlog, nsq.LogLevelWarning)

	// Prepare the struct
	mailer := &Mailer{
		Options:  options,
		Log:      log,
		Rethink:  session,
		Producer: producer,
	}

	// And a new NSQ consumer
	consumer, err := nsq.NewConsumer("send_email", "receive", nsq.NewConfig())
	if err != nil {
		log.WithField("err", err).Fatal("Unable to create a new NSQ consumer")
	}
	consumer.SetLogger(nsqlog, nsq.LogLevelWarning)
	//consumer.AddConcurrentHandlers(mailer, options.SenderConcurrency)
	mailer.Consumer = consumer

	// Connect to spamd
	mailer.Spam = spamc.New(options.SpamdAddress, 10)

	// Connect to Raven
	var rc *raven.Client
	if options.RavenDSN != "" {
		rc, err = raven.NewClient(options.RavenDSN, nil)
		if err != nil {
			log.WithField("err", err).Fatal("Unable to connect to Sentry")
		}
	}
	mailer.Raven = rc

	// Load TLS config
	if options.SMTPTLSCert != "" {
		cert, err := ioutil.ReadFile(options.SMTPTLSCert)
		if err != nil {
			log.WithField("err", err).Fatal("Unable to read tls cert file")
		}

		key, err := ioutil.ReadFile(options.SMTPTLSKey)
		if err != nil {
			log.WithField("err", err).Fatal("Unable to read tls key file")
		}

		pair, err := tls.X509KeyPair(cert, key)
		if err != nil {
			log.WithField("err", err).Fatal("Unable to parse tls keypair")
		}

		mailer.TLSConfig = &tls.Config{
			Certificates: []tls.Certificate{pair},
		}
	}

	// Return a new mailer struct
	return mailer
}