mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
7581cf96fb
This fix tries to add an additional syslog-format of `rfc5424micro` which follows the same as rfc5424 except that it use microsecond resolution for timestamp. The purpose is to solve the issue raised in #21793 where log events might lose its ordering if happens on the same second. The timestamp field in rfc5424 is derived from rfc3339, though the maximium resolution is limited to "TIME-SECFRAC" which is 6 (microsecond resolution). The appropriate documentation (`docs/admin/logging/overview.md`) has been updated to reflect the change in this fix. This fix adds a unit test to cover the newly introduced format. This fix fixes #21793. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
51 lines
1.7 KiB
Go
51 lines
1.7 KiB
Go
// +build linux
|
|
|
|
package syslog
|
|
|
|
import (
|
|
syslog "github.com/RackSec/srslog"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func functionMatches(expectedFun interface{}, actualFun interface{}) bool {
|
|
return reflect.ValueOf(expectedFun).Pointer() == reflect.ValueOf(actualFun).Pointer()
|
|
}
|
|
|
|
func TestParseLogFormat(t *testing.T) {
|
|
formatter, framer, err := parseLogFormat("rfc5424")
|
|
if err != nil || !functionMatches(rfc5424formatterWithAppNameAsTag, formatter) ||
|
|
!functionMatches(syslog.RFC5425MessageLengthFramer, framer) {
|
|
t.Fatal("Failed to parse rfc5424 format", err, formatter, framer)
|
|
}
|
|
|
|
formatter, framer, err = parseLogFormat("rfc5424micro")
|
|
if err != nil || !functionMatches(rfc5424microformatterWithAppNameAsTag, formatter) ||
|
|
!functionMatches(syslog.RFC5425MessageLengthFramer, framer) {
|
|
t.Fatal("Failed to parse rfc5424 (microsecond) format", err, formatter, framer)
|
|
}
|
|
|
|
formatter, framer, err = parseLogFormat("rfc3164")
|
|
if err != nil || !functionMatches(syslog.RFC3164Formatter, formatter) ||
|
|
!functionMatches(syslog.DefaultFramer, framer) {
|
|
t.Fatal("Failed to parse rfc3164 format", err, formatter, framer)
|
|
}
|
|
|
|
formatter, framer, err = parseLogFormat("")
|
|
if err != nil || !functionMatches(syslog.UnixFormatter, formatter) ||
|
|
!functionMatches(syslog.DefaultFramer, framer) {
|
|
t.Fatal("Failed to parse empty format", err, formatter, framer)
|
|
}
|
|
|
|
formatter, framer, err = parseLogFormat("invalid")
|
|
if err == nil {
|
|
t.Fatal("Failed to parse invalid format", err, formatter, framer)
|
|
}
|
|
}
|
|
|
|
func TestValidateLogOptEmpty(t *testing.T) {
|
|
emptyConfig := make(map[string]string)
|
|
if err := ValidateLogOpt(emptyConfig); err != nil {
|
|
t.Fatal("Failed to parse empty config", err)
|
|
}
|
|
}
|