Skip to content

Commit 43f031a

Browse files
authored
fix: use plain color theme (#153)
* fix: use plain color theme Show logs with INFO in plain white. Show logs with WARN in dark yellow. Show logs with ERROR in dark red. Show logs with SUCCESS in dark green. * feat: add loglevel option to colored-output flag Adds a new 'loglevel' option to the existing -k/--colored-output flag that provides log level-based coloring while preserving backward compatibility. - Extends -k flag to support pod|line|loglevel|false options - loglevel mode colors pod names (for pod differentiation) + log content based on log levels - Preserves existing pod/line/false behavior unchanged - Adds validation for colored-output option values - Updates help text to document the new option This addresses upstream feedback to provide log level coloring behind a flag instead of breaking backward compatibility.
1 parent 30b00b3 commit 43f031a

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

kubetail

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ where:
8686
-e, --regex The type of name matching to use (regex|substring). Defaults to ${regex}.
8787
-j, --jq If your output is json - use this jq-selector to parse it. Defaults to \"${default_jq_selector}\".
8888
example: --jq \".logger + \\\" \\\" + .message\"
89-
-k, --colored-output Use colored output (pod|line|false).
90-
pod = only color pod name, line = color entire line, false = don't use any colors.
89+
-k, --colored-output Use colored output (pod|line|loglevel|false).
90+
pod = only color pod name, line = color entire line, loglevel = color pod name + log content based on log levels, false = don't use any colors.
9191
Defaults to ${default_colored_output}.
9292
-z, --skip-colors Comma-separated list of colors to not use in output.
9393
If you have green foreground on black, this will skip dark grey and some greens: -z 2,8,10
@@ -248,6 +248,12 @@ else
248248
exit 1
249249
fi
250250

251+
# Validate colored_output option
252+
if [ "$colored_output" != "pod" ] && [ "$colored_output" != "line" ] && [ "$colored_output" != "loglevel" ] && [ "$colored_output" != "false" ]; then
253+
echo "Invalid colored output option: $colored_output. Valid options are: pod, line, loglevel, false"
254+
exit 1
255+
fi
256+
251257
# Join function that supports a multi-character separator (copied from http://stackoverflow.com/a/23673883/398441)
252258
function join() {
253259
# $1 is sep
@@ -381,7 +387,38 @@ for pod in ${matching_pods[@]}; do
381387
fi
382388

383389
kubectl_cmd="${KUBECTL_BIN} ${context:+--context=${context}} logs ${pod} ${container} -f=${follow} --previous=${previous} --since=${since} --tail=${tail} ${namespace_arg} ${cluster}"
384-
colorify_lines_cmd="while read -r; do echo \"$colored_line\" | tail -n +1; done"
390+
391+
if [ ${colored_output} == "loglevel" ]; then
392+
# Log level based coloring with colored pod names
393+
colorify_lines_cmd="while read -r; do
394+
# Create colored prefix similar to pod mode
395+
colored_prefix=\"${color_start}[${color_end}${color_index_prefix}${color_start}${display_name}]${color_end} \"
396+
397+
# Apply log level colorization to log content only, keeping pod name colored
398+
if echo \"\$REPLY\" | grep -qE '\\b(INFO|info)\\b'; then
399+
# INFO: plain white (default terminal color) for log content
400+
colored_line=\"\$colored_prefix\$REPLY\"
401+
elif echo \"\$REPLY\" | grep -qE '\\b(WARN|warn|WARNING|warning)\\b'; then
402+
# WARN: dark yellow for log content
403+
colored_line=\"\$colored_prefix\$(tput setaf 3)\$REPLY\$(tput sgr0)\"
404+
elif echo \"\$REPLY\" | grep -qE '\\b(ERROR|error|ERR|err)\\b'; then
405+
# ERROR: dark red for log content
406+
colored_line=\"\$colored_prefix\$(tput setaf 1)\$REPLY\$(tput sgr0)\"
407+
elif echo \"\$REPLY\" | grep -qE '\\b(SUCCESS|success)\\b'; then
408+
# SUCCESS: dark green for log content
409+
colored_line=\"\$colored_prefix\$(tput setaf 2)\$REPLY\$(tput sgr0)\"
410+
else
411+
# Default: plain log content with colored pod name
412+
colored_line=\"\$colored_prefix\$REPLY\"
413+
fi
414+
echo \"\$colored_line\" | tail -n +1
415+
done"
416+
else
417+
# Original coloring logic for pod/line/false modes
418+
colorify_lines_cmd="while read -r; do
419+
echo \"$colored_line\" | tail -n +1
420+
done"
421+
fi
385422
if [ "z" == "z$jq_selector" ]; then
386423
logs_commands+=("${kubectl_cmd} ${timestamps} | ${colorify_lines_cmd}");
387424
else

0 commit comments

Comments
 (0)