Snazy is a simple tool to parse json or other type of logs and output them in a nice format with nice colors.
As a tekton and pipelines-as-code developer who has to dig into controller/webhook logs all the time I wanted something that is a bit easier to look in the eyes and identify error/info/warning statements easily.
You do not have to use it only with tekton but work well with projects that uses
go-uber/zap library like
knative and many others.
It can work as a super-"tail" too if you want to stream logs and being able to have "actions" or "highlights" on a regexp match.
Go to the release page and grab the archive or package targeting your platform.
brew tap chmouel/snazy https://github.com/chmouel/snazy
brew install snazycargo install snazyWith your favourite aurhelper for example yay :
yay -S snazy-binSnazy is available from nixpkgs.
nix-env -iA snazy
nix run nixpkgs#snazy -- --help # your args are herebin install github.com/chmouel/snazy kubectl logs deployment/pod foo|docker run -i ghcr.io/chmouel/snazySource install
Snazy is built with rust, if you want to compile it directly you just need to
grab the source and run cargo build. (assuming you have the rust tool chain installed)
- Usually you use
snazyby "piping" logs into it :
kubectl logs deployment/controller|snazy-
It supports streaming too. When you have a
kubectl logs -fit will just wait for input and snazzily print your logs from the stream (one line at a time). -
you can pass one or many files on the command line to
snazyand it will parse them rather than using the standard input. -
If you do not pass a file and your input comes from https://github.com/boz/kail it will automatically detect it and print the
namespace/pod[container]as prefix :
-
The flag "--kail-prefix-format" let you customize how to display the kail format, the templates
{namespace},{pod},{container}will be replaced by its value and a "\n" will be replaced by a newline. As an example if you want to only show the current pod followed by a newline you can use the following template:--kail-prefix-format "{pod}\n"the environment variable
SNAZY_KAIL_PREFIX_FORMATlet you make this setting permanent. -
If you do not any prefix for kail you can pass the
--kail-no-prefixflag. -
If you want to highlight some patterns you can add the option
-r/--regexpfollowed by a REGEXP andsnazywill highlight it. You can have many-rswitches with many regexps, and you get different highlight for each match. -
If you want to have the highlight forced to some colors you can add the color at the beginning of the regexp followed by a colon. The colors can be one of
yellow,red,green,blue,magenta,cyan,white,blackor an RGB color model e.g:88,48,235. For example if you want to highlight ERROR in red and WARNING in yellow you can do:
% kubectl log pod|snazy -r red:ERROR -r yellow:WARNING -r green:INFO -r 88,48,235:MITIGATED- You can define a background color as well with this format, Only fixed colors are supported for now (ie: no rgb or fixed):
% kubectl log pod|snazy -r fg=black,bg=yellow:ERROR-
If
snazydon't recognize the line as JSON it will simply straight print it. Either way it will still apply regexp highlighting of the-roption or do the action commands matching (see below). This let you use it for any logs to do some regexp highlighting and action on pattern. -
If you want to only show some specific levels, you can add the flag
-f/--filter-levelsto filter by level or many-ffor many levels, for example, this only show warning and error from the log:
% kubectl log pod|snazy -f warning -f error- If you pass the flag
-l/--level-symbolsor set the environment variableSNAZY_LEVEL_SYMBOLS, snazy will show some pretty emojis rather than plain log level label :
-
You can customize the time printed with the
--time-formatflag (or the environment variableSNAZY_TIME_FORMAT), the variable respect the UNIXstrftimeformat strings. -
You can specify a timezone with the
--timezoneflag (or the environment variableSNAZY_TIMEZONE). By default, the timestamps are displayed in the server's timezone (usually UTC). The timezone should be specified in the IANA timezone database format (e.g.,America/New_York,Europe/Paris,Asia/Tokyo).kubectl logs deployment/controller | snazy --timezone America/New_York -
snazy looks for stack traces within the stacktrace field of JSON logs. When it finds a stack trace containing both a filename and a line number, snazy highlights it with a unique color, as illustrated in the image.
You can disable the display of stacktraces in error logs using the
--hide-stacktraceflag (or the environment variableSNAZY_HIDE_STACKTRACE).kubectl logs deployment/controller | snazy --hide-stacktrace -
If you want to skip showing some lines you can specify the flag
-S/--skip-line-regexp. When it matches the word or regexp in this value it will simply skipping printing the line. You can have multiple flags if you want to skip multiple lines. -
You can do your own field matching with the
-k/--json-keysflag, you need to pass the fieldsmsg,levelandts. The fields target a key in a json payload specified as JSON Object notation. The description of the fileds are:msg: The message text (string)level: The log level (eg: info) (string)ts: The timestamp, a float or a datetime.
If any of those fields are missing the parser will fails.
Example:
echo '{"the": {"msg": {"is": "message"}, "level": {"is": "INFO"}, "ts": [{"is": "2022-04-25T14:20:32.505637358Z"}]}}' | \ snazy -k msg=/the/msg/is -k level=/the/level/is -k ts=/the/ts/0/is # => INFO 14:20:32 message
-
Snazy support action command on regexp, which mean if you have a regexp matching a message it will run an action on it. It currently supports only one action one regexp. If you specify the string
"{}"it will be expanded to the matched string. For example on MacOS this command will display a notification with the pipelinerun that has succeeded:snazy --action-regexp "pipelinerun(s)?\s*.*has success" --action-command "osascript -e 'display notification \"{}\"'"
-
By default snazy will show only some fields as autodetected from your log. You can display all extra fields from your JSON logs using the
--extra-fieldsflag (or the environment variableSNAZY_EXTRA_FIELDS):
cat log.json | snazy --extra-fields
# or
export SNAZY_EXTRA_FIELDS=true
cat log.json | snazyOr only specific fields using --include-fields (comma-separated, or the environment variable SNAZY_INCLUDE_FIELDS):
cat log.json | snazy --include-fields duration_ms,status_code,url_path
# or
export SNAZY_INCLUDE_FIELDS=duration_ms,status_code,url_path
cat log.json | snazyThis will append the specified fields to the output, with the field names colorized for clarity.
Example output:
π 07:55:22 GitHub API call completed duration_ms=348 status_code=200 url_path=/api/v3/repos/chmouel/e2e-gapps/issues/comments/46969You can go even further with UNIX shell pipelines, and feed snazy to fzf for interactive filtering of the stream. for example to stream everything on a kubernetes cluster with kail, transforming the logs via snazy and finally using fzf to interactively select the patter to match:
kail --since=1h | snazy | fzf +s --ansiThis will give you a prompt with fzf where you can type the query you want.
An handy script located here let you show the log
of a GitHub action runs through snazy and the
bat pager.
You will need to setup the gh cli and install fzf to make it works.
Here is a video showing the feature https://streamable.com/7sf1hq
Shell completions are available for most shells using the command --shell-completion for example --shell-completion=zsh. Many different shell are supported. I let the reader figure out how to use them with their respective shells. Brew and RPM packages should have them auto configured for bash/fish/zsh.
Snazy can parse and colorize the output of kubectl get events, making it much easier to visually distinguish different Kubernetes object types (such as pods, deployments, replica sets, etc.). Each object type in the OBJECT column is assigned a distinct color, and all objects of the same type share the same color for consistency.
Example usage:
kubectl get events -n <namespace> | snazy- The
OBJECTcolumn will show each type (e.g.,pod/,replicaset/,deployment/) in a unique color. - The rest of the columns (
LAST SEEN,TYPE,REASON,MESSAGE) are also colorized for clarity.
Screenshot:
Add your screenshot here to showcase the feature!
- I have seen a tool like that before with another stupid name? I used to have a python script that does the same and more called (sugarjazy) but I wanted to experiment with Rust, so I called this one snazy.
- You missed a z to snazzy. True that. But snazy is easier to google than snazzy :p
- Why rust? Good question, I guess i'll go by why not? it's kinda nice to program high level cli with low level language easily. sharkdp/fd
- π Fediverse - <@[email protected]>
- π¦ Twitter - <@chmouel>
- π Blog - <https://blog.chmouel.com>