Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 // The syslog package provides a simple interface to | 5 // The syslog package provides a simple interface to |
6 // the system log service. It can send messages to the | 6 // the system log service. It can send messages to the |
7 // syslog daemon using UNIX domain sockets, UDP, or | 7 // syslog daemon using UNIX domain sockets, UDP, or |
8 // TCP connections. | 8 // TCP connections. |
9 package syslog | 9 package syslog |
10 | 10 |
11 import ( | 11 import ( |
12 "fmt"; | 12 "fmt"; |
13 "io"; | |
14 "log"; | 13 "log"; |
15 "net"; | 14 "net"; |
16 "os"; | 15 "os"; |
17 ) | 16 ) |
18 | 17 |
19 type Priority int | 18 type Priority int |
20 | 19 |
21 const ( | 20 const ( |
22 // From /usr/include/sys/syslog.h. | 21 // From /usr/include/sys/syslog.h. |
23 // These are the same on Linux, BSD, and OS X. | 22 // These are the same on Linux, BSD, and OS X. |
24 LOG_EMERG Priority = iota; | 23 LOG_EMERG Priority = iota; |
25 LOG_ALERT; | 24 LOG_ALERT; |
26 LOG_CRIT; | 25 LOG_CRIT; |
27 LOG_ERR; | 26 LOG_ERR; |
28 LOG_WARNING; | 27 LOG_WARNING; |
29 LOG_NOTICE; | 28 LOG_NOTICE; |
30 LOG_INFO; | 29 LOG_INFO; |
31 LOG_DEBUG; | 30 LOG_DEBUG; |
32 ) | 31 ) |
33 | 32 |
34 type syslog struct { | 33 // A Writer is a connection to a syslog server. |
34 type Writer struct { | |
35 priority Priority; | 35 priority Priority; |
36 prefix string; | 36 prefix string; |
37 conn net.Conn; | 37 conn net.Conn; |
38 } | 38 } |
39 | 39 |
40 // SyslogWriter is a wrapper for io.WriteCloser with convenience | |
41 // functions for sending logs using specific priorities. | |
42 type SyslogWriter interface { | |
rsc
2009/12/07 20:45:24
Final question: why is this an interface?
Will the
nictuku
2009/12/08 03:12:04
I misinterpreted the documentation and thought tha
| |
43 io.WriteCloser; | |
44 Emerg(m string) (err os.Error); | |
45 Crit(m string) (err os.Error); | |
46 Err(m string) (err os.Error); | |
47 Warning(m string) (err os.Error); | |
48 Notice(m string) (err os.Error); | |
49 Info(m string) (err os.Error); | |
50 Debug(m string) (err os.Error); | |
51 } | |
52 | |
53 | |
54 // New establishes a new connection to the system log daemon. | 40 // New establishes a new connection to the system log daemon. |
55 // Each write to the returned writer sends a log message with | 41 // Each write to the returned writer sends a log message with |
56 // the given priority and prefix. | 42 // the given priority and prefix. |
57 func New(priority Priority, prefix string) (w SyslogWriter, err os.Error) { | 43 func New(priority Priority, prefix string) (w *Writer, err os.Error) { |
58 return Dial("", "", priority, prefix) | 44 return Dial("", "", priority, prefix) |
59 } | 45 } |
60 | 46 |
61 // Dial establishes a connection to a log daemon by connecting | 47 // Dial establishes a connection to a log daemon by connecting |
62 // to address raddr on the network net. | 48 // to address raddr on the network net. |
63 // Each write to the returned writer sends a log message with | 49 // Each write to the returned writer sends a log message with |
64 // the given priority and prefix. | 50 // the given priority and prefix. |
65 func Dial(network, raddr string, priority Priority, prefix string) (w SyslogWrit er, err os.Error) { | 51 func Dial(network, raddr string, priority Priority, prefix string) (w *Writer, e rr os.Error) { |
66 if prefix == "" { | 52 if prefix == "" { |
67 prefix = os.Args[0] | 53 prefix = os.Args[0] |
68 } | 54 } |
69 var conn net.Conn; | 55 var conn net.Conn; |
70 if network == "" { | 56 if network == "" { |
71 conn, err = unixSyslog() | 57 conn, err = unixSyslog() |
72 } else { | 58 } else { |
73 conn, err = net.Dial(network, "", raddr) | 59 conn, err = net.Dial(network, "", raddr) |
74 } | 60 } |
75 » return &syslog{priority, prefix, conn}, err; | 61 » return &Writer{priority, prefix, conn}, err; |
76 } | 62 } |
77 | 63 |
78 func unixSyslog() (conn net.Conn, err os.Error) { | 64 func unixSyslog() (conn net.Conn, err os.Error) { |
79 logTypes := []string{"unixgram", "unix"}; | 65 logTypes := []string{"unixgram", "unix"}; |
80 logPaths := []string{"/dev/log", "/var/run/syslog"}; | 66 logPaths := []string{"/dev/log", "/var/run/syslog"}; |
81 var raddr string; | 67 var raddr string; |
82 for _, network := range logTypes { | 68 for _, network := range logTypes { |
83 for _, path := range logPaths { | 69 for _, path := range logPaths { |
84 raddr = path; | 70 raddr = path; |
85 conn, err := net.Dial(network, "", raddr); | 71 conn, err := net.Dial(network, "", raddr); |
86 if err != nil { | 72 if err != nil { |
87 continue | 73 continue |
88 } else { | 74 } else { |
89 return conn, nil | 75 return conn, nil |
90 } | 76 } |
91 } | 77 } |
92 } | 78 } |
93 return nil, os.ErrorString("Unix syslog delivery error"); | 79 return nil, os.ErrorString("Unix syslog delivery error"); |
94 } | 80 } |
95 | 81 |
96 // Write sends a log message to the syslog daemon. | 82 // Write sends a log message to the syslog daemon. |
97 func (l *syslog) Write(b []byte) (int, os.Error) { | 83 func (w *Writer) Write(b []byte) (int, os.Error) { |
98 » if l.priority > LOG_DEBUG || l.priority < LOG_EMERG { | 84 » if w.priority > LOG_DEBUG || w.priority < LOG_EMERG { |
99 return 0, os.EINVAL | 85 return 0, os.EINVAL |
100 } | 86 } |
101 » return fmt.Fprintf(l.conn, "<%d>%s: %s\n", l.priority, l.prefix, b); | 87 » return fmt.Fprintf(w.conn, "<%d>%s: %s\n", w.priority, w.prefix, b); |
102 } | 88 } |
103 | 89 |
104 func (l *syslog) writeString(p Priority, s string) (int, os.Error) { | 90 func (w *Writer) writeString(p Priority, s string) (int, os.Error) { |
105 » return fmt.Fprintf(l.conn, "<%d>%s: %s\n", p, l.prefix, s) | 91 » return fmt.Fprintf(w.conn, "<%d>%s: %s\n", p, w.prefix, s) |
106 } | 92 } |
107 | 93 |
108 func (l *syslog) Close() os.Error» { return l.conn.Close() } | 94 func (w *Writer) Close() os.Error» { return w.conn.Close() } |
109 | 95 |
110 // Emerg logs a message using the LOG_EMERG priority. | 96 // Emerg logs a message using the LOG_EMERG priority. |
111 func (l *syslog) Emerg(m string) (err os.Error) { | 97 func (w *Writer) Emerg(m string) (err os.Error) { |
112 » _, err = l.writeString(LOG_EMERG, m); | 98 » _, err = w.writeString(LOG_EMERG, m); |
113 return err; | 99 return err; |
114 } | 100 } |
115 // Crit logs a message using the LOG_CRIT priority. | 101 // Crit logs a message using the LOG_CRIT priority. |
116 func (l *syslog) Crit(m string) (err os.Error) { | 102 func (w *Writer) Crit(m string) (err os.Error) { |
117 » _, err = l.writeString(LOG_CRIT, m); | 103 » _, err = w.writeString(LOG_CRIT, m); |
118 return err; | 104 return err; |
119 } | 105 } |
120 // ERR logs a message using the LOG_ERR priority. | 106 // ERR logs a message using the LOG_ERR priority. |
121 func (l *syslog) Err(m string) (err os.Error) { | 107 func (w *Writer) Err(m string) (err os.Error) { |
122 » _, err = l.writeString(LOG_ERR, m); | 108 » _, err = w.writeString(LOG_ERR, m); |
123 return err; | 109 return err; |
124 } | 110 } |
125 | 111 |
126 // Warning logs a message using the LOG_WARNING priority. | 112 // Warning logs a message using the LOG_WARNING priority. |
127 func (l *syslog) Warning(m string) (err os.Error) { | 113 func (w *Writer) Warning(m string) (err os.Error) { |
128 » _, err = l.writeString(LOG_WARNING, m); | 114 » _, err = w.writeString(LOG_WARNING, m); |
129 return err; | 115 return err; |
130 } | 116 } |
131 | 117 |
132 // Notice logs a message using the LOG_NOTICE priority. | 118 // Notice logs a message using the LOG_NOTICE priority. |
133 func (l *syslog) Notice(m string) (err os.Error) { | 119 func (w *Writer) Notice(m string) (err os.Error) { |
134 » _, err = l.writeString(LOG_NOTICE, m); | 120 » _, err = w.writeString(LOG_NOTICE, m); |
135 return err; | 121 return err; |
136 } | 122 } |
137 // Info logs a message using the LOG_INFO priority. | 123 // Info logs a message using the LOG_INFO priority. |
138 func (l *syslog) Info(m string) (err os.Error) { | 124 func (w *Writer) Info(m string) (err os.Error) { |
139 » _, err = l.writeString(LOG_INFO, m); | 125 » _, err = w.writeString(LOG_INFO, m); |
140 return err; | 126 return err; |
141 } | 127 } |
142 // Debug logs a message using the LOG_DEBUG priority. | 128 // Debug logs a message using the LOG_DEBUG priority. |
143 func (l *syslog) Debug(m string) (err os.Error) { | 129 func (w *Writer) Debug(m string) (err os.Error) { |
144 » l.priority = LOG_DEBUG; | 130 » _, err = w.writeString(LOG_DEBUG, m); |
145 » _, err = io.WriteString(l, m); | |
146 return err; | 131 return err; |
147 } | 132 } |
148 | 133 |
149 // NewLogger provides an object that implements the full log.Logger interface, | 134 // NewLogger provides an object that implements the full log.Logger interface, |
150 // but sends messages to Syslog instead; flag is passed as is to Logger; | 135 // but sends messages to Syslog instead; flag is passed as is to Logger; |
151 // priority will be used for all messages sent using this interface. | 136 // priority will be used for all messages sent using this interface. |
152 // All messages are logged with priority p. | 137 // All messages are logged with priority p. |
153 func NewLogger(p Priority, flag int) *log.Logger { | 138 func NewLogger(p Priority, flag int) *log.Logger { |
154 s, err := New(p, ""); | 139 s, err := New(p, ""); |
155 if err != nil { | 140 if err != nil { |
156 return nil | 141 return nil |
157 } | 142 } |
158 return log.New(s, nil, "", flag); | 143 return log.New(s, nil, "", flag); |
159 } | 144 } |
LEFT | RIGHT |