diff options
author | gcalin <caling@protonmail.com> | 2022-03-04 19:04:58 +0100 |
---|---|---|
committer | gcalin <caling@protonmail.com> | 2022-03-29 17:29:13 +0200 |
commit | c185d8c0d6774e90f64f4c6a84270c20a9325944 (patch) | |
tree | d6500e6d9d740cb9a60772b86cc01b4b00a33c38 /libpod/logs/log.go | |
parent | 0eff4b70d0429c0dd1d95bc0a15f679cef351cb5 (diff) | |
download | podman-c185d8c0d6774e90f64f4c6a84270c20a9325944.tar.gz podman-c185d8c0d6774e90f64f4c6a84270c20a9325944.tar.bz2 podman-c185d8c0d6774e90f64f4c6a84270c20a9325944.zip |
Add option for pod logs to display different colors per container.
Signed-off-by: Krzysztof Baran <krysbaran@gmail.com>
Signed-off-by: gcalin <caling@protonmail.com>
Diffstat (limited to 'libpod/logs/log.go')
-rw-r--r-- | libpod/logs/log.go | 33 |
1 files changed, 32 insertions, 1 deletions
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 |