rfc5425

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2018 License: MIT Imports: 6 Imported by: 1

Documentation

Overview

Example
r := strings.NewReader("48 <1>1 2003-10-11T22:14:15.003Z host.local - - - -25 <3>1 - host.local - - - -38 <2>1 - host.local su - - - κόσμε")
output(NewParser(r, WithBestEffort()).Parse())
Output:

([]rfc5425.Result) (len=3) {
 (rfc5425.Result) {
  Message: (*rfc5424.SyslogMessage)({
   priority: (*uint8)(1),
   facility: (*uint8)(0),
   severity: (*uint8)(1),
   version: (uint16) 1,
   timestamp: (*time.Time)(2003-10-11 22:14:15.003 +0000 UTC),
   hostname: (*string)((len=10) "host.local"),
   appname: (*string)(<nil>),
   procID: (*string)(<nil>),
   msgID: (*string)(<nil>),
   structuredData: (*map[string]map[string]string)(<nil>),
   message: (*string)(<nil>)
  }),
  MessageError: (error) <nil>,
  Error: (error) <nil>
 },
 (rfc5425.Result) {
  Message: (*rfc5424.SyslogMessage)({
   priority: (*uint8)(3),
   facility: (*uint8)(0),
   severity: (*uint8)(3),
   version: (uint16) 1,
   timestamp: (*time.Time)(<nil>),
   hostname: (*string)((len=10) "host.local"),
   appname: (*string)(<nil>),
   procID: (*string)(<nil>),
   msgID: (*string)(<nil>),
   structuredData: (*map[string]map[string]string)(<nil>),
   message: (*string)(<nil>)
  }),
  MessageError: (error) <nil>,
  Error: (error) <nil>
 },
 (rfc5425.Result) {
  Message: (*rfc5424.SyslogMessage)({
   priority: (*uint8)(2),
   facility: (*uint8)(0),
   severity: (*uint8)(2),
   version: (uint16) 1,
   timestamp: (*time.Time)(<nil>),
   hostname: (*string)((len=10) "host.local"),
   appname: (*string)((len=2) "su"),
   procID: (*string)(<nil>),
   msgID: (*string)(<nil>),
   structuredData: (*map[string]map[string]string)(<nil>),
   message: (*string)((len=11) "κόσμε")
  }),
  MessageError: (error) <nil>,
  Error: (error) <nil>
 }
}
Example (Handler)
// Defining a custom handler to send the parsing results into a channel
EmittingParsing := func(p *Parser) chan Result {
	c := make(chan Result)

	toChannel := func(r *Result) {
		c <- *r
	}

	go func() {
		defer close(c)
		p.ParseExecuting(toChannel)
	}()

	return c
}

// Use it
r := strings.NewReader("16 <1>1 - - - - - -17 <2>12 A B C D E -16 <1>1")
results := EmittingParsing(NewParser(r, WithBestEffort()))

for r := range results {
	output(r)
}
Output:

(rfc5425.Result) {
 Message: (*rfc5424.SyslogMessage)({
  priority: (*uint8)(1),
  facility: (*uint8)(0),
  severity: (*uint8)(1),
  version: (uint16) 1,
  timestamp: (*time.Time)(<nil>),
  hostname: (*string)(<nil>),
  appname: (*string)(<nil>),
  procID: (*string)(<nil>),
  msgID: (*string)(<nil>),
  structuredData: (*map[string]map[string]string)(<nil>),
  message: (*string)(<nil>)
 }),
 MessageError: (error) <nil>,
 Error: (error) <nil>
}
(rfc5425.Result) {
 Message: (*rfc5424.SyslogMessage)({
  priority: (*uint8)(2),
  facility: (*uint8)(0),
  severity: (*uint8)(2),
  version: (uint16) 12,
  timestamp: (*time.Time)(<nil>),
  hostname: (*string)(<nil>),
  appname: (*string)(<nil>),
  procID: (*string)(<nil>),
  msgID: (*string)(<nil>),
  structuredData: (*map[string]map[string]string)(<nil>),
  message: (*string)(<nil>)
 }),
 MessageError: (*errors.errorString)(expecting a RFC3339MICRO timestamp or a nil value [col 6]),
 Error: (error) <nil>
}
(rfc5425.Result) {
 Message: (*rfc5424.SyslogMessage)({
  priority: (*uint8)(1),
  facility: (*uint8)(0),
  severity: (*uint8)(1),
  version: (uint16) 1,
  timestamp: (*time.Time)(<nil>),
  hostname: (*string)(<nil>),
  appname: (*string)(<nil>),
  procID: (*string)(<nil>),
  msgID: (*string)(<nil>),
  structuredData: (*map[string]map[string]string)(<nil>),
  message: (*string)(<nil>)
 }),
 MessageError: (*errors.errorString)(parsing error [col 4]),
 Error: (*errors.errorString)(found EOF after "<1>1", expecting a SYSLOGMSG containing 16 octets)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser is capable to parse byte buffer on the basis of RFC5425.

Use NewParser function to instantiate one.

func NewParser

func NewParser(r io.Reader, opts ...ParserOpt) *Parser

NewParser returns a pointer to a new instance of Parser.

func (*Parser) Parse

func (p *Parser) Parse() []Result

Parse parses the incoming bytes accumulating the results.

func (*Parser) ParseExecuting

func (p *Parser) ParseExecuting(handler ResultHandler)

ParseExecuting parses the incoming bytes executing the handler function for each Result.

It stops parsing when an error regarding RFC 5425 is found.

type ParserOpt

type ParserOpt func(p *Parser) *Parser

ParserOpt represents the type of options setters.

func WithBestEffort

func WithBestEffort() ParserOpt

WithBestEffort sets the best effort mode on.

When active the parser tries to recover as much of the syslog messages as possible.

type Result

type Result struct {
	Message      *rfc5424.SyslogMessage
	MessageError error
	Error        error
}

Result represent the resulting syslog message and (eventually) errors occured during parsing.

type ResultHandler

type ResultHandler func(result *Result)

ResultHandler is a function the user can use to specify what to do with every Result instance.

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

Scanner represents the lexical scanner for RFC5425.

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner returns a pointer to a new instance of Scanner.

func (*Scanner) Scan

func (s *Scanner) Scan() (tok Token)

Scan returns the next token.

type Token

type Token struct {
	// contains filtered or unexported fields
}

Token represents a lexical token of the RFC5425.

func (Token) String

func (t Token) String() string

String outputs the string representation of the receiving Token.

type TokenType

type TokenType int

TokenType represents a lexical token type of the RFC5425.

const (
	ILLEGAL TokenType = iota
	EOF
	WS
	MSGLEN
	SYSLOGMSG
)

Tokens

func (TokenType) String

func (i TokenType) String() string

String outputs the string representation of the receiving TokenType.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL