syslog

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2021 License: BSD-3-Clause Imports: 8 Imported by: 3

README

Syslog

Syslog package provides logger that generates syslog messages as defined in RFC 5424.

Example Logger

package main

import (
	"fmt"
	"github.com/confetti-framework/syslog"
	"os"
)

func main() {
	buf := &bytes.Buffer{}
	l := syslog.NewLogger(buf, syslog.USER, "hostname", "appName", "procid")

	// without structured data
	syslog.Info(l, "ImageUploaded", nil, "image uploaded by %s: %s", "username", "image.jpg")

	// with structured data
	sd := syslog.StructuredData{}
	sd.Element("id1").Set("par1", "val1")
	syslog.Error(l, "LoginFailed", sd, "login failed: %s", "username")

	fmt.Print(buf.String())

	// Output is similar to this:
	// <14>1 2017-08-15T23:13:15.335+02:00 hostname appName procid ImageUploaded - image uploaded by username: image.jpg
	// <11>1 2017-08-15T23:13:15.335+02:00 hostname appName procid LoginFailed [id1 par1="val1"] login failed: username
}	

Example Writer

package main

import (
	"github.com/confetti-framework/syslog"
	"log"
	"os"
)

func main() {
	const msg = "Start HTTP server (addr=:8080)"

	hostname := "laptop"
	appName := "testapp"
	procid := "123"
	wrappedWriter := syslog.NewWriter(os.Stdout, syslog.USER|syslog.NOTICE, hostname, appName, procid)
	logger := log.New(wrappedWriter, "", 0)
	logger.Println(msg)

	// Output is similar to this:
	// <13>1 2017-08-15T23:13:15.33+02:00 laptop testapp 123 - - Start HTTP server (addr=:8080)
}

Documentation

Overview

Package syslog provides logger that generates syslog messages as defined in RFC 5424.

Index

Examples

Constants

View Source
const (

	// From /usr/include/sys/syslog.h.
	// These are the same up to FTP on Linux, BSD, and OS X.
	KERN log_level.Priority = iota << 3
	USER
	MAIL
	DAEMON
	AUTH
	SYSLOG
	LPR
	NEWS
	UUCP
	CRON
	AUTHPRIV
	FTP

	LOCAL0
	LOCAL1
	LOCAL2
	LOCAL3
	LOCAL4
	LOCAL5
	LOCAL6
	LOCAL7
)

Variables

This section is empty.

Functions

func Alert

func Alert(l Logger, msgId string, sd StructuredData, format string, a ...interface{})

func Critical

func Critical(l Logger, msgId string, sd StructuredData, format string, a ...interface{})

func Debug

func Debug(l Logger, msgId string, sd StructuredData, format string, a ...interface{})

func Emergency

func Emergency(l Logger, msgId string, sd StructuredData, format string, a ...interface{})

func Error

func Error(l Logger, msgId string, sd StructuredData, format string, a ...interface{})

func Info

func Info(l Logger, msgId string, sd StructuredData, format string, a ...interface{})

func KeyBySeverity

func KeyBySeverity(severity log_level.Priority) string

func NewWriter

func NewWriter(out io.Writer, pri log_level.Priority, hostname, appName, procid string) io.Writer

NewWriter wrappes another io.Writer and returns a new io.Writer that generates syslog messages as defined in RFC 5424 and writes them to the given io.Writer. The returned io.Writer is NOT safe for concurrent use by multiple goroutines.

Example
package main

import (
	"github.com/confetti-framework/syslog"
	"github.com/confetti-framework/syslog/log_level"
	"log"
	"os"
)

func main() {
	const msg = "Start HTTP server (addr=:8080)"

	hostname := "laptop"
	appName := "testapp"
	procid := "123"
	wrappedWriter := syslog.NewWriter(os.Stdout, syslog.USER|log_level.NOTICE, hostname, appName, procid)
	logger := log.New(wrappedWriter, "", 0)
	logger.Println(msg)

	// Output is similar to this:
	// <13>1 2017-08-15T23:13:15.335+02:00 laptop testapp 123 - - Start HTTP server (addr=:8080)
}
Output:

func Notice

func Notice(l Logger, msgId string, sd StructuredData, format string, a ...interface{})

func Warning

func Warning(l Logger, msgId string, sd StructuredData, format string, a ...interface{})

Types

type Facility added in v0.1.1

type Facility = log_level.Priority

type Logger

type Logger interface {

	// Log generates a syslog message.
	Log(severity log_level.Priority, msgId string, sd StructuredData, msgFormat string, a ...interface{})
}

Logger generates syslog messages.

func NewLogger

func NewLogger(w io.Writer, facility log_level.Priority, hostname, appName, procid string) Logger

NewLogger returns a new syslog logger that writes to the specified io.Writer. The returned Logger is safe for concurrent use by multiple goroutines.

Example
package main

import (
	"bytes"
	"fmt"
	"github.com/confetti-framework/syslog"
)

func main() {
	buf := &bytes.Buffer{}
	l := syslog.NewLogger(buf, syslog.USER, "hostname", "appName", "procid")

	// without structured data
	syslog.Info(l, "ImageUploaded", nil, "image uploaded by %s: %s", "username", "image.jpg")

	// with structured data
	sd := syslog.StructuredData{}
	sd.Element("id1").Set("par1", "val1")
	syslog.Error(l, "LoginFailed", sd, "login failed: %s", "username")

	fmt.Print(buf.String())

	// Output is similar to this:
	// <14>1 2017-08-15T23:13:15.335+02:00 hostname appName procid ImageUploaded - image uploaded by username: image.jpg
	// <11>1 2017-08-15T23:13:15.335+02:00 hostname appName procid LoginFailed [id1 par1="val1"] login failed: username
}
Output:

type SDElement

type SDElement map[string]string

SDElement represents a structured data element and consists name-value pairs.

func (SDElement) Get

func (e SDElement) Get(name string) string

Get returns a value associated with the specified name.

func (SDElement) Names

func (e SDElement) Names() []string

Names returns the parameter names in lexicographical order.

func (SDElement) Set

func (e SDElement) Set(name, value string) SDElement

Set sets a value associated with the specified name.

type StructuredData

type StructuredData map[string]SDElement

StructuredData provides a mechanism to express information in a well defined, easily parseable and interpretable data format. There are multiple usage scenarios. For example, it may express meta- information about the syslog message or application-specific information such as traffic counters or IP addresses.

StructuredData can contain zero, one, or multiple structured data elements, which are referred to as SDElement.

func (StructuredData) Element

func (d StructuredData) Element(id string) SDElement

Element returns an SDElement associated with the given id. If an element with the id does not exist a new SDElement will be created.

func (StructuredData) Ids

func (d StructuredData) Ids() []string

Ids returns the ids of the SDElements in lexicographical order.

func (StructuredData) String

func (d StructuredData) String() string

Strings returns the string representation of the structured data.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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