Пример #1
0
func TestStdLoggerFormat(t *testing.T) {

	var buf bytes.Buffer
	var bufInfo bytes.Buffer
	sl := log.NewStdLogger(
		log.StdLevelOption(log.StdLevelDebug),
		log.StdGlobalWriterOption(&buf),
		log.StdInfoOption(&bufInfo, "TEST-INFO ", std.LstdFlags),
	)

	sl.Debug("my Debug", 3.14152)
	sl.Debug("my Debug2", "", 2.14152)
	sl.Debug("my Debug3", "key3", 3105, 4711, "Hello")
	sl.Info("InfoTEST")
	sl.Info("InfoTEST", "keyI", 117, 2009)

	aTestErr := errors.New("Cannot run PHP code")
	haveErr := sl.Error("ErrorTEST", "myErr", aTestErr)

	logs := buf.String()
	logsInfo := bufInfo.String()

	//	t.Log("", logs)
	//	t.Log("", logsInfo)

	assert.EqualError(t, haveErr, aTestErr.Error())
	assert.Contains(t, logs, "Debug2")
	assert.Contains(t, logs, "BAD_KEY_AT_INDEX_0")
	assert.Contains(t, logs, `key3: 3105 BAD_KEY_AT_INDEX_2: "Hello"`)
	assert.Contains(t, logs, "_: 3.14")

	assert.Contains(t, logsInfo, "InfoTEST")
	assert.Contains(t, logsInfo, `FIX_IMBALANCED_PAIRS: []interface {}{"keyI", 117, 2009}`)
}
Пример #2
0
func TestStdLogger(t *testing.T) {

	var buf bytes.Buffer

	sl := log.NewStdLogger(
		log.StdLevelOption(log.StdLevelTrace),
		log.StdTraceOption(&buf, "TEST-TRACE ", std.LstdFlags),
		log.StdDebugOption(&buf, "TEST-DEBUG ", std.LstdFlags),
		log.StdInfoOption(&buf, "TEST-INFO ", std.LstdFlags),
		log.StdWarnOption(&buf, "TEST-WARN ", std.LstdFlags),
		log.StdErrorOption(&buf, "TEST-ERROR ", std.LstdFlags),
		log.StdFatalOption(&buf, "TEST-FATAL ", std.LstdFlags),
	)
	sl.SetLevel(log.StdLevelInfo)
	assert.False(t, sl.IsTrace())
	assert.False(t, sl.IsDebug())
	assert.True(t, sl.IsInfo())
	assert.True(t, sl.IsWarn())

	sl.Trace("my trace1")
	sl.Trace("my trace2", "int", 29)
	sl.Debug("my Debug", "float", 3.14152)
	sl.Debug("my Debug2", 2.14152)
	sl.Info("InfoTEST")
	sl.Warn("WarnTEST")
	haveErr := sl.Error("ErrorTEST", "err1a", 1, "err2", 32.4232)

	logs := buf.String()

	assert.EqualError(t, haveErr, "ErrorTEST")
	assert.Contains(t, logs, "InfoTEST")
	assert.Contains(t, logs, "WarnTEST")
	assert.Contains(t, logs, "ErrorTEST")
	assert.NotContains(t, logs, "trace1")
	assert.NotContains(t, logs, "Debug2")

	buf.Reset()
	sl.SetLevel(log.StdLevelTrace)
	assert.True(t, sl.IsTrace())
	assert.True(t, sl.IsDebug())
	assert.True(t, sl.IsInfo())
	assert.True(t, sl.IsWarn())
	sl.Trace("my trace1")
	sl.Trace("my trace2", "int", 29)
	sl.Debug("my Debug", "float", 3.14152)
	sl.Debug("my Debug2", 2.14152)
	sl.Info("InfoTEST")

	logs = buf.String()

	assert.Contains(t, logs, "InfoTEST")
	assert.Contains(t, logs, "trace1")
	assert.Contains(t, logs, "Debug2")

}
Пример #3
0
func TestStdLoggerNewPanic(t *testing.T) {

	defer func() {
		if r := recover(); r != nil {
			if msg, ok := r.(string); ok {
				assert.EqualValues(t, "Arguments to New() can only be StdOption types!", msg)
			} else {
				t.Error("Expecting a string")
			}
		}
	}()

	var buf bytes.Buffer
	sl := log.NewStdLogger(
		log.StdGlobalWriterOption(&buf),
	)
	sl.New(log.StdLevelOption(log.StdLevelDebug), 1)
}
Пример #4
0
func TestStdLoggerGlobals(t *testing.T) {

	var buf bytes.Buffer
	sl := log.NewStdLogger(
		log.StdLevelOption(log.StdLevelDebug),
		log.StdGlobalWriterOption(&buf),
		log.StdGlobalFlagOption(std.Ldate),
	)
	sl.Trace("my trace1")
	sl.Trace("my trace2", "int", 29)
	sl.Debug("my Debug", "float", 3.14152)
	sl.Debug("my Debug2", 2.14152)
	sl.Info("InfoTEST")

	logs := buf.String()

	assert.NotContains(t, logs, "trace2")
	assert.Contains(t, logs, "InfoTEST")
	assert.NotContains(t, logs, "trace1")
	assert.Contains(t, logs, "Debug2")
}