syslog

package module
v0.0.0-...-4ea3f32 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2013 License: MIT Imports: 4 Imported by: 23

README

syslog

Golang alternative for built-in log/syslog package

Differences with standard library

The "log/syslog" package writes raw data directly into syslog socket. The resulting log message will look like:

Sep 18 10:28:52 server 2013-09-18T10:28:52Z server [programname][20310]: Log message 

You see that server name and datetime are duplicated. The log message format is hardcoded into source, thus you cannot omit data duplication.

Another major disadvantage is that resulting log message cannot be processed using tools such as rsyslog, because it cannot obtain the name of the sender application.

The approach used in this library is calling C functions openlog and syslog directly. It solves both problems mentioned above.

The library provides:

  • Openlog, Syslog functions with parameters identical to those in syslog.h C header
  • log/syslog-like functions for writing messages: Emerg, Alert, Crit etc.
  • Their formatted versions: Emergf, Alertf, Critf etc.
  • io.Writer interface

Restrictions

Because of using C-strings for calling syslog functions, to omit memory leaks when you call Openlog function the data from ident parameter is copied into static char array with fixed length. Length of this array is defined in syslog_wrapper.h and currently equals 1000. If length if ident string exceeds it, then it will be cut up to maximum value.

Example

import "github.com/blackjack/syslog"
func main() {
    syslog.Openlog("awesome_app", syslog.LOG_PID, syslog.LOG_USER)
    syslog.Syslog(syslog.LOG_INFO, "Hello syslog!")
    syslog.Err("Sample error message")
    syslog.Critf("Sample %s crit message", "formatted")
}

The resulting log message will look like:

Sep 18 18:13:46 server awesome_app[16844]: Hello syslog!
Sep 18 18:13:46 server awesome_app[16844]: Sample error message
Sep 18 18:13:46 server awesome_app[16844]: Sample formatted crit message

Feel the difference!

Documentation

Overview

Package syslog provides easy to use interface for syslog logging system

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alert

func Alert(msg string)

Writes syslog message with log level ALERT

func Alertf

func Alertf(format string, a ...interface{})

Formats according to a format specifier and writes to syslog with log level ALERT

func Closelog

func Closelog()

Closes the current Syslog connection, if there is one. This includes closing the /dev/log socket, if it is open. Closelog also sets the identification string for Syslog messages back to the default,

func Crit

func Crit(msg string)

Writes syslog message with log level CRIT

func Critf

func Critf(format string, a ...interface{})

Formats according to a format specifier and writes to syslog with log level CRIT

func Debug

func Debug(msg string)

Writes syslog message with log level DEBUG

func Debugf

func Debugf(format string, a ...interface{})

Formats according to a format specifier and writes to syslog with log level DEBUG

func Emerg

func Emerg(msg string)

Writes syslog message with log level EMERG

func Emergf

func Emergf(format string, a ...interface{})

Formats according to a format specifier and writes to syslog with log level EMERG

func Err

func Err(msg string)

Writes syslog message with log level ERR

func Errf

func Errf(format string, a ...interface{})

Formats according to a format specifier and writes to syslog with log level ERR

func Info

func Info(msg string)

Writes syslog message with log level INFO

func Infof

func Infof(format string, a ...interface{})

Formats according to a format specifier and writes to syslog with log level INFO

func Notice

func Notice(msg string)

Writes syslog message with log level NOTICE

func Noticef

func Noticef(format string, a ...interface{})

Formats according to a format specifier and writes to syslog with log level NOTICE

func Openlog

func Openlog(ident string, o Option, p Priority)

Opens or reopens a connection to Syslog in preparation for submitting messages. See http://www.gnu.org/software/libc/manual/html_node/openlog.html for parameters description

func Syslog

func Syslog(p Priority, msg string)

Writes msg to syslog with facility and priority indicated by parameter "p" You can combine facility and priority with bitwise or operator, e.g. : syslog.Syslog( syslog.LOG_INFO | syslog.LOG_USER, "Hello syslog")

func Syslogf

func Syslogf(p Priority, format string, a ...interface{})

Formats according to a format specifier and writes to syslog with facility and priority indicated by parameter "p"

func Warning

func Warning(msg string)

Writes syslog message with log level WARNING

func Warningf

func Warningf(format string, a ...interface{})

Formats according to a format specifier and writes to syslog with log level WARNING

Types

type LogMask

type LogMask int

LogMask is a bit string with one bit corresponding to each of the possible message priorities. If the bit is on, syslog handles messages of that priority normally. If it is off, syslog discards messages of that priority Use LOG_MASK and LOG_UPTO to construct an appropriate mask value

func LOG_MASK

func LOG_MASK(p Priority) LogMask

Mask for one priority

func LOG_UPTO

func LOG_UPTO(p Priority) LogMask

Generates a mask with the bits on for a certain priority and all priorities above it The unfortunate naming is due to the fact that internally, higher numbers are used for lower message priorities.

func SetLogMask

func SetLogMask(p LogMask) LogMask

Sets this logmask for the calling process, and returns the previous mask. If the mask argument is 0, the current logmask is not modified. Example: syslog.SetLogMask( syslog.LOG_MASK(LOG_EMERG) | syslog.LOG_MASK(LOG_ERROR) )

type Option

type Option int
const (
	//Option
	LOG_PID    Option = 0x01
	LOG_CONS   Option = 0x02
	LOG_NDELAY Option = 0x08
	LOG_NOWAIT Option = 0x10
	LOG_PERROR Option = 0x20
)

type Priority

type Priority int
const (
	LOG_EMERG Priority = iota
	LOG_ALERT
	LOG_CRIT
	LOG_ERR
	LOG_WARNING
	LOG_NOTICE
	LOG_INFO
	LOG_DEBUG
)
const (

	// From /usr/include/sys/syslog.h.
	// These are the same up to LOG_FTP on Linux, BSD, and OS X.
	LOG_KERN Priority = iota << 3
	LOG_USER
	LOG_MAIL
	LOG_DAEMON
	LOG_AUTH
	LOG_SYSLOG
	LOG_LPR
	LOG_NEWS
	LOG_UUCP
	LOG_CRON
	LOG_AUTHPRIV
	LOG_FTP

	LOG_LOCAL0
	LOG_LOCAL1
	LOG_LOCAL2
	LOG_LOCAL3
	LOG_LOCAL4
	LOG_LOCAL5
	LOG_LOCAL6
	LOG_LOCAL7
)

type Writer

type Writer struct {
	LogPriority Priority
}

An io.Writer() interface

func (*Writer) Write

func (w *Writer) Write(b []byte) (int, error)

Jump to

Keyboard shortcuts

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