srslog_unix.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package srslog
  2. import (
  3. "errors"
  4. "io"
  5. "net"
  6. )
  7. // unixSyslog opens a connection to the syslog daemon running on the
  8. // local machine using a Unix domain socket. This function exists because of
  9. // Solaris support as implemented by gccgo. On Solaris you can not
  10. // simply open a TCP connection to the syslog daemon. The gccgo
  11. // sources have a syslog_solaris.go file that implements unixSyslog to
  12. // return a type that satisfies the serverConn interface and simply calls the C
  13. // library syslog function.
  14. func unixSyslog() (conn serverConn, err error) {
  15. logTypes := []string{"unixgram", "unix"}
  16. logPaths := []string{"/dev/log", "/var/run/syslog", "/var/run/log"}
  17. for _, network := range logTypes {
  18. for _, path := range logPaths {
  19. conn, err := net.Dial(network, path)
  20. if err != nil {
  21. continue
  22. } else {
  23. return &localConn{conn: conn}, nil
  24. }
  25. }
  26. }
  27. return nil, errors.New("Unix syslog delivery error")
  28. }
  29. // localConn adheres to the serverConn interface, allowing us to send syslog
  30. // messages to the local syslog daemon over a Unix domain socket.
  31. type localConn struct {
  32. conn io.WriteCloser
  33. }
  34. // writeString formats syslog messages using time.Stamp instead of time.RFC3339,
  35. // and omits the hostname (because it is expected to be used locally).
  36. func (n *localConn) writeString(framer Framer, formatter Formatter, p Priority, hostname, tag, msg string) error {
  37. if framer == nil {
  38. framer = DefaultFramer
  39. }
  40. if formatter == nil {
  41. formatter = UnixFormatter
  42. }
  43. _, err := n.conn.Write([]byte(framer(formatter(p, hostname, tag, msg))))
  44. return err
  45. }
  46. // close the (local) network connection
  47. func (n *localConn) close() error {
  48. return n.conn.Close()
  49. }