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.
shell
brew tap chmouel/snazy https://github.com/chmouel/snazy
brew install snazy
shell
cargo install snazy
With your favourite aurhelper for example yay :
shell
yay -S snazy-bin
This repository includes a flake
(see NixOS Wiki on
Flakes).
If you have the nix flake
command enabled (currenty on
nixos-unstable, nixos-version
>= 22.05)
shell
nix run github:chmouel/snazy -- --help # your args are here
You can also use it to test and develop the source code:
shell
nix develop # drops you in a shell with all the thing needed
nix flake check # runs cargo test, rustfmt, …
shell
kubectl logs deployment/pod foo|docker run -i ghcr.io/chmouel/gosmee
Snazy is build with rust, if you want to compile it directly you just need to
grab the source and run cargo build
.
snazy
by "piping" logs into it :shell
kubectl logs deployment/controller|snazy
It supports streaming too. When you have a kubectl logs -f
it 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 snazy
and 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 :
If you want to customize the kail format, you can do it with the flag
--kail-prefix-format
it will replace the variable {namespace} {pod}
{container}
by its value. If you for example only want to print the pod
you can do :
--kail-prefix-format "{pod}"
or set the environement variable SNAZY_KAIL_PREFIX_FORMAT
to make it permanent.
If you do not any prefix for kail you can pass the --kail-no-prefix
flag.
If you want to highlight some patterns you can add the option -r REGEXP
and
snazy
will highlight it. You can have many -r
switches with many
regexps, and you get different highlight for each match.
If snazy
don't recognize the line as json it will symply straight print
it. Either way it will still apply regexp highlighting of the -r
option 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 levels, you can add the flag -f
to filter by
level or many -f
for many levels, for example, this only show warning and
error from the log:
shell
% kubectl log pod|snazy -f warning -f error
--level-symbols
or set the environment variable SNAZY_LEVEL_SYMBOLS
, snazy will show some pretty emojis rather than plain log level label :You can customize the time printed with the -t
flag (or the environment
variable SNAZY_TIME_FORMAT
), the variable respect the UNIX
strftime
format
strings.
You can do your own field matching with the -k/--json-keys
flag, you need to pass the fields msg
, level
and ts
.
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:
```shell 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
```
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 support 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:
```shell snazy --action-regexp "pipelinerun(s)?\s.has success" --action-command "osascript -e 'display notification \"{}\"'"
Shell completions are available for most shells in the misc/completions and it will be automatically installed with the aur/brew package.
Chmouel Boudjnah <@chmouel>