2014-07-31 17:03:21 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/docker/docker/engine"
|
2014-08-06 19:45:04 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonlog"
|
2014-09-15 14:44:58 -04:00
|
|
|
"github.com/docker/docker/pkg/log"
|
|
|
|
"github.com/docker/docker/pkg/tailfile"
|
|
|
|
"github.com/docker/docker/pkg/timeutils"
|
2014-07-31 17:03:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (daemon *Daemon) ContainerLogs(job *engine.Job) engine.Status {
|
|
|
|
if len(job.Args) != 1 {
|
|
|
|
return job.Errorf("Usage: %s CONTAINER\n", job.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
name = job.Args[0]
|
|
|
|
stdout = job.GetenvBool("stdout")
|
|
|
|
stderr = job.GetenvBool("stderr")
|
|
|
|
tail = job.Getenv("tail")
|
|
|
|
follow = job.GetenvBool("follow")
|
|
|
|
times = job.GetenvBool("timestamps")
|
|
|
|
lines = -1
|
|
|
|
format string
|
|
|
|
)
|
|
|
|
if !(stdout || stderr) {
|
|
|
|
return job.Errorf("You must choose at least one stream")
|
|
|
|
}
|
|
|
|
if times {
|
2014-09-15 14:44:58 -04:00
|
|
|
format = timeutils.RFC3339NanoFixed
|
2014-07-31 17:03:21 -04:00
|
|
|
}
|
|
|
|
if tail == "" {
|
|
|
|
tail = "all"
|
|
|
|
}
|
|
|
|
container := daemon.Get(name)
|
|
|
|
if container == nil {
|
|
|
|
return job.Errorf("No such container: %s", name)
|
|
|
|
}
|
|
|
|
cLog, err := container.ReadLog("json")
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
// Legacy logs
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("Old logs format")
|
2014-07-31 17:03:21 -04:00
|
|
|
if stdout {
|
|
|
|
cLog, err := container.ReadLog("stdout")
|
|
|
|
if err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("Error reading logs (stdout): %s", err)
|
2014-07-31 17:03:21 -04:00
|
|
|
} else if _, err := io.Copy(job.Stdout, cLog); err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("Error streaming logs (stdout): %s", err)
|
2014-07-31 17:03:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if stderr {
|
|
|
|
cLog, err := container.ReadLog("stderr")
|
|
|
|
if err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("Error reading logs (stderr): %s", err)
|
2014-07-31 17:03:21 -04:00
|
|
|
} else if _, err := io.Copy(job.Stderr, cLog); err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("Error streaming logs (stderr): %s", err)
|
2014-07-31 17:03:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("Error reading logs (json): %s", err)
|
2014-07-31 17:03:21 -04:00
|
|
|
} else {
|
|
|
|
if tail != "all" {
|
|
|
|
var err error
|
|
|
|
lines, err = strconv.Atoi(tail)
|
|
|
|
if err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("Failed to parse tail %s, error: %v, show all logs", tail, err)
|
2014-07-31 17:03:21 -04:00
|
|
|
lines = -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if lines != 0 {
|
|
|
|
if lines > 0 {
|
|
|
|
f := cLog.(*os.File)
|
|
|
|
ls, err := tailfile.TailFile(f, lines)
|
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
tmp := bytes.NewBuffer([]byte{})
|
|
|
|
for _, l := range ls {
|
|
|
|
fmt.Fprintf(tmp, "%s\n", l)
|
|
|
|
}
|
|
|
|
cLog = tmp
|
|
|
|
}
|
|
|
|
dec := json.NewDecoder(cLog)
|
|
|
|
for {
|
2014-08-06 19:45:04 -04:00
|
|
|
l := &jsonlog.JSONLog{}
|
2014-07-31 17:03:21 -04:00
|
|
|
|
|
|
|
if err := dec.Decode(l); err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("Error streaming logs: %s", err)
|
2014-07-31 17:03:21 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
logLine := l.Log
|
|
|
|
if times {
|
|
|
|
logLine = fmt.Sprintf("%s %s", l.Created.Format(format), logLine)
|
|
|
|
}
|
|
|
|
if l.Stream == "stdout" && stdout {
|
|
|
|
fmt.Fprintf(job.Stdout, "%s", logLine)
|
|
|
|
}
|
|
|
|
if l.Stream == "stderr" && stderr {
|
|
|
|
fmt.Fprintf(job.Stderr, "%s", logLine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-31 11:20:35 -04:00
|
|
|
if follow && container.IsRunning() {
|
2014-07-31 17:03:21 -04:00
|
|
|
errors := make(chan error, 2)
|
|
|
|
if stdout {
|
|
|
|
stdoutPipe := container.StdoutLogPipe()
|
|
|
|
go func() {
|
2014-08-06 19:45:04 -04:00
|
|
|
errors <- jsonlog.WriteLog(stdoutPipe, job.Stdout, format)
|
2014-07-31 17:03:21 -04:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
if stderr {
|
|
|
|
stderrPipe := container.StderrLogPipe()
|
|
|
|
go func() {
|
2014-08-06 19:45:04 -04:00
|
|
|
errors <- jsonlog.WriteLog(stderrPipe, job.Stderr, format)
|
2014-07-31 17:03:21 -04:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
err := <-errors
|
|
|
|
if err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("%s", err)
|
2014-07-31 17:03:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return engine.StatusOK
|
|
|
|
}
|