Categorygithub.com/digitalocean/captainslog
modulepackage
0.1.14
Repository: https://github.com/digitalocean/captainslog.git
Documentation: pkg.go.dev

# README

captainslog Build Status Doc Status

Construct, emit, and parse Syslog messages.

Usage

Create a captainslog.SyslogMsg from RF3164 bytes:

b := []byte("<191>2006-01-02T15:04:05.999999-07:00 host.example.org test: engage\n")
msg, err := captainslog.NewSyslogMsgFromBytes(b)
if err != nil {
	panic(err)
}

Create a captainslog.SyslogMsg by setting its fields:

msg := captainslog.NewSyslogMsg()
msg.SetFacility(captainslog.Local7)
msg.SetSeverity(captainslog.Err)

msgTime, err := time.Parse("2006 Jan 02 15:04:05", "2017 Aug 15 16:18:34")
if err != nil {
	t.Error(err)
}

msg.SetTime(msgTime)
msg.SetProgram("myprogram")
msg.SetPid("12")
msg.SetHost("host.example.com")

captainslog.NewSyslogMsg accepts the following functional options (note: these may also be passed to SyslogMsg.Bytes() and SyslogMsg.String):

captainslog.OptionUseLocalFormat tells SyslogMsg.String() and SyslogMsg.Byte() to format the message to be compatible with writing to /dev/log rather than over the wire.

captainslog.OptionUseRemoteFormat tells SyslogMsg.String() and SyslogMsg.Byte() to use wire format for the message instead of local format.

Serialize a captainslog.SyslogMsg to RFC3164 bytes:

b := msg.Bytes()

Create a captainslog.Parser and parse a message:

p := captainslog.NewParser(<options>)
msg, err := p.ParseBytes([]byte(line)

Both captainslog.NewSyslogMsgFromBytes and captainslog.NewParser accept the following functional arguments:

captainslog.OptionNoHostname sets the parser to not expect the hostname as part of the syslog message, and instead ask the host for its hostname.

captainslog.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.

captainslog.OptionUseGJSONParser uses the tidwall/gjson parser to parse JSON in the content field of the message. This may improve parsing performance.

captainslog.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.

Contibution Guidelines

We use the Collective Code Construction Contract for the development of captainslog. For details, see CONTRIBUTING.md.

License

Copyright 2016 DigitalOcean

Captainslog is released under the Mozilla Public License, version 2.0

# Functions

CheckForLikelyDateTime checks for a YYYY-MM-DD string.
ContentOptionParseJSON will treat the content as a CEE message.
ContentOptionRequireTerminator sets ParseContent to require a \n terminator.
ContentOptionUseGJSON will use the "github.com/tidwall/gjson" JSON parser, if ContentOptionParseJSON is specified.
NewParser returns a new parser.
NewPriority calculates a Priority from a Facility and Severity.
NewSyslogMsg creates a new empty SyslogMsg.
NewSyslogMsgFromBytes accepts a []byte containing an RFC3164 message and returns a SyslogMsg.
NewTag constructs a new empty captainslog.Tag.
OptionDontParseJSON sets the parser to not parse JSON in the content field of the message.
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.
OptionNoHostname sets the parser to not expect the hostname as part of the syslog message, and instead ask the host for its hostname.
OptionSanitizeProgram sets the parser to sanitize the syslog program name if needed.
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.
OptionUseLocalFormat tells SyslogMsg.String() and SyslogMsg.Byte() to format the message to be compatible with writing to /dev/log rather than over the wire.
OptionUseRemoteFormat tells SyslogMsg.String() and SyslogMsg.Byte() to use wire format for the message instead of local format.
ParseCEE will try to find a syslog cee cookie at the beginning of the passed in []byte.
ParseContent will try to find syslog content at the beginning of the passed in []byte.
ParseHost will try to find a host at the beginning of the passed in []byte.
ParsePri will try to find a syslog priority at the beginning of the passed in []byte.
ParseTag will try to find a syslog tag at the beginning of the passed in []byte.
ParseTime will try to find a syslog time at the beginning of the passed in []byte.
TagOptionSanitizeProgram sets the tag options to sanitize the program name.

# Constants

Alert is an alert rfc3164 severity.
Auth is the auth rfc3164 facility.
AuthPriv is the authpriv rfc3164 facility.
Crit is a critical level rfc3164 severity.
Cron is the cron rfc3164 facility.
Daemon is the daemon rfc3164 facility.
Debug is a debug level rfc3164 severity.
Emerg is an emergency rfc3164 severity.
Err is an error level rfc3164 severity.
FTP is the ftp rfc3164 facility.
Info is an info level rfc3164 severity.
Kern is the kernel rfc3164 facility.
Local0 is the local0 rfc3164 facility.
Local1 is the local1 rfc3164 facility.
Local2 is the local2 rfc3164 facility.
Local3 is the local3 rfc3164 facility.
Local4 is the local4 rfc3164 facility.
Local5 is the local5 rfc3164 facility.
Local6 is the local6 rfc3164 facility.
Local7 is the local7 rfc3164 facility.
LPR is the printer rfc3164 facility.
Mail is the mail rfc3164 facility.
News is a news rfc3164 facility.
Notice is a notice level rfc3164 severity.
Syslog is the syslog rfc3164 facility.
User is the user rfc3164 facility.
UUCP is the UUCP rfc3164 facility.
Warning is a warning level rfc3164 severity.

# Variables

ErrBadContent is returned when the content of a message is malformed.
ErrBadFacility is returned when a facility is not within allowed values.
ErrBadHost is returned when the host of a message is malformed.
ErrBadPriority is returned when the priority of a message is malformed.
ErrBadSeverity is returned when a severity is not within allowed values.
ErrBadTag is returned when the tag of a message is malformed.
ErrBadTime is returned when the time of a message is malformed.

# Structs

Content holds the Content of a syslog message, including the Content as a string, and a struct of the JSONValues of appropriate.
Parser is a parser for syslog messages.
Priority represents the PRI of a rfc3164 message.
SyslogMsg holds an Unmarshaled rfc3164 message.
Tag holds the data derviced from a syslog message's tag, including the full tag, the program name and the pid.
Time holds both the time derviced from a syslog message along with the time format string used to parse it.

# Type aliases

Facility represents a syslog facility code.
Severity represents a syslog severity code.
SyslogMsgOption can be passed to SyslogMsg.String(), SyslogMsg.Byte(), or NewSyslogMsg to control formatting.