Documentation
¶
Index ¶
- Variables
- func CheckForLikelyDateTime(buf []byte) bool
- func ContentOptionParseJSON(opts *contentOpts)
- func ContentOptionRequireTerminator(opts *contentOpts)
- func ContentOptionUseGJSON(opts *contentOpts)
- func OptionDontParseJSON(p *Parser)
- func OptionLocation(location *time.Location) func(*Parser)
- func OptionNoHostname(p *Parser)
- func OptionSanitizeProgram(p *Parser)
- func OptionUseGJSONParser(p *Parser)
- func OptionUseLocalFormat(s *SyslogMsg)
- func OptionUseRemoteFormat(s *SyslogMsg)
- func ParseCEE(buf []byte) (int, string, error)
- func ParseHost(buf []byte) (int, string, error)
- func TagOptionSanitizeProgram(opts *tagOpts)
- type Content
- type Facility
- type Parser
- type Priority
- type Severity
- type SyslogMsg
- func (s *SyslogMsg) AddTag(key string, value interface{})
- func (s *SyslogMsg) AddTagArray(key string, value interface{}) error
- func (s *SyslogMsg) Bytes(options ...SyslogMsgOption) []byte
- func (s *SyslogMsg) JSON() ([]byte, error)
- func (s *SyslogMsg) SetContent(c string) error
- func (s *SyslogMsg) SetFacility(f Facility) error
- func (s *SyslogMsg) SetHost(h string)
- func (s *SyslogMsg) SetPid(p string)
- func (s *SyslogMsg) SetProgram(p string)
- func (s *SyslogMsg) SetSeverity(sv Severity) error
- func (s *SyslogMsg) SetTime(t time.Time)
- func (s *SyslogMsg) String(options ...SyslogMsgOption) string
- type SyslogMsgOption
- type Tag
- type Time
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( //ErrBadTime is returned when the time of a message is malformed. ErrBadTime = errors.New("Time not found") //ErrBadHost is returned when the host of a message is malformed. ErrBadHost = errors.New("Host not found") //ErrBadTag is returned when the tag of a message is malformed. ErrBadTag = errors.New("Tag not found") //ErrBadContent is returned when the content of a message is malformed. ErrBadContent = errors.New("Content not found") )
var ( //ErrBadPriority is returned when the priority of a message is malformed. ErrBadPriority = errors.New("Priority not found") //ErrBadFacility is returned when a facility is not within allowed values. ErrBadFacility = errors.New("Facility not found") //ErrBadSeverity is returned when a severity is not within allowed values. ErrBadSeverity = errors.New("Severity not found") )
Functions ¶
func CheckForLikelyDateTime ¶ added in v0.1.14
CheckForLikelyDateTime checks for a YYYY-MM-DD string. If one is found, we use this to decide that trying to parse a full rsyslog style timestamp is worth the cpu time.
func ContentOptionParseJSON ¶ added in v0.1.14
func ContentOptionParseJSON(opts *contentOpts)
ContentOptionParseJSON will treat the content as a CEE message
func ContentOptionRequireTerminator ¶ added in v0.1.14
func ContentOptionRequireTerminator(opts *contentOpts)
ContentOptionRequireTerminator sets ParseContent to require a \n terminator
func ContentOptionUseGJSON ¶ added in v0.1.14
func ContentOptionUseGJSON(opts *contentOpts)
ContentOptionUseGJSON will use the "github.com/tidwall/gjson" JSON parser, if ContentOptionParseJSON is specified
func OptionDontParseJSON ¶
func OptionDontParseJSON(p *Parser)
OptionDontParseJSON sets the parser to not parse JSON in the content field of the message. A subsequent call to SyslogMsg.String() or SyslogMsg.Bytes() will then use SyslogMsg.Content for the content field, unless SyslogMsg.JSONValues have been added since the message was originally parsed. If SyslogMsg.JSONValues have been added, the call to SyslogMsg.String() or SyslogMsg.Bytes() will then parse the JSON, and merge the results with the keys in SyslogMsg.JSONVaues.
func OptionLocation ¶ added in v0.1.14
OptionLocation is a helper function to configure the parser to parse time in the given timezone, If the parsed time contains a valid timezone identifier this takes precedence. Default timezone is UTC.
func OptionNoHostname ¶
func OptionNoHostname(p *Parser)
OptionNoHostname sets the parser to not expect the hostname as part of the syslog message, and instead ask the host for its hostname.
func OptionSanitizeProgram ¶ added in v0.1.14
func OptionSanitizeProgram(p *Parser)
OptionSanitizeProgram sets the parser to sanitize the syslog program name if needed. Useful for programs such as /usr/bin/someprogram.
func OptionUseGJSONParser ¶ added in v0.1.14
func OptionUseGJSONParser(p *Parser)
OptionUseGJSONParser uses an alternate parser for CEE JSON content:
https://github.com/tidwall/gjson
Particularly for logs with significant numbers of JSON fields, this is expected to yield performance gains. This setting has no effect when used with OptionDontParseJSON.
func OptionUseLocalFormat ¶ added in v0.1.14
func OptionUseLocalFormat(s *SyslogMsg)
OptionUseLocalFormat tells SyslogMsg.String() and SyslogMsg.Byte() to format the message to be compatible with writing to /dev/log rather than over the wire.
func OptionUseRemoteFormat ¶ added in v0.1.14
func OptionUseRemoteFormat(s *SyslogMsg)
OptionUseRemoteFormat tells SyslogMsg.String() and SyslogMsg.Byte() to use wire format for the message instead of local format
func ParseCEE ¶ added in v0.1.14
ParseCEE will try to find a syslog cee cookie at the beginning of the passed in []byte. It returns the offset from the start of the []byte to the end of the cee string, the string, and an error.
func ParseHost ¶ added in v0.1.14
ParseHost will try to find a host at the beginning of the passed in []byte. It will return the offset from the start of the []byte to the end of the host string, a captainslog.Priority, and an error.
func TagOptionSanitizeProgram ¶ added in v0.1.14
func TagOptionSanitizeProgram(opts *tagOpts)
TagOptionSanitizeProgram sets the tag options to sanitize the program name
Types ¶
type Content ¶ added in v0.1.14
Content holds the Content of a syslog message, including the Content as a string, and a struct of the JSONValues of appropriate.
func ParseContent ¶ added in v0.1.14
ParseContent will try to find syslog content at the beginning of the passed in []byte. It returns the offset from the start of the []byte to the end of the content, a captainslog.Content, and an error. It accepts two options:
ContentOptionRequireTerminator: if true, if the syslog message does not
contain a '\n' terminator it will be treated as invalid.
ContentOptionParseJSON: if true, it will treat the content field of the
syslog message as a CEE message and parse the JSON.
type Facility ¶
type Facility int
Facility represents a syslog facility code
const ( //Kern is the kernel rfc3164 facility. Kern Facility = 0 //User is the user rfc3164 facility. User Facility = 1 // Mail is the mail rfc3164 facility. Mail Facility = 2 // Daemon is the daemon rfc3164 facility. Daemon Facility = 3 // Auth is the auth rfc3164 facility. Auth Facility = 4 // Syslog is the syslog rfc3164 facility. Syslog Facility = 5 // LPR is the printer rfc3164 facility. LPR Facility = 6 // News is a news rfc3164 facility. News Facility = 7 // UUCP is the UUCP rfc3164 facility. UUCP Facility = 8 // Cron is the cron rfc3164 facility. Cron Facility = 9 //AuthPriv is the authpriv rfc3164 facility. AuthPriv Facility = 10 // FTP is the ftp rfc3164 facility. FTP Facility = 11 // Local0 is the local0 rfc3164 facility. Local0 Facility = 16 // Local1 is the local1 rfc3164 facility. Local1 Facility = 17 // Local2 is the local2 rfc3164 facility. Local2 Facility = 18 // Local3 is the local3 rfc3164 facility. Local3 Facility = 19 // Local4 is the local4 rfc3164 facility. Local4 Facility = 20 // Local5 is the local5 rfc3164 facility. Local5 Facility = 21 // Local6 is the local6 rfc3164 facility. Local6 Facility = 22 // Local7 is the local7 rfc3164 facility. Local7 Facility = 23 )
func (*Facility) FromString ¶ added in v0.1.14
FromString sets the Facility from a string representation.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is a parser for syslog messages.
type Priority ¶
Priority represents the PRI of a rfc3164 message.
func NewPriority ¶
NewPriority calculates a Priority from a Facility and Severity.
func ParsePri ¶ added in v0.1.14
ParsePri will try to find a syslog priority at the beginning of the passed in []byte. It will return the offset from the start of the []byte to the end of the priority string, a captainslog.Priority, and an error.
func (*Priority) SetFacility ¶ added in v0.1.14
SetFacility sets the Facility component of the Priority.
func (*Priority) SetSeverity ¶ added in v0.1.14
SetSeverity sets the Severity component of the Priority.
type Severity ¶
type Severity int
Severity represents a syslog severity code
const ( // Emerg is an emergency rfc3164 severity Emerg Severity = 0 // Alert is an alert rfc3164 severity Alert Severity = 1 // Crit is a critical level rfc3164 severity Crit Severity = 2 // Err is an error level rfc3164 severity Err Severity = 3 // Warning is a warning level rfc3164 severity Warning Severity = 4 // Notice is a notice level rfc3164 severity Notice Severity = 5 // Info is an info level rfc3164 severity Info Severity = 6 // Debug is a debug level rfc3164 severity Debug Severity = 7 )
func (*Severity) FromString ¶ added in v0.1.14
FromString loads a syslog severity from a string representation
type SyslogMsg ¶
type SyslogMsg struct { Pri Priority Time time.Time Host string Tag Tag Cee string IsJSON bool IsCee bool Content string JSONValues map[string]interface{} // contains filtered or unexported fields }
SyslogMsg holds an Unmarshaled rfc3164 message.
func NewSyslogMsg ¶
func NewSyslogMsg(options ...SyslogMsgOption) SyslogMsg
NewSyslogMsg creates a new empty SyslogMsg.
func NewSyslogMsgFromBytes ¶
NewSyslogMsgFromBytes accepts a []byte containing an RFC3164 message and returns a SyslogMsg. If the original RFC3164 message is a CEE enhanced message, the JSON will be parsed into the JSONValues map[string]inferface{}
Example ¶
package main import ( "fmt" "github.com/digitalocean/captainslog" ) func main() { msg, err := captainslog.NewSyslogMsgFromBytes([]byte("<191>2006-01-02T15:04:05.999999-07:00 host.example.org test: engage\n")) if err != nil { panic(err) } fmt.Printf("Syslog message was from host %q", msg.Host) }
Output: Syslog message was from host "host.example.org"
func (*SyslogMsg) AddTag ¶
AddTag adds a tag to the value at key. If the key exists, the value currently at the key will be overwritten.
func (*SyslogMsg) AddTagArray ¶
AddTagArray adds a tag to an array of tags at the key. If the key does not already exist, it will create the key and initially it to a []interface{}.
func (*SyslogMsg) Bytes ¶
func (s *SyslogMsg) Bytes(options ...SyslogMsgOption) []byte
Bytes returns the SyslogMsg as RFC3164 []byte.
func (*SyslogMsg) JSON ¶
JSON returns a JSON representation of the message encoded in a []byte. Syslog fields are named with a "syslog_" prefix to avoid potential collision with fields from the message body.
func (*SyslogMsg) SetContent ¶ added in v0.1.14
SetContent accepts a string to set the content of the SyslogMsg. It will check to see if the string is JSON and try to parse it if so.
func (*SyslogMsg) SetFacility ¶ added in v0.1.14
SetFacility accepts a captainslog.Facility to set the facility of the SyslogMsg
func (*SyslogMsg) SetHost ¶ added in v0.1.14
SetHost accepts a string to set the host of the SyslogMsg
func (*SyslogMsg) SetProgram ¶ added in v0.1.14
SetProgram accepts a string to set the programname of the SyslogMsg
func (*SyslogMsg) SetSeverity ¶ added in v0.1.14
SetSeverity accepts a captainslog.Severity to set the severity of the SyslogMsg
func (*SyslogMsg) SetTime ¶ added in v0.1.14
SetTime accepts a time.Time to set the time of the SyslogMsg
func (*SyslogMsg) String ¶
func (s *SyslogMsg) String(options ...SyslogMsgOption) string
String returns the SyslogMsg as an RFC3164 string.
type SyslogMsgOption ¶ added in v0.1.14
type SyslogMsgOption func(*SyslogMsg)
SyslogMsgOption can be passed to SyslogMsg.String(), SyslogMsg.Byte(), or NewSyslogMsg to control formatting
type Tag ¶ added in v0.1.14
Tag holds the data derviced from a syslog message's tag, including the full tag, the program name and the pid.
type Time ¶ added in v0.1.14
Time holds both the time derviced from a syslog message along with the time format string used to parse it.