logentries.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Package logentries provides the log driver for forwarding server logs
  2. // to logentries endpoints.
  3. package logentries
  4. import (
  5. "fmt"
  6. "github.com/Sirupsen/logrus"
  7. "github.com/bsphere/le_go"
  8. "github.com/docker/docker/daemon/logger"
  9. )
  10. type logentries struct {
  11. tag string
  12. containerID string
  13. containerName string
  14. writer *le_go.Logger
  15. extra map[string]string
  16. }
  17. const (
  18. name = "logentries"
  19. token = "logentries-token"
  20. )
  21. func init() {
  22. if err := logger.RegisterLogDriver(name, New); err != nil {
  23. logrus.Fatal(err)
  24. }
  25. if err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {
  26. logrus.Fatal(err)
  27. }
  28. }
  29. // New creates a logentries logger using the configuration passed in on
  30. // the context. The supported context configuration variable is
  31. // logentries-token.
  32. func New(info logger.Info) (logger.Logger, error) {
  33. logrus.WithField("container", info.ContainerID).
  34. WithField("token", info.Config[token]).
  35. Debug("logging driver logentries configured")
  36. log, err := le_go.Connect(info.Config[token])
  37. if err != nil {
  38. return nil, err
  39. }
  40. return &logentries{
  41. containerID: info.ContainerID,
  42. containerName: info.ContainerName,
  43. writer: log,
  44. }, nil
  45. }
  46. func (f *logentries) Log(msg *logger.Message) error {
  47. data := map[string]string{
  48. "container_id": f.containerID,
  49. "container_name": f.containerName,
  50. "source": msg.Source,
  51. "log": string(msg.Line),
  52. }
  53. for k, v := range f.extra {
  54. data[k] = v
  55. }
  56. ts := msg.Timestamp
  57. logger.PutMessage(msg)
  58. f.writer.Println(f.tag, ts, data)
  59. return nil
  60. }
  61. func (f *logentries) Close() error {
  62. return f.writer.Close()
  63. }
  64. func (f *logentries) Name() string {
  65. return name
  66. }
  67. // ValidateLogOpt looks for logentries specific log option logentries-address.
  68. func ValidateLogOpt(cfg map[string]string) error {
  69. for key := range cfg {
  70. switch key {
  71. case "env":
  72. case "env-regex":
  73. case "labels":
  74. case "tag":
  75. case key:
  76. default:
  77. return fmt.Errorf("unknown log opt '%s' for logentries log driver", key)
  78. }
  79. }
  80. if cfg[token] == "" {
  81. return fmt.Errorf("Missing logentries token")
  82. }
  83. return nil
  84. }