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 // Package syslog provides a simple interface to the system log service. It
6 // can send messages to the syslog daemon using UNIX domain sockets, UDP, or
20 // From /usr/include/sys/syslog.h.
21 // These are the same on Linux, BSD, and OS X.
22 LOG_EMERG Priority = iota
32 // A Writer is a connection to a syslog server.
39 type serverConn interface {
40 writeBytes(p Priority, prefix string, b []byte) (int, os.Error)
41 writeString(p Priority, prefix string, s string) (int, os.Error)
49 // New establishes a new connection to the system log daemon.
50 // Each write to the returned writer sends a log message with
51 // the given priority and prefix.
52 func New(priority Priority, prefix string) (w *Writer, err os.Error) {
53 return Dial("", "", priority, prefix)
56 // Dial establishes a connection to a log daemon by connecting
57 // to address raddr on the network net.
58 // Each write to the returned writer sends a log message with
59 // the given priority and prefix.
60 func Dial(network, raddr string, priority Priority, prefix string) (w *Writer, err os.Error) {
66 conn, err = unixSyslog()
69 c, err = net.Dial(network, raddr)
72 return &Writer{priority, prefix, conn}, err
75 // Write sends a log message to the syslog daemon.
76 func (w *Writer) Write(b []byte) (int, os.Error) {
77 if w.priority > LOG_DEBUG || w.priority < LOG_EMERG {
80 return w.conn.writeBytes(w.priority, w.prefix, b)
83 func (w *Writer) writeString(p Priority, s string) (int, os.Error) {
84 return w.conn.writeString(p, w.prefix, s)
87 func (w *Writer) Close() os.Error { return w.conn.close() }
89 // Emerg logs a message using the LOG_EMERG priority.
90 func (w *Writer) Emerg(m string) (err os.Error) {
91 _, err = w.writeString(LOG_EMERG, m)
94 // Crit logs a message using the LOG_CRIT priority.
95 func (w *Writer) Crit(m string) (err os.Error) {
96 _, err = w.writeString(LOG_CRIT, m)
99 // ERR logs a message using the LOG_ERR priority.
100 func (w *Writer) Err(m string) (err os.Error) {
101 _, err = w.writeString(LOG_ERR, m)
105 // Warning logs a message using the LOG_WARNING priority.
106 func (w *Writer) Warning(m string) (err os.Error) {
107 _, err = w.writeString(LOG_WARNING, m)
111 // Notice logs a message using the LOG_NOTICE priority.
112 func (w *Writer) Notice(m string) (err os.Error) {
113 _, err = w.writeString(LOG_NOTICE, m)
116 // Info logs a message using the LOG_INFO priority.
117 func (w *Writer) Info(m string) (err os.Error) {
118 _, err = w.writeString(LOG_INFO, m)
121 // Debug logs a message using the LOG_DEBUG priority.
122 func (w *Writer) Debug(m string) (err os.Error) {
123 _, err = w.writeString(LOG_DEBUG, m)
127 func (n netConn) writeBytes(p Priority, prefix string, b []byte) (int, os.Error) {
128 return fmt.Fprintf(n.conn, "<%d>%s: %s\n", p, prefix, b)
131 func (n netConn) writeString(p Priority, prefix string, s string) (int, os.Error) {
132 return fmt.Fprintf(n.conn, "<%d>%s: %s\n", p, prefix, s)
135 func (n netConn) close() os.Error {
136 return n.conn.Close()
139 // NewLogger provides an object that implements the full log.Logger interface,
140 // but sends messages to Syslog instead; flag is passed as is to Logger;
141 // priority will be used for all messages sent using this interface.
142 // All messages are logged with priority p.
143 func NewLogger(p Priority, flag int) *log.Logger {
148 return log.New(s, "", flag)