kubectl plugin to parse and colorize JSON and timestamped logs, similar to jq but optimized for Kubernetes log streams.
- kubectl-jsonlogs: Works as both a kubectl plugin and k9s plugin (pods/logs scope)
- JSON Log Parsing: Pretty-prints JSON logs with colorized output (similar to
jq) - Pulumi Output Formatting: Automatically detects and pretty-prints Pulumi output in
msgfields with colorized resource status, outputs, and summaries - Timestamped Logs: Automatically detects and colorizes timestamps in non-JSON logs
- Log Level Detection: Colorizes log lines based on detected log levels (ERROR, WARN, INFO, DEBUG)
- Minimal Dependencies: Uses only Python standard library (no external modules required)
- Virtual Environment Support: Optional venv loader for future extensions
-
Make the script executable and copy it to a directory in your PATH:
chmod +x kubectl-jsonlogs cp kubectl-jsonlogs $HOME/.local/bin -
Verify installation:
kubectl jsonlogs --help
# Create kubectl plugin directory if it doesn't exist
mkdir -p ~/.kubectl/plugins
# Create symlink
ln -s /path/to/workspace/kubectl-jsonlogs ~/.kubectl/plugins/kubectl-jsonlogsA ready-to-use plugins.yaml file is included in the repository. To use it:
-
Copy the plugins.yaml to your k9s config directory:
k9s info # check the plugin path mkdir -p ~/.config/k9s cp /path/to/workspace/plugins.yaml ~/.config/k9s/plugins.yaml
-
Or merge it into your existing
~/.config/k9s/config.ymlunder theplugins:section.
To manually configure kubectl-jsonlogs for k9s, add the following to your ~/.config/k9s/config.yml:
plugins:
json-logs:
shortCut: Ctrl-J
confirm: false
description: "Pretty JSON Logs (All Containers)"
scopes:
- container
command: kubectl
background: false
args:
- jsonlogs
- $POD
- $NAMESPACE
- $CONTEXT
- $NAMENote:
- The
containerscope allows you to use the plugin from the container view in k9s - The plugin shows logs from the selected container only
- k9s variables:
$POD(pod name),$NAMESPACE,$CONTEXT,$NAME(container name) - When container is not specified (e.g., direct command line usage), the plugin uses
--all-containersflag to tail logs from all containers in the pod - Container names are highlighted in bold cyan to distinguish logs from different containers
Make sure kubectl-jsonlogs is in your PATH for k9s to find it.
As kubectl plugin (pipe mode):
kubectl logs <pod-name> | kubectl jsonlogsAs k9s plugin (container scope):
- Navigate to a container in k9s container view
- Press
Ctrl-J(or your configured shortcut) - The plugin will automatically fetch and colorize logs from the selected container with pretty JSON formatting
- Container names are highlighted in bold cyan when viewing logs from multiple containers
Direct usage (with arguments):
# Show logs from all containers (when container is not specified)
kubectl jsonlogs <pod-name> <namespace> <context>
# Show logs from a specific container
kubectl jsonlogs <pod-name> <namespace> <context> <container-name>Why use kubectl-jsonlogs for k9s?
- Works from container view (scope:
container) - Shows logs from the selected container only
- Container names are colorized and highlighted for easy identification
- Automatically formats and colorizes JSON logs
- Better integration with k9s workflow
JSON Logs:
kubectl logs my-app | kubectl jsonlogsOutput will be colorized JSON with:
- Blue keys
- Green strings
- Magenta numbers
- Yellow booleans
- Cyan brackets/braces
Timestamped Logs:
kubectl logs nginx-pod | kubectl jsonlogsOutput will have:
- Gray timestamps
- Red ERROR level logs
- Yellow WARN level logs
- Cyan INFO level logs
- Gray DEBUG level logs
Mixed Content: The plugin automatically detects JSON objects within log lines and colorizes them appropriately.
Pulumi Output:
When JSON logs contain Pulumi output in the msg field, it is automatically detected and formatted as a multi-line, colorized block:
kubectl logs my-pulumi-app | kubectl jsonlogsPulumi output formatting includes:
- Resource status lines: Colorized based on status (green for creating/created, yellow for updating)
- Section headers: Bold cyan for "Outputs:", "Resources:", "Duration:"
- Status indicators: Yellow for "@ updating" lines
- Output values: Formatted with colored keys and values
- Summary lines: Formatted resource counts and durations
Example output:
{
"level": "info",
"msg":
Resource Updated Updating (resource):
+ pulumi:pulumi:Stack my-stack creating (0s)
+ aws:iam:Policy my-policy created (0.91s)
Outputs:
roleArn: "arn:aws:iam::123456789012:role/my-role"
Resources:
+ 5 created
Duration: 5s
}If you need to add optional dependencies in the future:
-
Create a virtual environment:
cd /path/to/workspace python3 -m venv venv -
Install any optional dependencies:
source venv/bin/activate pip install <optional-package>
-
The script will automatically detect and use the venv if it exists in:
$HOME/.venv/(default location)$HOME/.kubectl-jsonlogs-venv/(plugin-specific location)./venv/(relative to script)./.venv/../venv/
Colors are automatically disabled when output is not a TTY (e.g., when piping to a file). This ensures compatibility with log aggregation tools.
- Python 3.6+
- No external dependencies (uses only Python standard library)
Plugin not found:
- Ensure the script is in your PATH
- Verify the script is executable:
ls -l kubectl-jsonlogs - For k9s: Make sure
kubectl-jsonlogsis accessible viakubectl jsonlogscommand - For k9s: Ensure the plugin is configured with scope
containerand uses correct variables:$POD,$NAMESPACE,$CONTEXT,$NAME
No colors:
- Colors are disabled when output is not a TTY
- Ensure you're running in a terminal that supports ANSI colors
Performance:
- The script processes logs line-by-line for real-time streaming
- For very high-volume logs, consider using
jqfor pure JSON logs
