Ejemplo n.º 1
0
func Test_Logger_NewWithDevNull(t *testing.T) {
	logger, err := New("null")
	assert.Nil(t, err)
	assert.IsType(t, (*os.File)(nil), logger.out)

	logger, err = New("nil")
	assert.Nil(t, err)
	assert.IsType(t, (*os.File)(nil), logger.out)
}
Ejemplo n.º 2
0
func Test_Logger_Output(t *testing.T) {
	// mock os.Stdout
	stdout := os.Stdout
	r, w, _ := os.Pipe()
	os.Stdout = w

	s := "output testing"
	expected := s + "\n"
	testCases := map[Level]string{
		Ldebug: "[DEBUG]",
		Linfo:  "[INFO]",
		Lwarn:  "[WARN]",
		Lerror: "[ERROR]",
		Lfatal: "[FATAL]",
		Lpanic: "[PANIC]",
		Ltrace: "[Stack]",
	}

	logger, _ := New("stdout")
	logger.SetSkip(1)

	for level, tag := range testCases {
		logger.Output(level, s)

		buf := make([]byte, 1024)
		n, err := r.Read(buf)
		assert.Nil(t, err)
		assert.Contains(t, string(buf[:n]), tag)
		assert.Contains(t, string(buf[:n]), expected)
	}

	os.Stdout = stdout
}
Ejemplo n.º 3
0
func Test_Logger_AddTags(t *testing.T) {
	// mock os.Stdout
	stdout := os.Stdout
	r, w, _ := os.Pipe()
	os.Stdout = w

	tags := []string{"testing", "logger"}
	newTags := []string{"new_testing", "logger"}
	s := "output testing"
	expected := "[DEBUG, testing, logger, new_testing]"

	logger, _ := New("stdout")
	logger.SetSkip(1)
	logger.SetTags(tags...)
	logger.AddTags(newTags...)

	logger.Debug(s)

	buf := make([]byte, 1024)
	n, err := r.Read(buf)
	assert.Nil(t, err)
	assert.Contains(t, string(buf[:n]), expected)

	os.Stdout = stdout
}
Ejemplo n.º 4
0
func Test_Logger_NewWithFile(t *testing.T) {
	filename := "logger-testing"

	logger, err := New(filename)
	assert.Nil(t, err)
	assert.IsType(t, (*os.File)(nil), logger.out)

	os.Remove(filename)
}
Ejemplo n.º 5
0
func Test_Logger_NewWithStderr(t *testing.T) {
	logger, err := New("stderr")
	assert.Nil(t, err)
	assert.Equal(t, os.Stderr, logger.out)
}
Ejemplo n.º 6
0
func Test_Logger_New(t *testing.T) {
	logger, err := New("nil")
	assert.Nil(t, err)
	assert.Implements(t, (*io.Writer)(nil), logger)
}