Docker-DCO-1.1-Signed-off-by: Erik Hollensbe <github@hollensbe.org> (github: erikh)
| ... | ... |
@@ -4,10 +4,11 @@ import ( |
| 4 | 4 |
"bytes" |
| 5 | 5 |
"encoding/json" |
| 6 | 6 |
"io" |
| 7 |
+ "log" |
|
| 7 | 8 |
"sync" |
| 8 | 9 |
"time" |
| 9 | 10 |
|
| 10 |
- "github.com/docker/docker/utils" |
|
| 11 |
+ "github.com/docker/docker/pkg/jsonlog" |
|
| 11 | 12 |
) |
| 12 | 13 |
|
| 13 | 14 |
// BroadcastWriter accumulate multiple io.WriteCloser by stream. |
| ... | ... |
@@ -19,7 +20,7 @@ type BroadcastWriter struct {
|
| 19 | 19 |
|
| 20 | 20 |
// AddWriter adds new io.WriteCloser for stream. |
| 21 | 21 |
// If stream is "", then all writes proceed as is. Otherwise every line from |
| 22 |
-// input will be packed to serialized utils.JSONLog. |
|
| 22 |
+// input will be packed to serialized jsonlog.JSONLog. |
|
| 23 | 23 |
func (w *BroadcastWriter) AddWriter(writer io.WriteCloser, stream string) {
|
| 24 | 24 |
w.Lock() |
| 25 | 25 |
if _, ok := w.streams[stream]; !ok {
|
| ... | ... |
@@ -53,9 +54,9 @@ func (w *BroadcastWriter) Write(p []byte) (n int, err error) {
|
| 53 | 53 |
if stream == "" {
|
| 54 | 54 |
continue |
| 55 | 55 |
} |
| 56 |
- b, err := json.Marshal(utils.JSONLog{Log: line, Stream: stream, Created: created})
|
|
| 56 |
+ b, err := json.Marshal(jsonlog.JSONLog{Log: line, Stream: stream, Created: created})
|
|
| 57 | 57 |
if err != nil {
|
| 58 |
- utils.Errorf("Error making JSON log line: %s", err)
|
|
| 58 |
+ log.Printf("Error making JSON log line: %s", err)
|
|
| 59 | 59 |
continue |
| 60 | 60 |
} |
| 61 | 61 |
b = append(b, '\n') |
| 62 | 62 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,45 @@ |
| 0 |
+package jsonlog |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "encoding/json" |
|
| 4 |
+ "fmt" |
|
| 5 |
+ "io" |
|
| 6 |
+ "log" |
|
| 7 |
+ "time" |
|
| 8 |
+) |
|
| 9 |
+ |
|
| 10 |
+type JSONLog struct {
|
|
| 11 |
+ Log string `json:"log,omitempty"` |
|
| 12 |
+ Stream string `json:"stream,omitempty"` |
|
| 13 |
+ Created time.Time `json:"time"` |
|
| 14 |
+} |
|
| 15 |
+ |
|
| 16 |
+func (jl *JSONLog) Format(format string) (string, error) {
|
|
| 17 |
+ if format == "" {
|
|
| 18 |
+ return jl.Log, nil |
|
| 19 |
+ } |
|
| 20 |
+ if format == "json" {
|
|
| 21 |
+ m, err := json.Marshal(jl) |
|
| 22 |
+ return string(m), err |
|
| 23 |
+ } |
|
| 24 |
+ return fmt.Sprintf("[%s] %s", jl.Created.Format(format), jl.Log), nil
|
|
| 25 |
+} |
|
| 26 |
+ |
|
| 27 |
+func WriteLog(src io.Reader, dst io.WriteCloser, format string) error {
|
|
| 28 |
+ dec := json.NewDecoder(src) |
|
| 29 |
+ for {
|
|
| 30 |
+ l := &JSONLog{}
|
|
| 31 |
+ |
|
| 32 |
+ if err := dec.Decode(l); err == io.EOF {
|
|
| 33 |
+ return nil |
|
| 34 |
+ } else if err != nil {
|
|
| 35 |
+ log.Printf("Error streaming logs: %s", err)
|
|
| 36 |
+ return err |
|
| 37 |
+ } |
|
| 38 |
+ line, err := l.Format(format) |
|
| 39 |
+ if err != nil {
|
|
| 40 |
+ return err |
|
| 41 |
+ } |
|
| 42 |
+ fmt.Fprintf(dst, "%s", line) |
|
| 43 |
+ } |
|
| 44 |
+} |
| ... | ... |
@@ -6,7 +6,6 @@ import ( |
| 6 | 6 |
"crypto/sha1" |
| 7 | 7 |
"crypto/sha256" |
| 8 | 8 |
"encoding/hex" |
| 9 |
- "encoding/json" |
|
| 10 | 9 |
"fmt" |
| 11 | 10 |
"io" |
| 12 | 11 |
"io/ioutil" |
| ... | ... |
@@ -19,7 +18,6 @@ import ( |
| 19 | 19 |
"strings" |
| 20 | 20 |
"sync" |
| 21 | 21 |
"syscall" |
| 22 |
- "time" |
|
| 23 | 22 |
|
| 24 | 23 |
"github.com/docker/docker/dockerversion" |
| 25 | 24 |
) |
| ... | ... |
@@ -264,42 +262,6 @@ func (r *bufReader) Close() error {
|
| 264 | 264 |
return closer.Close() |
| 265 | 265 |
} |
| 266 | 266 |
|
| 267 |
-type JSONLog struct {
|
|
| 268 |
- Log string `json:"log,omitempty"` |
|
| 269 |
- Stream string `json:"stream,omitempty"` |
|
| 270 |
- Created time.Time `json:"time"` |
|
| 271 |
-} |
|
| 272 |
- |
|
| 273 |
-func (jl *JSONLog) Format(format string) (string, error) {
|
|
| 274 |
- if format == "" {
|
|
| 275 |
- return jl.Log, nil |
|
| 276 |
- } |
|
| 277 |
- if format == "json" {
|
|
| 278 |
- m, err := json.Marshal(jl) |
|
| 279 |
- return string(m), err |
|
| 280 |
- } |
|
| 281 |
- return fmt.Sprintf("[%s] %s", jl.Created.Format(format), jl.Log), nil
|
|
| 282 |
-} |
|
| 283 |
- |
|
| 284 |
-func WriteLog(src io.Reader, dst io.WriteCloser, format string) error {
|
|
| 285 |
- dec := json.NewDecoder(src) |
|
| 286 |
- for {
|
|
| 287 |
- l := &JSONLog{}
|
|
| 288 |
- |
|
| 289 |
- if err := dec.Decode(l); err == io.EOF {
|
|
| 290 |
- return nil |
|
| 291 |
- } else if err != nil {
|
|
| 292 |
- Errorf("Error streaming logs: %s", err)
|
|
| 293 |
- return err |
|
| 294 |
- } |
|
| 295 |
- line, err := l.Format(format) |
|
| 296 |
- if err != nil {
|
|
| 297 |
- return err |
|
| 298 |
- } |
|
| 299 |
- fmt.Fprintf(dst, "%s", line) |
|
| 300 |
- } |
|
| 301 |
-} |
|
| 302 |
- |
|
| 303 | 267 |
func GetTotalUsedFds() int {
|
| 304 | 268 |
if fds, err := ioutil.ReadDir(fmt.Sprintf("/proc/%d/fd", os.Getpid())); err != nil {
|
| 305 | 269 |
Errorf("Error opening /proc/%d/fd: %s", os.Getpid(), err)
|