/vendor/github.com/docker/docker/daemon/logger/jsonfilelog/jsonfilelog.go

https://bitbucket.org/gollariel/dockertool · Go · 157 lines · 129 code · 16 blank · 12 comment · 26 complexity · 8637500298206ce74fa3c36bd4e934bf MD5 · raw file

  1. // Package jsonfilelog provides the default Logger implementation for
  2. // Docker logging. This logger logs to files on the host server in the
  3. // JSON format.
  4. package jsonfilelog
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. "strconv"
  10. "sync"
  11. "github.com/Sirupsen/logrus"
  12. "github.com/docker/docker/daemon/logger"
  13. "github.com/docker/docker/daemon/logger/loggerutils"
  14. "github.com/docker/docker/pkg/jsonlog"
  15. "github.com/docker/go-units"
  16. )
  17. // Name is the name of the file that the jsonlogger logs to.
  18. const Name = "json-file"
  19. // JSONFileLogger is Logger implementation for default Docker logging.
  20. type JSONFileLogger struct {
  21. buf *bytes.Buffer
  22. writer *loggerutils.RotateFileWriter
  23. mu sync.Mutex
  24. readers map[*logger.LogWatcher]struct{} // stores the active log followers
  25. extra []byte // json-encoded extra attributes
  26. }
  27. func init() {
  28. if err := logger.RegisterLogDriver(Name, New); err != nil {
  29. logrus.Fatal(err)
  30. }
  31. if err := logger.RegisterLogOptValidator(Name, ValidateLogOpt); err != nil {
  32. logrus.Fatal(err)
  33. }
  34. }
  35. // New creates new JSONFileLogger which writes to filename passed in
  36. // on given context.
  37. func New(info logger.Info) (logger.Logger, error) {
  38. var capval int64 = -1
  39. if capacity, ok := info.Config["max-size"]; ok {
  40. var err error
  41. capval, err = units.FromHumanSize(capacity)
  42. if err != nil {
  43. return nil, err
  44. }
  45. }
  46. var maxFiles = 1
  47. if maxFileString, ok := info.Config["max-file"]; ok {
  48. var err error
  49. maxFiles, err = strconv.Atoi(maxFileString)
  50. if err != nil {
  51. return nil, err
  52. }
  53. if maxFiles < 1 {
  54. return nil, fmt.Errorf("max-file cannot be less than 1")
  55. }
  56. }
  57. writer, err := loggerutils.NewRotateFileWriter(info.LogPath, capval, maxFiles)
  58. if err != nil {
  59. return nil, err
  60. }
  61. var extra []byte
  62. attrs, err := info.ExtraAttributes(nil)
  63. if err != nil {
  64. return nil, err
  65. }
  66. if len(attrs) > 0 {
  67. var err error
  68. extra, err = json.Marshal(attrs)
  69. if err != nil {
  70. return nil, err
  71. }
  72. }
  73. return &JSONFileLogger{
  74. buf: bytes.NewBuffer(nil),
  75. writer: writer,
  76. readers: make(map[*logger.LogWatcher]struct{}),
  77. extra: extra,
  78. }, nil
  79. }
  80. // Log converts logger.Message to jsonlog.JSONLog and serializes it to file.
  81. func (l *JSONFileLogger) Log(msg *logger.Message) error {
  82. timestamp, err := jsonlog.FastTimeMarshalJSON(msg.Timestamp)
  83. if err != nil {
  84. return err
  85. }
  86. l.mu.Lock()
  87. logline := msg.Line
  88. if !msg.Partial {
  89. logline = append(msg.Line, '\n')
  90. }
  91. err = (&jsonlog.JSONLogs{
  92. Log: logline,
  93. Stream: msg.Source,
  94. Created: timestamp,
  95. RawAttrs: l.extra,
  96. }).MarshalJSONBuf(l.buf)
  97. logger.PutMessage(msg)
  98. if err != nil {
  99. l.mu.Unlock()
  100. return err
  101. }
  102. l.buf.WriteByte('\n')
  103. _, err = l.writer.Write(l.buf.Bytes())
  104. l.buf.Reset()
  105. l.mu.Unlock()
  106. return err
  107. }
  108. // ValidateLogOpt looks for json specific log options max-file & max-size.
  109. func ValidateLogOpt(cfg map[string]string) error {
  110. for key := range cfg {
  111. switch key {
  112. case "max-file":
  113. case "max-size":
  114. case "labels":
  115. case "env":
  116. case "env-regex":
  117. default:
  118. return fmt.Errorf("unknown log opt '%s' for json-file log driver", key)
  119. }
  120. }
  121. return nil
  122. }
  123. // LogPath returns the location the given json logger logs to.
  124. func (l *JSONFileLogger) LogPath() string {
  125. return l.writer.LogPath()
  126. }
  127. // Close closes underlying file and signals all readers to stop.
  128. func (l *JSONFileLogger) Close() error {
  129. l.mu.Lock()
  130. err := l.writer.Close()
  131. for r := range l.readers {
  132. r.Close()
  133. delete(l.readers, r)
  134. }
  135. l.mu.Unlock()
  136. return err
  137. }
  138. // Name returns name of this logger.
  139. func (l *JSONFileLogger) Name() string {
  140. return Name
  141. }