diff --git a/client/driver/syslog/parser.go b/client/driver/syslog/parser.go index dae46f5d9..f7159e3d3 100644 --- a/client/driver/syslog/parser.go +++ b/client/driver/syslog/parser.go @@ -11,6 +11,7 @@ import ( "github.com/jeromer/syslogparser" ) +// Errors related to parsing priority var ( ErrPriorityNoStart = fmt.Errorf("No start char found for priority") ErrPriorityEmpty = fmt.Errorf("Priority field empty") @@ -20,40 +21,52 @@ var ( ErrPriorityNonDigit = fmt.Errorf("Non digit found in priority") ) +// Priority header and ending characters const ( PRI_PART_START = '<' PRI_PART_END = '>' ) +// Priority holds all the priority bits in a syslog log line type Priority struct { P syslog.Priority F syslog.Priority S syslog.Priority } +// DockerLogParser parses a line of log message that the docker daemon ships type DockerLogParser struct { - line []byte + line []byte + content []byte + severity Priority log *log.Logger } +// NewDockerLogParser creates a new DockerLogParser func NewDockerLogParser(line []byte) *DockerLogParser { return &DockerLogParser{line: line} } +// Parse parses a syslog log line func (d *DockerLogParser) Parse() error { + severity, _, _ := d.parsePriority(d.line) + msgIdx := d.logContentIndex(d.line) + d.severity = severity + d.content = d.line[msgIdx:] return nil } +// Dump creates a map of the parsed log line and severity func (d *DockerLogParser) Dump() syslogparser.LogParts { - severity, _, _ := d.parsePriority(d.line) - msgIdx := d.logContentIndex(d.line) return map[string]interface{}{ - "content": d.line[msgIdx:], - "severity": severity.S, + "content": d.content, + "severity": d.severity, } } +// logContentIndex finds out the index of the start index of the content in a +// syslog line func (d *DockerLogParser) logContentIndex(line []byte) int { cursor := 0 numSpace := 0 @@ -75,6 +88,7 @@ func (d *DockerLogParser) logContentIndex(line []byte) int { return cursor + 1 } +// parsePriority parses the priority in a syslog message func (d *DockerLogParser) parsePriority(line []byte) (Priority, int, error) { cursor := 0 pri := d.newPriority(0) @@ -112,10 +126,12 @@ func (d *DockerLogParser) parsePriority(line []byte) (Priority, int, error) { return pri, cursor, ErrPriorityNoEnd } +// isDigit checks if a byte is a numeric char func (d *DockerLogParser) isDigit(c byte) bool { return c >= '0' && c <= '9' } +// newPriority creates a new default priority func (d *DockerLogParser) newPriority(p int) Priority { // The Priority value is calculated by first multiplying the Facility // number by 8 and then adding the numerical value of the Severity. @@ -129,6 +145,7 @@ func (d *DockerLogParser) newPriority(p int) Priority { func (d *DockerLogParser) Location(location *time.Location) { } +// CustomParser is a parser to parse docker syslog lines type CustomParser struct { logger *log.Logger }