syslog

package module
v0.0.0-...-6be94e8 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2015 License: BSD-3-Clause Imports: 10 Imported by: 5

README

syslog

fork of the standard go syslog package

This adds the ability to write to syslog daemons using TLS, as well as implementing this for Windows


package main

import (
	"crypto/tls"
	"crypto/x509"
	"fmt"

	"github.com/gabriel-samfira/syslog"
)

const caPem = `-----BEGIN CERTIFICATE-----
MIICXzCCAcqgAwIBAgIBADALBgkqhkiG9w0BAQUwRTENMAsGA1UEChMEanVqdTE0
MDIGA1UEAwwranVqdS1nZW5lcmF0ZWQgQ0EgZm9yIGVudmlyb25tZW50ICJyc3lz
bG9nIjAeFw0xNDA4MDUxMjEzNTBaFw0yNDA4MDUxMjE4NTBaMEUxDTALBgNVBAoT
BGp1anUxNDAyBgNVBAMMK2p1anUtZ2VuZXJhdGVkIENBIGZvciBlbnZpcm9ubWVu
dCAicnN5c2xvZyIwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALSz4DWGHrXW
xp6uwwJ3j6amUhQajtGetkrPWLXp85gpdnwDgXgCOm/RXWHV2F2FtiSXkAf9FOQR
AOz2UhElHRMsv4+dsLJL9HfG2VtD6p73qR4vpwMYfIYb9ofHoK9A9tSpUoZRwZRz
wgoiayjeXvXMh9WRiszjln9dpYsUmZQlAgMBAAGjYzBhMA4GA1UdDwEB/wQEAwIA
pDAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBRtRlWT4zNaljsAYuaJo4epOwaH
HTAfBgNVHSMEGDAWgBRtRlWT4zNaljsAYuaJo4epOwaHHTALBgkqhkiG9w0BAQUD
gYEAAwi3/RUlgxt5xEQW3V4kgZmyAMrGt6uM417htZw/7E9CkfCFPjYKIITQKjAO
2ytOpL9dkJcDPW488vWkTBBqBSJWX6Vjz+T1Z6sebw24+VvvTo7oaQGhlJD4stLY
byTiSrVQmhaH5QPCErgdeBn6AZkIZ1XuB5VMoYTYbBLObO0=
-----END CERTIFICATE-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rf0d82CNJLrNN2ywx1QI4QfP75gZhqEL
ARyZvCNjyxKVHa8D252NgLSKsUBTGllB3Dn9M8MZ9i9w6AapSwTwy9hxCrgB6ILC
6BnWW+HpuWq6v1Ft+lNycwoDwlevlpX7jfpmQTaNxYFg2jECQQDs354qlZs/Boqz
RTdgkM31kglcXUo8W4ZxU35DiVWsGb24boo6HurTwyqJBOogxDnWIZw4kgCbdRUW
FMA/04TtAkEA8nm8+WghdSgRDxXD486zzhrRnt6++vcARiJs4Mc621H9yjNwLrHz
2eIdWeE/2/xXtETWtGTX9ByQ8ufg3+kCBQJADDlF+kCaMFhwE+xAfVU7q66LmR6f
VBoNCBAc9fNCXo09gyUBMRqjV6Y8rbF5O5OkwG4fl7PBIEScf/U2LpUFyQJBAIdt
rzquCmHhKwX95hdKz+qB2CqfxpNted2yRJWXMSxmMxXIfRPXmJdNT49v27cGzgWF
nVXMLUHO4raJBHSLM/ECQQCAAuxb/GLAPDH9cbHo1BglU2mSzT81hSqanXcAapeh
2Y4xinXaXKxrgDFmPQJJZ2P+iCQuZp522N1+uro1zDlL
-----END RSA PRIVATE KEY-----`

func main() {
	caCert := x509.NewCertPool()
	ok := caCert.AppendCertsFromPEM([]byte(caPem))
	if !ok {
		fmt.Println("failed to parse root certificate")
		return
	}
	keyPair, err := tls.X509KeyPair([]byte(cert), []byte(key))
	if err != nil {
		fmt.Println("invalid keypair")
		return
	}

	tlsCfg := &tls.Config{
		ClientCAs: caCert,
		Certificates: []tls.Certificate{
			keyPair,
		},
		InsecureSkipVerify: true,
	}
	sLog, err := syslog.Dial("tcp", "192.168.200.51:6514", syslog.LOG_CRIT, "juju-syslog_test", tlsCfg)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer sLog.Close()
	err = sLog.Warning("hello")
	if err != nil {
		fmt.Println(err)
		return
	}
}

Documentation

Overview

Package syslog provides a simple interface to the system log service. It can send messages to the syslog daemon using UNIX domain sockets, UDP or TCP.

Only one call to Dial is necessary. On write failures, the syslog client will attempt to reconnect to the server and write again.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(p Priority, logFlag int) (*log.Logger, error)

NewLogger creates a log.Logger whose output is written to the system log service with the specified priority. The logFlag argument is the flag set passed through to log.New to create the Logger.

Types

type Priority

type Priority int

The Priority is a combination of the syslog facility and severity. For example, LOG_ALERT | LOG_FTP sends an alert severity message from the FTP facility. The default severity is LOG_EMERG; the default facility is LOG_KERN.

const (

	// From /usr/include/sys/syslog.h.
	// These are the same on Linux, BSD, and OS X.
	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 {
	// contains filtered or unexported fields
}

A Writer is a connection to a syslog server.

func Dial

func Dial(network, raddr string, priority Priority, tag string, tlsCfg *tls.Config) (*Writer, error)

Dial establishes a connection to a log daemon by connecting to address raddr on the network net. Each write to the returned writer sends a log message with the given facility, severity and tag.

func New

func New(priority Priority, tag string) (w *Writer, err error)

New establishes a new connection to the system log daemon. Each write to the returned writer sends a log message with the given priority and prefix.

func (*Writer) Alert

func (w *Writer) Alert(m string) (err error)

Alert logs a message with severity LOG_ALERT, ignoring the severity passed to New.

func (*Writer) Close

func (w *Writer) Close() error

Close closes a connection to the syslog daemon.

func (*Writer) Crit

func (w *Writer) Crit(m string) (err error)

Crit logs a message with severity LOG_CRIT, ignoring the severity passed to New.

func (*Writer) Debug

func (w *Writer) Debug(m string) (err error)

Debug logs a message with severity LOG_DEBUG, ignoring the severity passed to New.

func (*Writer) Emerg

func (w *Writer) Emerg(m string) (err error)

Emerg logs a message with severity LOG_EMERG, ignoring the severity passed to New.

func (*Writer) Err

func (w *Writer) Err(m string) (err error)

Err logs a message with severity LOG_ERR, ignoring the severity passed to New.

func (*Writer) Info

func (w *Writer) Info(m string) (err error)

Info logs a message with severity LOG_INFO, ignoring the severity passed to New.

func (*Writer) Notice

func (w *Writer) Notice(m string) (err error)

Notice logs a message with severity LOG_NOTICE, ignoring the severity passed to New.

func (*Writer) Warning

func (w *Writer) Warning(m string) (err error)

Warning logs a message with severity LOG_WARNING, ignoring the severity passed to New.

func (*Writer) Write

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

Write sends a log message to the syslog daemon.

Jump to

Keyboard shortcuts

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