Documentation
¶
Overview ¶
Package syslog sends messages to the local syslog daemon over a Unix datagram socket (e.g. /dev/log), in the RFC 3164 form glibc's syslog() uses (the default, which journald and rsyslog parse) or in RFC 5424, and provides AuditHandler, a slog.Handler forwarding audit records there.
Index ¶
- Constants
- Variables
- func KeepAudit(next slog.Handler, level slog.Leveler) slog.Handler
- func ValidateAppName(name string) error
- type AuditHandler
- func (h *AuditHandler) Close(ctx context.Context) error
- func (h *AuditHandler) Enabled(_ context.Context, level slog.Level) bool
- func (h *AuditHandler) Handle(_ context.Context, record slog.Record) error
- func (h *AuditHandler) Stats() AuditStats
- func (h *AuditHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *AuditHandler) WithGroup(name string) slog.Handler
- type AuditOption
- type AuditStats
- type Facility
- type Format
- type Message
- type Option
- type Severity
- type Syslog
Constants ¶
const ( // AuditKey is the attribute that marks a record as an audit record; its // value, the audit kind (e.g. "token_issued"), becomes the MSGID. AuditKey = "audit" // LevelNotice is the slog level mapped to the syslog notice severity. LevelNotice = slog.LevelInfo + 2 // DefaultQueueSize is the number of audit records waiting to be sent // beyond which new ones are dropped. DefaultQueueSize = 1024 )
const ( // DefaultSocket is the Unix datagram socket of the local syslog daemon. DefaultSocket = "/dev/log" // DefaultSendTimeout bounds each write to the socket. DefaultSendTimeout = 1 * time.Second // DefaultMaxSize is the maximum size of a serialized message: Unix // datagram sockets reject larger ones. DefaultMaxSize = 8 * 1024 )
const Version = 1
Version is the syslog protocol version (RFC 5424, section 6.2.2).
Variables ¶
var ( // ErrClosed is returned when sending through a closed Syslog. ErrClosed = errors.New("syslog client closed") // ErrTooLarge is returned when a message's header and structured data // alone exceed the maximum size. ErrTooLarge = errors.New("syslog message header exceeds the maximum size") )
var ErrQueueFull = errors.New("syslog audit queue full")
ErrQueueFull is reported when audit records are dropped because the queue is full.
Functions ¶
func KeepAudit ¶
KeepAudit returns a handler passing to next the records at or above level, and the audit records whatever the level, so that lowering the verbosity of the regular log (even turning it off) never removes the audit trail from it. A record is an audit record exactly when AuditHandler would forward it: it is at slog.LevelInfo or above and carries a non-empty AuditKey attribute outside any group. next must be enabled at slog.LevelInfo for audit records to reach it.
func ValidateAppName ¶
ValidateAppName checks an RFC 5424 APP-NAME: 1 to 48 printable US-ASCII characters, and not the NILVALUE "-".
Types ¶
type AuditHandler ¶
type AuditHandler struct {
// contains filtered or unexported fields
}
AuditHandler is a slog.Handler that forwards to syslog only the records carrying an AuditKey attribute, either on the record or added with WithAttrs; other records are ignored. It is meant to be combined with the regular handler (e.g. with slog.NewMultiHandler).
Each audit record becomes an RFC 5424 message whose MSGID is the audit kind and whose text is a single-line JSON object holding the record's message ("msg"), its level ("level") and its attributes, attributes in groups under dotted keys (e.g. "req.id"). If it does not fit in the maximum message size, attributes are left out, last first, and "truncated" is set to true.
Records are sent asynchronously, through a bounded queue: Handle never blocks on the socket, and records are dropped when the queue is full or the send fails (see WithFailureFunc and WithRecoveryFunc). Audit records must be logged at slog.LevelInfo or above.
func NewAuditHandler ¶
func NewAuditHandler(s *Syslog, facility Facility, options ...AuditOption) (*AuditHandler, error)
NewAuditHandler returns an AuditHandler sending audit records through s, with the given facility, and starts the goroutine sending them; Close stops it and closes s.
func (*AuditHandler) Close ¶
func (h *AuditHandler) Close(ctx context.Context) error
Close stops accepting audit records, sends those still queued until ctx is done, and closes the syslog connection. It closes the handlers derived from h too, and only the first call has any effect.
func (*AuditHandler) Stats ¶
func (h *AuditHandler) Stats() AuditStats
Stats returns the current AuditStats; handlers derived with WithAttrs or WithGroup share them.
type AuditOption ¶
type AuditOption func(*auditSink)
AuditOption is a functional option type that allows us to configure the AuditHandler.
func WithFailureFunc ¶
func WithFailureFunc(f func(err error)) AuditOption
WithFailureFunc allows to specify a function called when audit records start being dropped, with the cause (ErrQueueFull, or the error of a failed send). It is called once per failure episode, not per record, and may be called from any goroutine.
func WithQueueSize ¶
func WithQueueSize(size int) AuditOption
WithQueueSize allows to specify the number of audit records waiting to be sent beyond which new ones are dropped.
func WithRecoveryFunc ¶
func WithRecoveryFunc(f func(dropped uint64)) AuditOption
WithRecoveryFunc allows to specify a function called when an audit record is delivered again after a failure episode, with the number of records dropped during it. It may be called from any goroutine.
type AuditStats ¶
type AuditStats struct {
// Dropped counts the records dropped since the handler started.
Dropped uint64
// Queued is the number of records waiting to be sent.
Queued int
}
AuditStats describes an AuditHandler's delivery, for the metrics.
type Facility ¶
type Facility uint8
Facility is the system component a message comes from (RFC 5424, section 6.2.1), with its numerical code as value.
const ( FacilityKern Facility = iota FacilityUser FacilityMail FacilityDaemon FacilityAuth FacilitySyslog FacilityLPR FacilityNews FacilityUUCP FacilityCron FacilityAuthpriv FacilityFTP FacilityNTP FacilityAudit FacilityAlert FacilityClock FacilityLocal0 FacilityLocal1 FacilityLocal2 FacilityLocal3 FacilityLocal4 FacilityLocal5 FacilityLocal6 FacilityLocal7 )
The facilities, by code. FacilityKern is reserved for the kernel and refused, so that a zero value cannot slip through unnoticed.
func ParseFacility ¶
ParseFacility returns the facility named name: auth, authpriv, daemon or local0 to local7.
type Format ¶
type Format int
Format is the wire format of the messages.
const ( // FormatRFC3164 is "<PRI>Mmm dd hh:mm:ss TAG[PID]: MSG", as glibc's // syslog() sends it to /dev/log: journald (which provides /dev/log under // systemd) and rsyslog parse it, with the application as // SYSLOG_IDENTIFIER. It has no MSGID nor structured data, and its // timestamp no year nor fraction. It is the default. FormatRFC3164 Format = iota // FormatRFC5424 is the structured format of RFC 5424, which journald // does not parse (the header ends up in the message text): only for a // syslog daemon that owns the socket and understands it. FormatRFC5424 )
type Message ¶
type Message struct {
Facility Facility
Severity Severity
ID string
// Time is the time of the event; the zero value means now.
Time time.Time
Content any // either a string or an object that will be marshalled to JSON
// Data holds the structured data elements, by ID; each parameter has the
// form "name=value". It requires an enterprise number (WithEnterprise).
Data map[string][]string
}
Message contains the set of information that is specific to a given message: its priority (in terms of facility and severity), the message type identifier (to group similar messages, the message text and optionally a set of parameters.
type Option ¶
type Option func(*Syslog)
Option is a functional option type that allows us to configure the Syslog.
func WithApplication ¶
WithApplication allows to specify the name of the application that is going to send events to the syslog (RFC 5424 APP-NAME).
func WithEnterprise ¶
WithEnterprise allows to specify the IANA private enterprise number that qualifies the IDs of the structured data elements (e.g. "32473" gives "user@32473"). Without it, messages cannot carry structured data.
func WithFormat ¶
WithFormat allows to specify the wire format (default: FormatRFC3164).
func WithMaxSize ¶
WithMaxSize allows to specify the maximum size of a serialized message; longer message texts are truncated on a UTF-8 character boundary. Zero disables the limit.
func WithProcess ¶
WithProcess allows to specify the name of the process that is sending events to the syslog (RFC 5424 PROCID).
func WithSendTimeout ¶
WithSendTimeout allows to specify the timeout of each write to the socket.
func WithSocket ¶
WithSocket allows to specify the path of the Unix datagram socket of the syslog daemon.
type Severity ¶
type Severity uint8
Severity is the importance of a message (RFC 5424, section 6.2.1), with its numerical code as value: the lower, the more severe.
type Syslog ¶
type Syslog struct {
// contains filtered or unexported fields
}
Syslog wraps a syslog connection and stores all common configuration elements. It is safe for concurrent use.
func New ¶
New creates a new Syslog connected to the syslog socket, initialising all relevant fields to the defaults unless options are provided: the application defaults to the base name of the executable, the process to the current PID, the socket to DefaultSocket, the maximum size to DefaultMaxSize and the send timeout to DefaultSendTimeout; there is no default enterprise number.
func (*Syslog) Send ¶
Send prepares a message in the client's format and sends it to the syslog socket. The message is validated first, and its text truncated on a UTF-8 character boundary if the whole would exceed the maximum size. If the write fails (e.g. because the syslog daemon restarted), the socket is dialed again and the write retried once.