syslog

package
v0.0.0-...-93285da Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2017 License: Apache-2.0, BSD-3-Clause Imports: 11 Imported by: 0

README

Using this library you can easy implement your own syslog server that:

  1. Can listen on multiple UDP ports and unix domain sockets.

  2. Can pass parsed syslog messages to your own handlers so your code can analyze and respond for them.

See documentation and example server.

Documentation

Overview

Syslog server library. It is based on RFC 3164 so it doesn't parse properly packets with new header format (described in RFC 5424).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseHandler

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

BaseHandler is desigend for simplify the creation of real handlers. It implements Handler interface using nonblocking queuing of messages and simple message filtering.

func NewBaseHandler

func NewBaseHandler(qlen int, filter func(*Message) bool, ft bool) *BaseHandler

NewBaseHandler creates BaseHandler using specified filter. If filter is nil or if it returns true messages are passed to BaseHandler internal queue (of qlen length). If filter returns false or ft is true messages are returned to server for future processing by other handlers.

func (*BaseHandler) End

func (h *BaseHandler) End()

End signals the server that handler properly shutdown. You need to call End only if Get has returned nil before.

func (*BaseHandler) Get

func (h *BaseHandler) Get() *Message

Get returns first message from internal queue. It waits for message if queue is empty. It returns nil if there is no more messages to process and handler should shutdown.

func (*BaseHandler) Handle

func (h *BaseHandler) Handle(m *Message) *Message

Handle inserts m in an internal queue. It immediately returns even if queue is full. If m == nil it closes queue and waits for End method call before return.

func (*BaseHandler) Queue

func (h *BaseHandler) Queue() <-chan *Message

Queue returns BaseHandler internal queue as read-only channel. You can use it directly, especially if your handler need to select from multiple channels or have to work without blocking. You need to check if this channel is closed by sender and properly shutdown in this case.

type Facility

type Facility byte
const (
	Kern Facility = iota
	User
	Mail
	Daemon
	Auth
	Syslog
	Lpr
	News
	Uucp
	Cron
	Authpriv
	System0
	System1
	System2
	System3
	System4
	Local0
	Local1
	Local2
	Local3
	Local4
	Local5
	Local6
	Local7
)

func (Facility) String

func (f Facility) String() string

type FatalLogger

type FatalLogger interface {
	Fatal(...interface{})
	Fatalf(format string, v ...interface{})
	Fatalln(...interface{})
}

FatalLogger is an interface for logging package internal fatal errors

type FileHandler

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

FileHandler implements Handler interface in the way to save messages into a text file. It properly handles logrotate HUP signal (closes a file and tries to open/create new one).

func NewFileHandler

func NewFileHandler(filename string, qlen int, filter func(*Message) bool,
	ft bool) *FileHandler

NewFileHandler accepts all arguments expected by NewBaseHandler plus filename which is the path to the log file.

func (*FileHandler) Handle

func (h *FileHandler) Handle(m *Message) *Message

func (*FileHandler) SetLogger

func (h *FileHandler) SetLogger(l Logger)

SetLogger changes an internal logger used to log I/O errors. By default I/O errors are written to os.Stderr using log.Logger.

type Handler

type Handler interface {
	// Handle should return Message (mayby modified) for future processing by
	// other handlers or return nil. If Handle is called with nil message it
	// should complete all remaining work and properly shutdown before return.
	Handle(*Message) *Message
}

Handler handles syslog messages

type Logger

type Logger interface {
	Print(...interface{})
	Printf(format string, v ...interface{})
	Println(...interface{})
}

Logger is an interface for package internal (non fatal) logging

type Message

type Message struct {
	Time   time.Time
	Source net.Addr
	Facility
	Severity
	Timestamp time.Time // optional
	Hostname  string    // optional
	Tag       string    // message tag as defined in RFC 3164
	Content   string    // message content as defined in RFC 3164
	Tag1      string    // alternate message tag (white rune as separator)
	Content1  string    // alternate message content (white rune as separator)
}

func (*Message) NetSrc

func (m *Message) NetSrc() string

NetSrc only network part of Source as string (IP for UDP or Name for UDS)

func (*Message) String

func (m *Message) String() string

type Server

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

func NewServer

func NewServer() *Server

NewServer creates idle server

func (*Server) AddHandler

func (s *Server) AddHandler(h Handler)

AddHandler adds h to internal ordered list of handlers

func (*Server) Listen

func (s *Server) Listen(addr string) error

Listen starts gorutine that receives syslog messages on specified address. addr can be a path (for unix domain sockets) or host:port (for UDP).

func (*Server) SetLogger

func (s *Server) SetLogger(l FatalLogger)

SetLogger sets logger for server errors. A running server is rather quiet and logs only fatal errors using FatalLogger interface. By default standard Go logger is used so errors are writen to stderr and after that whole application is halted. Using SetLogger you can change this behavior (log erross elsewhere and don't halt whole application).

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown stops server.

type Severity

type Severity byte
const (
	Emerg Severity = iota
	Alert
	Crit
	Err
	Warning
	Notice
	Info
	Debug
)

func (Severity) String

func (s Severity) String() string

Jump to

Keyboard shortcuts

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