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:
60
ctr/logs.go
Normal file
60
ctr/logs.go
Normal 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
|
||||
}
|
||||
@@ -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") {
|
||||
|
||||
Reference in New Issue
Block a user