forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils.Debugf -> log.Printf, move jsonlog to own package.
Docker-DCO-1.1-Signed-off-by: Erik Hollensbe <[email protected]> (github: erikh)
- Loading branch information
Erik Hollensbe
committed
Aug 6, 2014
1 parent
5ab09f2
commit 5cdb9c8
Showing
3 changed files
with
50 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package jsonlog | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"time" | ||
) | ||
|
||
type JSONLog struct { | ||
Log string `json:"log,omitempty"` | ||
Stream string `json:"stream,omitempty"` | ||
Created time.Time `json:"time"` | ||
} | ||
|
||
func (jl *JSONLog) Format(format string) (string, error) { | ||
if format == "" { | ||
return jl.Log, nil | ||
} | ||
if format == "json" { | ||
m, err := json.Marshal(jl) | ||
return string(m), err | ||
} | ||
return fmt.Sprintf("[%s] %s", jl.Created.Format(format), jl.Log), nil | ||
} | ||
|
||
func WriteLog(src io.Reader, dst io.WriteCloser, format string) error { | ||
dec := json.NewDecoder(src) | ||
for { | ||
l := &JSONLog{} | ||
|
||
if err := dec.Decode(l); err == io.EOF { | ||
return nil | ||
} else if err != nil { | ||
log.Printf("Error streaming logs: %s", err) | ||
return err | ||
} | ||
line, err := l.Format(format) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintf(dst, "%s", line) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters