1
// Copyright 2009 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
5
// The syslog package provides a simple interface to
6
// the system log service. It can send messages to the
7
// syslog daemon using UNIX domain sockets, UDP, or
21
// From /usr/include/sys/syslog.h.
22
// These are the same on Linux, BSD, and OS X.
23
LOG_EMERG Priority = iota
33
// A Writer is a connection to a syslog server.
40
// New establishes a new connection to the system log daemon.
41
// Each write to the returned writer sends a log message with
42
// the given priority and prefix.
43
func New(priority Priority, prefix string) (w *Writer, err os.Error) {
44
return Dial("", "", priority, prefix)
47
// Dial establishes a connection to a log daemon by connecting
48
// to address raddr on the network net.
49
// Each write to the returned writer sends a log message with
50
// the given priority and prefix.
51
func Dial(network, raddr string, priority Priority, prefix string) (w *Writer, err os.Error) {
57
conn, err = unixSyslog()
59
conn, err = net.Dial(network, "", raddr)
61
return &Writer{priority, prefix, conn}, err
64
func unixSyslog() (conn net.Conn, err os.Error) {
65
logTypes := []string{"unixgram", "unix"}
66
logPaths := []string{"/dev/log", "/var/run/syslog"}
68
for _, network := range logTypes {
69
for _, path := range logPaths {
71
conn, err := net.Dial(network, "", raddr)
79
return nil, os.ErrorString("Unix syslog delivery error")
82
// Write sends a log message to the syslog daemon.
83
func (w *Writer) Write(b []byte) (int, os.Error) {
84
if w.priority > LOG_DEBUG || w.priority < LOG_EMERG {
87
return fmt.Fprintf(w.conn, "<%d>%s: %s\n", w.priority, w.prefix, b)
90
func (w *Writer) writeString(p Priority, s string) (int, os.Error) {
91
return fmt.Fprintf(w.conn, "<%d>%s: %s\n", p, w.prefix, s)
94
func (w *Writer) Close() os.Error { return w.conn.Close() }
96
// Emerg logs a message using the LOG_EMERG priority.
97
func (w *Writer) Emerg(m string) (err os.Error) {
98
_, err = w.writeString(LOG_EMERG, m)
101
// Crit logs a message using the LOG_CRIT priority.
102
func (w *Writer) Crit(m string) (err os.Error) {
103
_, err = w.writeString(LOG_CRIT, m)
106
// ERR logs a message using the LOG_ERR priority.
107
func (w *Writer) Err(m string) (err os.Error) {
108
_, err = w.writeString(LOG_ERR, m)
112
// Warning logs a message using the LOG_WARNING priority.
113
func (w *Writer) Warning(m string) (err os.Error) {
114
_, err = w.writeString(LOG_WARNING, m)
118
// Notice logs a message using the LOG_NOTICE priority.
119
func (w *Writer) Notice(m string) (err os.Error) {
120
_, err = w.writeString(LOG_NOTICE, m)
123
// Info logs a message using the LOG_INFO priority.
124
func (w *Writer) Info(m string) (err os.Error) {
125
_, err = w.writeString(LOG_INFO, m)
128
// Debug logs a message using the LOG_DEBUG priority.
129
func (w *Writer) Debug(m string) (err os.Error) {
130
_, err = w.writeString(LOG_DEBUG, m)
134
// NewLogger provides an object that implements the full log.Logger interface,
135
// but sends messages to Syslog instead; flag is passed as is to Logger;
136
// priority will be used for all messages sent using this interface.
137
// All messages are logged with priority p.
138
func NewLogger(p Priority, flag int) *log.Logger {
143
return log.New(s, "", flag)