Exemplo n.º 1
0
// Format formats the log message.
func (f Custom) Format(msg *gol.LogMessage) (string, error) {
	lmsg := msg.FieldLength()
	buffer := make([]string, lmsg, lmsg)

	i := 0
	for k, v := range *msg {
		if k != fields.Severity && k != fields.Timestamp {
			buffer[i] = fmt.Sprintf("%s:'%s'", k, v)
			i += 1
		}
	}

	t, _ := msg.Timestamp()

	if severity, err := msg.Severity(); err != nil {
		return fmt.Sprintf("%s UNKNOWN %s\n", t.String(), strings.Join(buffer, " ")), nil
	} else {
		switch severity >= field_severity.Error {
		case true:
			return fmt.Sprintf("%s %s %s\n", t.String(), color.RedString("%s", severity), strings.Join(buffer, " ")), nil
		default:
			return fmt.Sprintf("%s %s %s\n", t.String(), severity, strings.Join(buffer, " ")), nil
		}
	}
}
Exemplo n.º 2
0
func (s *MessageTestSuite) TestTimestamp() {
	var msg *gol.LogMessage

	msg = &gol.LogMessage{"key": "value"}
	v, err := msg.Timestamp()
	assert.Equal(s.T(), fmt.Errorf("Message does not contain field timestamp"), err)
	assert.Nil(s.T(), v)

	msg = gol.NewInfo("key", "value")
	v, err = msg.Timestamp()
	assert.Nil(s.T(), err)
	assert.True(s.T(), v.Before(time.Now()))
}