summaryrefslogtreecommitdiff
path: root/libpod
diff options
context:
space:
mode:
Diffstat (limited to 'libpod')
-rw-r--r--libpod/container_log.go14
-rw-r--r--libpod/container_log_linux.go3
-rw-r--r--libpod/container_log_unsupported.go2
-rw-r--r--libpod/logs/log.go33
-rw-r--r--libpod/oci_conmon_linux.go2
5 files changed, 44 insertions, 10 deletions
diff --git a/libpod/container_log.go b/libpod/container_log.go
index dd5c36e26..7a9eb2dbf 100644
--- a/libpod/container_log.go
+++ b/libpod/container_log.go
@@ -23,8 +23,8 @@ func init() {
// Log is a runtime function that can read one or more container logs.
func (r *Runtime) Log(ctx context.Context, containers []*Container, options *logs.LogOptions, logChannel chan *logs.LogLine) error {
- for _, ctr := range containers {
- if err := ctr.ReadLog(ctx, options, logChannel); err != nil {
+ for c, ctr := range containers {
+ if err := ctr.ReadLog(ctx, options, logChannel, int64(c)); err != nil {
return err
}
}
@@ -32,26 +32,26 @@ func (r *Runtime) Log(ctx context.Context, containers []*Container, options *log
}
// ReadLog reads a containers log based on the input options and returns log lines over a channel.
-func (c *Container) ReadLog(ctx context.Context, options *logs.LogOptions, logChannel chan *logs.LogLine) error {
+func (c *Container) ReadLog(ctx context.Context, options *logs.LogOptions, logChannel chan *logs.LogLine, colorID int64) error {
switch c.LogDriver() {
case define.PassthroughLogging:
return errors.Wrapf(define.ErrNoLogs, "this container is using the 'passthrough' log driver, cannot read logs")
case define.NoLogging:
return errors.Wrapf(define.ErrNoLogs, "this container is using the 'none' log driver, cannot read logs")
case define.JournaldLogging:
- return c.readFromJournal(ctx, options, logChannel)
+ return c.readFromJournal(ctx, options, logChannel, colorID)
case define.JSONLogging:
// TODO provide a separate implementation of this when Conmon
// has support.
fallthrough
case define.KubernetesLogging, "":
- return c.readFromLogFile(ctx, options, logChannel)
+ return c.readFromLogFile(ctx, options, logChannel, colorID)
default:
return errors.Wrapf(define.ErrInternal, "unrecognized log driver %q, cannot read logs", c.LogDriver())
}
}
-func (c *Container) readFromLogFile(ctx context.Context, options *logs.LogOptions, logChannel chan *logs.LogLine) error {
+func (c *Container) readFromLogFile(ctx context.Context, options *logs.LogOptions, logChannel chan *logs.LogLine, colorID int64) error {
t, tailLog, err := logs.GetLogFile(c.LogPath(), options)
if err != nil {
// If the log file does not exist, this is not fatal.
@@ -65,6 +65,7 @@ func (c *Container) readFromLogFile(ctx context.Context, options *logs.LogOption
for _, nll := range tailLog {
nll.CID = c.ID()
nll.CName = c.Name()
+ nll.ColorID = colorID
if nll.Since(options.Since) && nll.Until(options.Until) {
logChannel <- nll
}
@@ -97,6 +98,7 @@ func (c *Container) readFromLogFile(ctx context.Context, options *logs.LogOption
}
nll.CID = c.ID()
nll.CName = c.Name()
+ nll.ColorID = colorID
if nll.Since(options.Since) && nll.Until(options.Until) {
logChannel <- nll
}
diff --git a/libpod/container_log_linux.go b/libpod/container_log_linux.go
index 8ae8ff2c0..d96647e51 100644
--- a/libpod/container_log_linux.go
+++ b/libpod/container_log_linux.go
@@ -45,7 +45,7 @@ func (c *Container) initializeJournal(ctx context.Context) error {
return journal.Send("", journal.PriInfo, m)
}
-func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOptions, logChannel chan *logs.LogLine) error {
+func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOptions, logChannel chan *logs.LogLine, colorID int64) error {
// We need the container's events in the same journal to guarantee
// consistency, see #10323.
if options.Follow && c.runtime.config.Engine.EventsLogger != "journald" {
@@ -231,6 +231,7 @@ func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOption
}
logLine, err := logs.NewJournaldLogLine(message, options.Multi)
+ logLine.ColorID = colorID
if err != nil {
logrus.Errorf("Failed parse log line: %v", err)
return
diff --git a/libpod/container_log_unsupported.go b/libpod/container_log_unsupported.go
index 4f50f9f4c..c84a578cc 100644
--- a/libpod/container_log_unsupported.go
+++ b/libpod/container_log_unsupported.go
@@ -11,7 +11,7 @@ import (
"github.com/pkg/errors"
)
-func (c *Container) readFromJournal(_ context.Context, _ *logs.LogOptions, _ chan *logs.LogLine) error {
+func (c *Container) readFromJournal(_ context.Context, _ *logs.LogOptions, _ chan *logs.LogLine, colorID int64) error {
return errors.Wrapf(define.ErrOSNotSupported, "Journald logging only enabled with systemd on linux")
}
diff --git a/libpod/logs/log.go b/libpod/logs/log.go
index 9672f6ee1..0eb3bb922 100644
--- a/libpod/logs/log.go
+++ b/libpod/logs/log.go
@@ -27,6 +27,9 @@ const (
// FullLogType signifies a log line is full
FullLogType = "F"
+
+ //ANSIEscapeResetCode is a code that resets all colors and text effects
+ ANSIEscapeResetCode = "\033[0m"
)
// LogOptions is the options you can use for logs
@@ -37,6 +40,7 @@ type LogOptions struct {
Until time.Time
Tail int64
Timestamps bool
+ Colors bool
Multi bool
WaitGroup *sync.WaitGroup
UseName bool
@@ -50,6 +54,7 @@ type LogLine struct {
Msg string
CID string
CName string
+ ColorID int64
}
// GetLogFile returns an hp tail for a container given options
@@ -162,6 +167,24 @@ func getTailLog(path string, tail int) ([]*LogLine, error) {
return tailLog, nil
}
+//getColor returns a ANSI escape code for color based on the colorID
+func getColor(colorID int64) string {
+ colors := map[int64]string{
+ 0: "\033[37m", // Light Gray
+ 1: "\033[31m", // Red
+ 2: "\033[33m", // Yellow
+ 3: "\033[34m", // Blue
+ 4: "\033[35m", // Magenta
+ 5: "\033[36m", // Cyan
+ 6: "\033[32m", // Green
+ }
+ return colors[colorID%int64(len(colors))]
+}
+
+func (l *LogLine) colorize(prefix string) string {
+ return getColor(l.ColorID) + prefix + l.Msg + ANSIEscapeResetCode
+}
+
// String converts a log line to a string for output given whether a detail
// bool is specified.
func (l *LogLine) String(options *LogOptions) string {
@@ -177,10 +200,18 @@ func (l *LogLine) String(options *LogOptions) string {
out = fmt.Sprintf("%s ", cid)
}
}
+
if options.Timestamps {
out += fmt.Sprintf("%s ", l.Time.Format(LogTimeFormat))
}
- return out + l.Msg
+
+ if options.Colors {
+ out = l.colorize(out)
+ } else {
+ out += l.Msg
+ }
+
+ return out
}
// Since returns a bool as to whether a log line occurred after a given time
diff --git a/libpod/oci_conmon_linux.go b/libpod/oci_conmon_linux.go
index 38bf85834..264236dc1 100644
--- a/libpod/oci_conmon_linux.go
+++ b/libpod/oci_conmon_linux.go
@@ -661,7 +661,7 @@ func (r *ConmonOCIRuntime) HTTPAttach(ctr *Container, req *http.Request, w http.
}
errChan <- err
}()
- if err := ctr.ReadLog(context.Background(), logOpts, logChan); err != nil {
+ if err := ctr.ReadLog(context.Background(), logOpts, logChan, 0); err != nil {
return err
}
go func() {