|
86 | 86 | -e, --regex The type of name matching to use (regex|substring). Defaults to ${regex}. |
87 | 87 | -j, --jq If your output is json - use this jq-selector to parse it. Defaults to \"${default_jq_selector}\". |
88 | 88 | 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. |
91 | 91 | Defaults to ${default_colored_output}. |
92 | 92 | -z, --skip-colors Comma-separated list of colors to not use in output. |
93 | 93 | If you have green foreground on black, this will skip dark grey and some greens: -z 2,8,10 |
@@ -248,6 +248,12 @@ else |
248 | 248 | exit 1 |
249 | 249 | fi |
250 | 250 |
|
| 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 | + |
251 | 257 | # Join function that supports a multi-character separator (copied from http://stackoverflow.com/a/23673883/398441) |
252 | 258 | function join() { |
253 | 259 | # $1 is sep |
@@ -381,7 +387,38 @@ for pod in ${matching_pods[@]}; do |
381 | 387 | fi |
382 | 388 |
|
383 | 389 | 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 |
385 | 422 | if [ "z" == "z$jq_selector" ]; then |
386 | 423 | logs_commands+=("${kubectl_cmd} ${timestamps} | ${colorify_lines_cmd}"); |
387 | 424 | else |
|
0 commit comments