| | |
| | |
| | |
| |
|
| | |
| |
|
| | package syslog |
| |
|
| | import ( |
| | "errors" |
| | "fmt" |
| | "log" |
| | "net" |
| | "os" |
| | "strings" |
| | "sync" |
| | "time" |
| | ) |
| |
|
| | |
| | |
| | |
| | |
| | type Priority int |
| |
|
| | const severityMask = 0x07 |
| | const facilityMask = 0xf8 |
| |
|
| | const ( |
| | |
| |
|
| | |
| | |
| | LOG_EMERG Priority = iota |
| | LOG_ALERT |
| | LOG_CRIT |
| | LOG_ERR |
| | LOG_WARNING |
| | LOG_NOTICE |
| | LOG_INFO |
| | LOG_DEBUG |
| | ) |
| |
|
| | const ( |
| | |
| |
|
| | |
| | |
| | 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 struct { |
| | priority Priority |
| | tag string |
| | hostname string |
| | network string |
| | raddr string |
| |
|
| | mu sync.Mutex |
| | conn serverConn |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | type serverConn interface { |
| | writeString(p Priority, hostname, tag, s, nl string) error |
| | close() error |
| | } |
| |
|
| | type netConn struct { |
| | local bool |
| | conn net.Conn |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | func New(priority Priority, tag string) (*Writer, error) { |
| | return Dial("", "", priority, tag) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func Dial(network, raddr string, priority Priority, tag string) (*Writer, error) { |
| | if priority < 0 || priority > LOG_LOCAL7|LOG_DEBUG { |
| | return nil, errors.New("log/syslog: invalid priority") |
| | } |
| |
|
| | if tag == "" { |
| | tag = os.Args[0] |
| | } |
| | hostname, _ := os.Hostname() |
| |
|
| | w := &Writer{ |
| | priority: priority, |
| | tag: tag, |
| | hostname: hostname, |
| | network: network, |
| | raddr: raddr, |
| | } |
| |
|
| | w.mu.Lock() |
| | defer w.mu.Unlock() |
| |
|
| | err := w.connect() |
| | if err != nil { |
| | return nil, err |
| | } |
| | return w, nil |
| | } |
| |
|
| | |
| | |
| | func (w *Writer) connect() (err error) { |
| | if w.conn != nil { |
| | |
| | w.conn.close() |
| | w.conn = nil |
| | } |
| |
|
| | if w.network == "" { |
| | w.conn, err = unixSyslog() |
| | if w.hostname == "" { |
| | w.hostname = "localhost" |
| | } |
| | } else { |
| | var c net.Conn |
| | c, err = net.Dial(w.network, w.raddr) |
| | if err == nil { |
| | w.conn = &netConn{ |
| | conn: c, |
| | local: w.network == "unixgram" || w.network == "unix", |
| | } |
| | if w.hostname == "" { |
| | w.hostname = c.LocalAddr().String() |
| | } |
| | } |
| | } |
| | return |
| | } |
| |
|
| | |
| | func (w *Writer) Write(b []byte) (int, error) { |
| | return w.writeAndRetry(w.priority, string(b)) |
| | } |
| |
|
| | |
| | func (w *Writer) Close() error { |
| | w.mu.Lock() |
| | defer w.mu.Unlock() |
| |
|
| | if w.conn != nil { |
| | err := w.conn.close() |
| | w.conn = nil |
| | return err |
| | } |
| | return nil |
| | } |
| |
|
| | |
| | |
| | func (w *Writer) Emerg(m string) error { |
| | _, err := w.writeAndRetry(LOG_EMERG, m) |
| | return err |
| | } |
| |
|
| | |
| | |
| | func (w *Writer) Alert(m string) error { |
| | _, err := w.writeAndRetry(LOG_ALERT, m) |
| | return err |
| | } |
| |
|
| | |
| | |
| | func (w *Writer) Crit(m string) error { |
| | _, err := w.writeAndRetry(LOG_CRIT, m) |
| | return err |
| | } |
| |
|
| | |
| | |
| | func (w *Writer) Err(m string) error { |
| | _, err := w.writeAndRetry(LOG_ERR, m) |
| | return err |
| | } |
| |
|
| | |
| | |
| | func (w *Writer) Warning(m string) error { |
| | _, err := w.writeAndRetry(LOG_WARNING, m) |
| | return err |
| | } |
| |
|
| | |
| | |
| | func (w *Writer) Notice(m string) error { |
| | _, err := w.writeAndRetry(LOG_NOTICE, m) |
| | return err |
| | } |
| |
|
| | |
| | |
| | func (w *Writer) Info(m string) error { |
| | _, err := w.writeAndRetry(LOG_INFO, m) |
| | return err |
| | } |
| |
|
| | |
| | |
| | func (w *Writer) Debug(m string) error { |
| | _, err := w.writeAndRetry(LOG_DEBUG, m) |
| | return err |
| | } |
| |
|
| | func (w *Writer) writeAndRetry(p Priority, s string) (int, error) { |
| | pr := (w.priority & facilityMask) | (p & severityMask) |
| |
|
| | w.mu.Lock() |
| | defer w.mu.Unlock() |
| |
|
| | if w.conn != nil { |
| | if n, err := w.write(pr, s); err == nil { |
| | return n, nil |
| | } |
| | } |
| | if err := w.connect(); err != nil { |
| | return 0, err |
| | } |
| | return w.write(pr, s) |
| | } |
| |
|
| | |
| | |
| | func (w *Writer) write(p Priority, msg string) (int, error) { |
| | |
| | nl := "" |
| | if !strings.HasSuffix(msg, "\n") { |
| | nl = "\n" |
| | } |
| |
|
| | err := w.conn.writeString(p, w.hostname, w.tag, msg, nl) |
| | if err != nil { |
| | return 0, err |
| | } |
| | |
| | |
| | |
| | return len(msg), nil |
| | } |
| |
|
| | func (n *netConn) writeString(p Priority, hostname, tag, msg, nl string) error { |
| | if n.local { |
| | |
| | |
| | |
| | timestamp := time.Now().Format(time.Stamp) |
| | _, err := fmt.Fprintf(n.conn, "<%d>%s %s[%d]: %s%s", |
| | p, timestamp, |
| | tag, os.Getpid(), msg, nl) |
| | return err |
| | } |
| | timestamp := time.Now().Format(time.RFC3339) |
| | _, err := fmt.Fprintf(n.conn, "<%d>%s %s %s[%d]: %s%s", |
| | p, timestamp, hostname, |
| | tag, os.Getpid(), msg, nl) |
| | return err |
| | } |
| |
|
| | func (n *netConn) close() error { |
| | return n.conn.Close() |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | func NewLogger(p Priority, logFlag int) (*log.Logger, error) { |
| | s, err := New(p, "") |
| | if err != nil { |
| | return nil, err |
| | } |
| | return log.New(s, "", logFlag), nil |
| | } |
| |
|