logentries.go 2.6 KB

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