Example #1
0
func TestMiddlewareIntegration(t *testing.T) {
	generalLogContainer := &bytes.Buffer{}
	logContainerForUser107 := &bytes.Buffer{}

	rlog := reactLog.New(generalLogContainer)
	rlog.AddReaction("user ID 107", &reactLog.Redirect{logContainerForUser107})

	opts, err := options.New("base", map[string]interface{}{
		"formatter": "text",
		"level":     logrus.FatalLevel,
	})

	Convey("Middleware logging options should be successfully created without any errors", t, func() {
		So(err, ShouldBeNil)
	})

	log := New(opts)

	log.SetOutput(rlog)

	log.Info("This is normal log")
	log.Info("This is log that concers user ID 107 with important data")

	Convey("Test logger middleware redirect", t, func() {
		So(generalLogContainer.String(), ShouldContainSubstring, "This is normal log")
	})

	Convey("Test logger middleware redirect", t, func() {
		So(logContainerForUser107.String(), ShouldContainSubstring, "This is log that concers user ID 107 with important data")
	})
}
Example #2
0
func ExampleDiscard() {
	reactLogger := reactLog.New(os.Stdout) // use os.Stderr for default log functionality
	reactLogger.AddReaction("INFO", &reactLog.Discard{})

	logger := log.New(reactLogger, "", 0)
	logger.Println("INFO this will NOT be written")
	logger.Println("ERROR this will be written")
	// Output: ERROR this will be written
}
Example #3
0
func ExampleCopy() {
	logContainerForUser107 := &bytes.Buffer{} // it can be any io.Writer eq. file

	reactLogger := reactLog.New(os.Stdout) // use os.Stderr for default log functionality
	reactLogger.AddReaction("user ID 107", &reactLog.Copy{logContainerForUser107})

	logger := log.New(reactLogger, "", 0)

	logger.Println("Log concerning user ID 107 with extra data")
	logger.Println("INFO dummy log")

	fmt.Println(logContainerForUser107) // in logContainerForUser107 is copy of log just for USER_107
	// Output:
	// Log concerning user ID 107 with extra data
	// INFO dummy log
	// Log concerning user ID 107 with extra data
}
Example #4
0
func ExampleRedirect() {
	logContainerForUser107 := &bytes.Buffer{} // it can be any io.Writer eq. file

	reactLogger := reactLog.New(ioutil.Discard) // use os.Stderr for default log functionality
	reactLogger.AddReaction("user ID 107", &reactLog.Redirect{logContainerForUser107})

	logger := log.New(reactLogger, "", 0)

	logger.Println("INFO dummy log 1")
	logger.Println("ERROR dummy log 2")

	logger.Println("Log concerning user ID 107 with extra data")

	logger.Println("INFO dummy log 3")
	logger.Println("ERROR dummy log 4")

	fmt.Println(logContainerForUser107)
	// Output: Log concerning user ID 107 with extra data
}