2014-09-13 04:40:09 +04:00
|
|
|
package logrus_papertrail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
format = "Jan 2 15:04:05"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PapertrailHook to send logs to a logging service compatible with the Papertrail API.
|
|
|
|
type PapertrailHook struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
AppName string
|
|
|
|
UDPConn net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPapertrailHook creates a hook to be added to an instance of logger.
|
|
|
|
func NewPapertrailHook(host string, port int, appName string) (*PapertrailHook, error) {
|
|
|
|
conn, err := net.Dial("udp", fmt.Sprintf("%s:%d", host, port))
|
|
|
|
return &PapertrailHook{host, port, appName, conn}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire is called when a log event is fired.
|
|
|
|
func (hook *PapertrailHook) Fire(entry *logrus.Entry) error {
|
|
|
|
date := time.Now().Format(format)
|
2014-09-14 00:03:15 +04:00
|
|
|
payload := fmt.Sprintf("<22> %s %s: [%s] %s", date, hook.AppName, entry.Data["level"], entry.Message)
|
2014-09-13 04:40:09 +04:00
|
|
|
|
|
|
|
line, err := entry.String()
|
2014-09-14 00:03:40 +04:00
|
|
|
bytesWritten, err := hook.UDPConn.Write([]byte(payload))
|
2014-09-13 04:40:09 +04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Unable to read entry, %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := fmt.Sprintf("<22> %s %s: [%s] %s", date, hook.AppName, entry.Data["level"], line)
|
|
|
|
|
|
|
|
_, err = hook.UDPConn.Write([]byte(payload))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Unable to send log line to Papertrail via UDP, %v", err)
|
2014-09-14 00:03:40 +04:00
|
|
|
fmt.Fprintf(os.Stderr, "Unable to send log line to Papertrail via UDP. Wrote %d bytes before error: %v", bytesWritten, err)
|
2014-09-13 04:40:09 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Levels returns the available logging levels.
|
|
|
|
func (hook *PapertrailHook) Levels() []logrus.Level {
|
|
|
|
return []logrus.Level{
|
2014-09-14 00:52:24 +04:00
|
|
|
logrus.PanicLevel,
|
|
|
|
logrus.FatalLevel,
|
|
|
|
logrus.ErrorLevel,
|
|
|
|
logrus.WarnLevel,
|
|
|
|
logrus.InfoLevel,
|
|
|
|
logrus.DebugLevel,
|
2014-09-13 04:40:09 +04:00
|
|
|
}
|
|
|
|
}
|