OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package syslog provides a simple interface to the system log service. It | 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 | 6 // can send messages to the syslog daemon using UNIX domain sockets, UDP, or |
7 // TCP connections. | 7 // TCP connections. |
8 package syslog | 8 package syslog |
9 | 9 |
10 import ( | 10 import ( |
(...skipping 19 matching lines...) Expand all Loading... |
30 ) | 30 ) |
31 | 31 |
32 // A Writer is a connection to a syslog server. | 32 // A Writer is a connection to a syslog server. |
33 type Writer struct { | 33 type Writer struct { |
34 priority Priority | 34 priority Priority |
35 prefix string | 35 prefix string |
36 conn serverConn | 36 conn serverConn |
37 } | 37 } |
38 | 38 |
39 type serverConn interface { | 39 type serverConn interface { |
40 » writeBytes(p Priority, prefix string, b []byte) (int, os.Error) | 40 » writeBytes(p Priority, prefix string, b []byte) (int, error) |
41 » writeString(p Priority, prefix string, s string) (int, os.Error) | 41 » writeString(p Priority, prefix string, s string) (int, error) |
42 » close() os.Error | 42 » close() error |
43 } | 43 } |
44 | 44 |
45 type netConn struct { | 45 type netConn struct { |
46 conn net.Conn | 46 conn net.Conn |
47 } | 47 } |
48 | 48 |
49 // New establishes a new connection to the system log daemon. | 49 // New establishes a new connection to the system log daemon. |
50 // Each write to the returned writer sends a log message with | 50 // Each write to the returned writer sends a log message with |
51 // the given priority and prefix. | 51 // the given priority and prefix. |
52 func New(priority Priority, prefix string) (w *Writer, err os.Error) { | 52 func New(priority Priority, prefix string) (w *Writer, err error) { |
53 return Dial("", "", priority, prefix) | 53 return Dial("", "", priority, prefix) |
54 } | 54 } |
55 | 55 |
56 // Dial establishes a connection to a log daemon by connecting | 56 // Dial establishes a connection to a log daemon by connecting |
57 // to address raddr on the network net. | 57 // to address raddr on the network net. |
58 // Each write to the returned writer sends a log message with | 58 // Each write to the returned writer sends a log message with |
59 // the given priority and prefix. | 59 // the given priority and prefix. |
60 func Dial(network, raddr string, priority Priority, prefix string) (w *Writer, e
rr os.Error) { | 60 func Dial(network, raddr string, priority Priority, prefix string) (w *Writer, e
rr error) { |
61 if prefix == "" { | 61 if prefix == "" { |
62 prefix = os.Args[0] | 62 prefix = os.Args[0] |
63 } | 63 } |
64 var conn serverConn | 64 var conn serverConn |
65 if network == "" { | 65 if network == "" { |
66 conn, err = unixSyslog() | 66 conn, err = unixSyslog() |
67 } else { | 67 } else { |
68 var c net.Conn | 68 var c net.Conn |
69 c, err = net.Dial(network, raddr) | 69 c, err = net.Dial(network, raddr) |
70 conn = netConn{c} | 70 conn = netConn{c} |
71 } | 71 } |
72 return &Writer{priority, prefix, conn}, err | 72 return &Writer{priority, prefix, conn}, err |
73 } | 73 } |
74 | 74 |
75 // Write sends a log message to the syslog daemon. | 75 // Write sends a log message to the syslog daemon. |
76 func (w *Writer) Write(b []byte) (int, os.Error) { | 76 func (w *Writer) Write(b []byte) (int, error) { |
77 if w.priority > LOG_DEBUG || w.priority < LOG_EMERG { | 77 if w.priority > LOG_DEBUG || w.priority < LOG_EMERG { |
78 return 0, os.EINVAL | 78 return 0, os.EINVAL |
79 } | 79 } |
80 return w.conn.writeBytes(w.priority, w.prefix, b) | 80 return w.conn.writeBytes(w.priority, w.prefix, b) |
81 } | 81 } |
82 | 82 |
83 func (w *Writer) writeString(p Priority, s string) (int, os.Error) { | 83 func (w *Writer) writeString(p Priority, s string) (int, error) { |
84 return w.conn.writeString(p, w.prefix, s) | 84 return w.conn.writeString(p, w.prefix, s) |
85 } | 85 } |
86 | 86 |
87 func (w *Writer) Close() os.Error { return w.conn.close() } | 87 func (w *Writer) Close() error { return w.conn.close() } |
88 | 88 |
89 // Emerg logs a message using the LOG_EMERG priority. | 89 // Emerg logs a message using the LOG_EMERG priority. |
90 func (w *Writer) Emerg(m string) (err os.Error) { | 90 func (w *Writer) Emerg(m string) (err error) { |
91 _, err = w.writeString(LOG_EMERG, m) | 91 _, err = w.writeString(LOG_EMERG, m) |
92 return err | 92 return err |
93 } | 93 } |
94 // Crit logs a message using the LOG_CRIT priority. | 94 // Crit logs a message using the LOG_CRIT priority. |
95 func (w *Writer) Crit(m string) (err os.Error) { | 95 func (w *Writer) Crit(m string) (err error) { |
96 _, err = w.writeString(LOG_CRIT, m) | 96 _, err = w.writeString(LOG_CRIT, m) |
97 return err | 97 return err |
98 } | 98 } |
99 // ERR logs a message using the LOG_ERR priority. | 99 // ERR logs a message using the LOG_ERR priority. |
100 func (w *Writer) Err(m string) (err os.Error) { | 100 func (w *Writer) Err(m string) (err error) { |
101 _, err = w.writeString(LOG_ERR, m) | 101 _, err = w.writeString(LOG_ERR, m) |
102 return err | 102 return err |
103 } | 103 } |
104 | 104 |
105 // Warning logs a message using the LOG_WARNING priority. | 105 // Warning logs a message using the LOG_WARNING priority. |
106 func (w *Writer) Warning(m string) (err os.Error) { | 106 func (w *Writer) Warning(m string) (err error) { |
107 _, err = w.writeString(LOG_WARNING, m) | 107 _, err = w.writeString(LOG_WARNING, m) |
108 return err | 108 return err |
109 } | 109 } |
110 | 110 |
111 // Notice logs a message using the LOG_NOTICE priority. | 111 // Notice logs a message using the LOG_NOTICE priority. |
112 func (w *Writer) Notice(m string) (err os.Error) { | 112 func (w *Writer) Notice(m string) (err error) { |
113 _, err = w.writeString(LOG_NOTICE, m) | 113 _, err = w.writeString(LOG_NOTICE, m) |
114 return err | 114 return err |
115 } | 115 } |
116 // Info logs a message using the LOG_INFO priority. | 116 // Info logs a message using the LOG_INFO priority. |
117 func (w *Writer) Info(m string) (err os.Error) { | 117 func (w *Writer) Info(m string) (err error) { |
118 _, err = w.writeString(LOG_INFO, m) | 118 _, err = w.writeString(LOG_INFO, m) |
119 return err | 119 return err |
120 } | 120 } |
121 // Debug logs a message using the LOG_DEBUG priority. | 121 // Debug logs a message using the LOG_DEBUG priority. |
122 func (w *Writer) Debug(m string) (err os.Error) { | 122 func (w *Writer) Debug(m string) (err error) { |
123 _, err = w.writeString(LOG_DEBUG, m) | 123 _, err = w.writeString(LOG_DEBUG, m) |
124 return err | 124 return err |
125 } | 125 } |
126 | 126 |
127 func (n netConn) writeBytes(p Priority, prefix string, b []byte) (int, os.Error)
{ | 127 func (n netConn) writeBytes(p Priority, prefix string, b []byte) (int, error) { |
128 return fmt.Fprintf(n.conn, "<%d>%s: %s\n", p, prefix, b) | 128 return fmt.Fprintf(n.conn, "<%d>%s: %s\n", p, prefix, b) |
129 } | 129 } |
130 | 130 |
131 func (n netConn) writeString(p Priority, prefix string, s string) (int, os.Error
) { | 131 func (n netConn) writeString(p Priority, prefix string, s string) (int, error) { |
132 return fmt.Fprintf(n.conn, "<%d>%s: %s\n", p, prefix, s) | 132 return fmt.Fprintf(n.conn, "<%d>%s: %s\n", p, prefix, s) |
133 } | 133 } |
134 | 134 |
135 func (n netConn) close() os.Error { | 135 func (n netConn) close() error { |
136 return n.conn.Close() | 136 return n.conn.Close() |
137 } | 137 } |
138 | 138 |
139 // NewLogger provides an object that implements the full log.Logger interface, | 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; | 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. | 141 // priority will be used for all messages sent using this interface. |
142 // All messages are logged with priority p. | 142 // All messages are logged with priority p. |
143 func NewLogger(p Priority, flag int) *log.Logger { | 143 func NewLogger(p Priority, flag int) *log.Logger { |
144 s, err := New(p, "") | 144 s, err := New(p, "") |
145 if err != nil { | 145 if err != nil { |
146 return nil | 146 return nil |
147 } | 147 } |
148 return log.New(s, "", flag) | 148 return log.New(s, "", flag) |
149 } | 149 } |
OLD | NEW |