2014-07-31 21:03:21 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2014-10-30 19:33:26 +00:00
|
|
|
"sync"
|
2014-07-31 21:03:21 +00:00
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-08-06 23:45:04 +00:00
|
|
|
"github.com/docker/docker/pkg/jsonlog"
|
2015-04-11 21:49:14 +00:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2014-09-15 18:44:58 +00:00
|
|
|
"github.com/docker/docker/pkg/tailfile"
|
|
|
|
"github.com/docker/docker/pkg/timeutils"
|
2014-07-31 21:03:21 +00:00
|
|
|
)
|
|
|
|
|
2015-04-11 21:49:14 +00:00
|
|
|
type ContainerLogsConfig struct {
|
|
|
|
Follow, Timestamps bool
|
|
|
|
Tail string
|
|
|
|
UseStdout, UseStderr bool
|
|
|
|
OutStream io.Writer
|
|
|
|
}
|
2014-07-31 21:03:21 +00:00
|
|
|
|
2015-04-11 21:49:14 +00:00
|
|
|
func (daemon *Daemon) ContainerLogs(name string, config *ContainerLogsConfig) error {
|
2014-07-31 21:03:21 +00:00
|
|
|
var (
|
|
|
|
lines = -1
|
|
|
|
format string
|
|
|
|
)
|
2015-04-11 21:49:14 +00:00
|
|
|
if !(config.UseStdout || config.UseStderr) {
|
2015-03-25 07:44:12 +00:00
|
|
|
return fmt.Errorf("You must choose at least one stream")
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
2015-04-11 21:49:14 +00:00
|
|
|
if config.Timestamps {
|
2014-09-15 18:44:58 +00:00
|
|
|
format = timeutils.RFC3339NanoFixed
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
2015-04-11 21:49:14 +00:00
|
|
|
if config.Tail == "" {
|
|
|
|
config.Tail = "all"
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
2015-04-11 21:49:14 +00:00
|
|
|
|
2014-12-16 23:06:35 +00:00
|
|
|
container, err := daemon.Get(name)
|
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
2015-04-11 21:49:14 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
outStream = config.OutStream
|
|
|
|
errStream io.Writer
|
|
|
|
)
|
|
|
|
if !container.Config.Tty {
|
|
|
|
errStream = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
|
|
|
|
outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
|
|
|
|
} else {
|
|
|
|
errStream = outStream
|
|
|
|
}
|
|
|
|
|
2015-02-06 00:24:47 +00:00
|
|
|
if container.LogDriverType() != "json-file" {
|
2015-03-25 07:44:12 +00:00
|
|
|
return fmt.Errorf("\"logs\" endpoint is supported only for \"json-file\" logging driver")
|
2015-02-06 00:24:47 +00:00
|
|
|
}
|
2014-07-31 21:03:21 +00:00
|
|
|
cLog, err := container.ReadLog("json")
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
// Legacy logs
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Old logs format")
|
2015-04-11 21:49:14 +00:00
|
|
|
if config.UseStdout {
|
2014-07-31 21:03:21 +00:00
|
|
|
cLog, err := container.ReadLog("stdout")
|
|
|
|
if err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("Error reading logs (stdout): %s", err)
|
2015-04-11 21:49:14 +00:00
|
|
|
} else if _, err := io.Copy(outStream, cLog); err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("Error streaming logs (stdout): %s", err)
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-11 21:49:14 +00:00
|
|
|
if config.UseStderr {
|
2014-07-31 21:03:21 +00:00
|
|
|
cLog, err := container.ReadLog("stderr")
|
|
|
|
if err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("Error reading logs (stderr): %s", err)
|
2015-04-11 21:49:14 +00:00
|
|
|
} else if _, err := io.Copy(errStream, cLog); err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("Error streaming logs (stderr): %s", err)
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("Error reading logs (json): %s", err)
|
2014-07-31 21:03:21 +00:00
|
|
|
} else {
|
2015-04-11 21:49:14 +00:00
|
|
|
if config.Tail != "all" {
|
2014-07-31 21:03:21 +00:00
|
|
|
var err error
|
2015-04-11 21:49:14 +00:00
|
|
|
lines, err = strconv.Atoi(config.Tail)
|
2014-07-31 21:03:21 +00:00
|
|
|
if err != nil {
|
2015-04-11 21:49:14 +00:00
|
|
|
logrus.Errorf("Failed to parse tail %s, error: %v, show all logs", config.Tail, err)
|
2014-07-31 21:03:21 +00:00
|
|
|
lines = -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if lines != 0 {
|
|
|
|
if lines > 0 {
|
|
|
|
f := cLog.(*os.File)
|
|
|
|
ls, err := tailfile.TailFile(f, lines)
|
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
|
|
|
tmp := bytes.NewBuffer([]byte{})
|
|
|
|
for _, l := range ls {
|
|
|
|
fmt.Fprintf(tmp, "%s\n", l)
|
|
|
|
}
|
|
|
|
cLog = tmp
|
|
|
|
}
|
|
|
|
dec := json.NewDecoder(cLog)
|
2014-09-18 13:29:19 +00:00
|
|
|
l := &jsonlog.JSONLog{}
|
2014-07-31 21:03:21 +00:00
|
|
|
for {
|
|
|
|
if err := dec.Decode(l); err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("Error streaming logs: %s", err)
|
2014-07-31 21:03:21 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
logLine := l.Log
|
2015-04-11 21:49:14 +00:00
|
|
|
if config.Timestamps {
|
2015-01-28 22:33:15 +00:00
|
|
|
// format can be "" or time format, so here can't be error
|
|
|
|
logLine, _ = l.Format(format)
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
2015-04-11 21:49:14 +00:00
|
|
|
if l.Stream == "stdout" && config.UseStdout {
|
|
|
|
io.WriteString(outStream, logLine)
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
2015-04-11 21:49:14 +00:00
|
|
|
if l.Stream == "stderr" && config.UseStderr {
|
|
|
|
io.WriteString(errStream, logLine)
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
2014-09-18 13:29:19 +00:00
|
|
|
l.Reset()
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-11 21:49:14 +00:00
|
|
|
if config.Follow && container.IsRunning() {
|
2014-07-31 21:03:21 +00:00
|
|
|
errors := make(chan error, 2)
|
2014-10-30 19:33:26 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
2015-04-11 21:49:14 +00:00
|
|
|
if config.UseStdout {
|
2014-10-30 19:33:26 +00:00
|
|
|
wg.Add(1)
|
2014-07-31 21:03:21 +00:00
|
|
|
stdoutPipe := container.StdoutLogPipe()
|
2014-09-22 06:55:46 +00:00
|
|
|
defer stdoutPipe.Close()
|
2014-07-31 21:03:21 +00:00
|
|
|
go func() {
|
2015-04-11 21:49:14 +00:00
|
|
|
errors <- jsonlog.WriteLog(stdoutPipe, outStream, format)
|
2014-10-30 19:33:26 +00:00
|
|
|
wg.Done()
|
2014-07-31 21:03:21 +00:00
|
|
|
}()
|
|
|
|
}
|
2015-04-11 21:49:14 +00:00
|
|
|
if config.UseStderr {
|
2014-10-30 19:33:26 +00:00
|
|
|
wg.Add(1)
|
2014-07-31 21:03:21 +00:00
|
|
|
stderrPipe := container.StderrLogPipe()
|
2014-09-22 06:55:46 +00:00
|
|
|
defer stderrPipe.Close()
|
2014-07-31 21:03:21 +00:00
|
|
|
go func() {
|
2015-04-11 21:49:14 +00:00
|
|
|
errors <- jsonlog.WriteLog(stderrPipe, errStream, format)
|
2014-10-30 19:33:26 +00:00
|
|
|
wg.Done()
|
2014-07-31 21:03:21 +00:00
|
|
|
}()
|
|
|
|
}
|
2014-10-30 19:33:26 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
close(errors)
|
|
|
|
|
|
|
|
for err := range errors {
|
|
|
|
if err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("%s", err)
|
2014-10-30 19:33:26 +00:00
|
|
|
}
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
2014-10-30 19:33:26 +00:00
|
|
|
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|