srslog.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package srslog
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. )
  9. // This interface allows us to work with both local and network connections,
  10. // and enables Solaris support (see syslog_unix.go).
  11. type serverConn interface {
  12. writeString(framer Framer, formatter Formatter, p Priority, hostname, tag, s string) error
  13. close() error
  14. }
  15. // New establishes a new connection to the system log daemon. Each
  16. // write to the returned Writer sends a log message with the given
  17. // priority and prefix.
  18. func New(priority Priority, tag string) (w *Writer, err error) {
  19. return Dial("", "", priority, tag)
  20. }
  21. // Dial establishes a connection to a log daemon by connecting to
  22. // address raddr on the specified network. Each write to the returned
  23. // Writer sends a log message with the given facility, severity and
  24. // tag.
  25. // If network is empty, Dial will connect to the local syslog server.
  26. func Dial(network, raddr string, priority Priority, tag string) (*Writer, error) {
  27. return DialWithTLSConfig(network, raddr, priority, tag, nil)
  28. }
  29. // DialWithTLSCertPath establishes a secure connection to a log daemon by connecting to
  30. // address raddr on the specified network. It uses certPath to load TLS certificates and configure
  31. // the secure connection.
  32. func DialWithTLSCertPath(network, raddr string, priority Priority, tag, certPath string) (*Writer, error) {
  33. serverCert, err := ioutil.ReadFile(certPath)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return DialWithTLSCert(network, raddr, priority, tag, serverCert)
  38. }
  39. // DialWIthTLSCert establishes a secure connection to a log daemon by connecting to
  40. // address raddr on the specified network. It uses serverCert to load a TLS certificate
  41. // and configure the secure connection.
  42. func DialWithTLSCert(network, raddr string, priority Priority, tag string, serverCert []byte) (*Writer, error) {
  43. pool := x509.NewCertPool()
  44. pool.AppendCertsFromPEM(serverCert)
  45. config := tls.Config{
  46. RootCAs: pool,
  47. }
  48. return DialWithTLSConfig(network, raddr, priority, tag, &config)
  49. }
  50. // DialWithTLSConfig establishes a secure connection to a log daemon by connecting to
  51. // address raddr on the specified network. It uses tlsConfig to configure the secure connection.
  52. func DialWithTLSConfig(network, raddr string, priority Priority, tag string, tlsConfig *tls.Config) (*Writer, error) {
  53. if err := validatePriority(priority); err != nil {
  54. return nil, err
  55. }
  56. if tag == "" {
  57. tag = os.Args[0]
  58. }
  59. hostname, _ := os.Hostname()
  60. w := &Writer{
  61. priority: priority,
  62. tag: tag,
  63. hostname: hostname,
  64. network: network,
  65. raddr: raddr,
  66. tlsConfig: tlsConfig,
  67. }
  68. w.Lock()
  69. defer w.Unlock()
  70. err := w.connect()
  71. if err != nil {
  72. return nil, err
  73. }
  74. return w, err
  75. }
  76. // NewLogger creates a log.Logger whose output is written to
  77. // the system log service with the specified priority. The logFlag
  78. // argument is the flag set passed through to log.New to create
  79. // the Logger.
  80. func NewLogger(p Priority, logFlag int) (*log.Logger, error) {
  81. s, err := New(p, "")
  82. if err != nil {
  83. return nil, err
  84. }
  85. return log.New(s, "", logFlag), nil
  86. }