Add basic logging to file support

This currently logs to a json file with the stream type.  This is slow
and hard on the cpu and memory so we need to swich this over to
something like protobufs for the binary logs but this is just a start.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2015-12-10 17:07:21 -08:00
parent 8469b6d6a4
commit e5545a1461
12 changed files with 278 additions and 58 deletions

60
ctr/logs.go Normal file
View file

@ -0,0 +1,60 @@
package main
import (
"encoding/json"
"io"
"os"
"time"
"github.com/codegangsta/cli"
"github.com/docker/containerd"
)
var LogsCommand = cli.Command{
Name: "logs",
Usage: "view binary container logs generated by containerd",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "follow,f",
Usage: "follow/tail the logs",
},
},
Action: func(context *cli.Context) {
path := context.Args().First()
if path == "" {
fatal("path to the log cannot be empty", 1)
}
if err := readLogs(path, context.Bool("follow")); err != nil {
fatal(err.Error(), 1)
}
},
}
func readLogs(path string, follow bool) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
dec := json.NewDecoder(f)
for {
var msg *containerd.Message
if err := dec.Decode(&msg); err != nil {
if err == io.EOF {
if follow {
time.Sleep(100 * time.Millisecond)
continue
}
return nil
}
return err
}
switch msg.Stream {
case "stdout":
os.Stdout.Write(msg.Data)
case "stderr":
os.Stderr.Write(msg.Data)
}
}
return nil
}

View file

@ -34,9 +34,10 @@ func main() {
},
}
app.Commands = []cli.Command{
ContainersCommand,
CheckpointCommand,
ContainersCommand,
EventsCommand,
LogsCommand,
}
app.Before = func(context *cli.Context) error {
if context.GlobalBool("debug") {