Beispiel #1
0
func (s *ManagerTestSuite) TestSend() {
	m := gol.NewEmergency("field", "value")

	// l1 will not filter the message
	mf1 := &mfmock.LogFilter{}
	mf1.On("Filter", m).Return(false)
	mfmt1 := &mfmock.LogFormatter{}
	mfmt1.On("Format", m).Return("EMERGENCY field=value", nil)
	mw1 := &mfmock.Writer{}
	mw1.On("Write", mock.Anything).Return(21, nil)
	l1 := logger_simple.New(mf1, mfmt1, mw1)

	// l2 will filter the message
	mf2 := &mfmock.LogFilter{}
	mf2.On("Filter", m).Return(true)
	mfmt2 := &mfmock.LogFormatter{}
	mw2 := &mfmock.Writer{}
	l2 := logger_simple.New(mf2, mfmt2, mw2)

	s.manager.Register("l1", l1)
	s.manager.Register("l2", l2)

	s.manager.Run()
	assert.Nil(s.T(), s.manager.Send(m))
	time.Sleep(1 * time.Second)
	s.manager.Close()

	mf1.AssertExpectations(s.T())
	mfmt1.AssertExpectations(s.T())
	mw1.AssertExpectations(s.T())

	mf2.AssertExpectations(s.T())
	mfmt2.AssertExpectations(s.T())
	mw2.AssertExpectations(s.T())
}
Beispiel #2
0
func (s *LoggerTestSuite) TestSendFormatterError() {
	msg := gol.NewEmergency("message", "unknown")
	m := &golmock.LogFormatter{}
	m.On("Format", msg).Return("", fmt.Errorf("internal error"))

	s.logger.SetFormatter(m)
	assert.Error(s.T(), s.logger.Send(msg))

	m.AssertExpectations(s.T())
}
Beispiel #3
0
func main() {
	log.Send(&gol.LogMessage{"hello": "world!"})
	log.Send(gol.NewError("error", "info"))
	log.Send(gol.NewDebug("debug", "info"))

	errorLog.Send(&gol.LogMessage{"hello": "world!"})
	errorLog.Send(gol.NewEmergency("emergency", "errorLog"))
	errorLog.Send(gol.NewError("error", "errorLog"))
	errorLog.Send(gol.NewWarning("error", "errorLog"))
	errorLog.Send(gol.NewInfo("info", "errorLog"))
	errorLog.Send(gol.NewDebug("debug", "errorLog"))
}
Beispiel #4
0
func main() {
	// this will be written to stderr
	logger.Send(gol.NewEmergency("message", "system is down"))
	logger.Send(gol.NewAlert("message", "failed to write to disk"))
	logger.Send(gol.NewCritical("message", "high server load"))
	logger.Send(gol.NewError("message", "invalid number format"))

	// this will not be written anywhere
	logger.Send(gol.NewWarning("message", "performance close to 1s threshold"))
	logger.Send(gol.NewNotice("message", "failed to communicate with monitoring service"))
	logger.Send(gol.NewInfo("message", "requested processed in 250ms"))
	logger.Send(gol.NewDebug("debug", "var x = 10"))
}
Beispiel #5
0
func (s *FilterTestSuite) TestFilter() {
	f := filter.New(field.Type(field.Emergency))
	cases := []gol.NewLogMessageFunc{
		gol.NewAlert, gol.NewCritical, gol.NewError, gol.NewWarning, gol.NewNotice, gol.NewInfo, gol.NewDebug,
	}

	assert.False(s.T(), f.Filter(gol.NewEmergency()))
	for _, newFunc := range cases {
		assert.True(s.T(), f.Filter(newFunc()))
	}

	f = filter.New(field.Type(field.Debug))
	cases = []gol.NewLogMessageFunc{
		gol.NewEmergency, gol.NewAlert, gol.NewCritical, gol.NewError, gol.NewWarning, gol.NewNotice, gol.NewInfo,
	}

	for _, newFunc := range cases {
		assert.False(s.T(), f.Filter(newFunc()))
	}
	assert.False(s.T(), f.Filter(gol.NewDebug()))
}
Beispiel #6
0
func (s *LoggerTestSuite) TestSendNilFormatter() {
	// reset logger built by SetupTest
	s.logger = golsys.New("udp", ":10001", syslog.LOG_INFO, "test", nil)
	assert.Error(s.T(), s.logger.Send(gol.NewEmergency("message", "unknown")))
}
Beispiel #7
0
func (s *ManagerTestSuite) TestSendWithoutRun() {
	m := gol.NewEmergency("field", "value")

	assert.Equal(s.T(), s.manager.Send(m), fmt.Errorf("manager.simple.LogManager is not running"))
}