| 1 | # jl - JSON Log Viewer [](https://godoc.org/github.com/mightyguava/jl) [](https://circleci.com/gh/mightyguava/jl/tree/master) |
| 2 | |
| 3 | jl (JL) is a parser and formatter for JSON logs, making machine-readable JSON logs human readable again. |
| 4 | |
| 5 |  |
| 6 | |
| 7 | ## Installing |
| 8 | |
| 9 | ``` |
| 10 | go get -u github.com/mightyguava/jl/cmd/jl |
| 11 | ``` |
| 12 | |
| 13 | ## Usage |
| 14 | |
| 15 | jl consumes from stdin and writes to stdout. To use jl, just pipe your JSON logs into jl. For example |
| 16 | |
| 17 | ```sh |
| 18 | ./my-app-executable | jl |
| 19 | cat app-log.json | jl |
| 20 | ``` |
| 21 | |
| 22 | jl can read from a file too |
| 23 | |
| 24 | ```sh |
| 25 | jl my-app-log.json |
| 26 | ``` |
| 27 | |
| 28 | jl itself doesn't support following log files, but since it can consume from a pipe, you can just use `tail` |
| 29 | ```sh |
| 30 | tail -F app-log.json | jl |
| 31 | ``` |
| 32 | |
| 33 | you can page jl's colorized output using `less` with the `-R` flag |
| 34 | |
| 35 | ```sh |
| 36 | jl my-app-log.json | less -R |
| 37 | ``` |
| 38 | |
| 39 | ## Formatters |
| 40 | |
| 41 | jl currently supports 2 formatters, with plans to make the formatters customizable. |
| 42 | |
| 43 | The default is `-format compact`, which extracts only important fields from the JSON log, like `message`, `timestamp`, `level`, colorizes and presents them in a easy to skim way. It drops un-recongized fields from the logs. |
| 44 | |
| 45 | The other option is `-format logfmt`, which formats the JSON logs in a way that closely resembles [logfmt](https://blog.codeship.com/logfmt-a-log-format-thats-easy-to-read-and-write/). This option will emit all fields from each log line. |
| 46 | |
| 47 | Both formatters will echo non-JSON log lines as-is. |
| 48 | |
| 49 | ## Log formats |
| 50 | |
| 51 | JSON application logs tend to have some core shared fields, like `level`, `timestamp`, and `message` |
| 52 | that jq tries to discover and prioritize for formatting. For now, the following formats work best |
| 53 | with jq. For string fields other than `level`, only the keys matter. |
| 54 | |
| 55 | ### Java-like |
| 56 | ```json |
| 57 | { |
| 58 | "level": "error", |
| 59 | "timestamp": "2019-02-02 15:39:45", |
| 60 | "logger": "HelloWorldService", |
| 61 | "thread": "main", |
| 62 | "message": "hello world", |
| 63 | "exception": "java.lang.IllegalArgumentException: The world isn't here\n...stacktraces..." |
| 64 | } |
| 65 | ``` |
| 66 | See [log_example.json](./examples/log_example.json) for a more complete example. |
| 67 | |
| 68 | ### Go/Logrus-like |
| 69 | ```json |
| 70 | { |
| 71 | "level": "error", |
| 72 | "timestamp": "2019-02-02 15:39:45", |
| 73 | "msg": "hello world", |
| 74 | "error": "hello error", |
| 75 | "stack": "\nhello\n\thello.go\nworld\n\tworld.go" |
| 76 | } |
| 77 | ``` |
| 78 | |
| 79 | ### Google Stackdriver |
| 80 | |
| 81 | Stackdriver logs require a little pre-processing from `jq`. |
| 82 | |
| 83 | ```shell script |
| 84 | gcloud logging read "resource.type=cloud_run_revision AND resource.labels.service_name=<YOUR_SERVICE>" --freshness=30m --format=json \ |
| 85 | | jq -c -r 'reverse | .[]' \ |
| 86 | | jl |
| 87 | ``` |
| 88 | |
| 89 | ```json |
| 90 | { |
| 91 | "insertId": "5e864b4c000cc26666c50a3b", |
| 92 | "jsonPayload": { |
| 93 | "message": "hello world", |
| 94 | "foo": "bar" |
| 95 | }, |
| 96 | "labels": { |
| 97 | "instanceId": "..." |
| 98 | }, |
| 99 | "logName": "projects/<PROJECT>/logs/run.googleapis.com%2Fstderr", |
| 100 | "receiveTimestamp": "2020-04-02T20:30:05.116903175Z", |
| 101 | "resource": { |
| 102 | "labels": { |
| 103 | "location": "...", |
| 104 | "project_id": "...", |
| 105 | }, |
| 106 | "type": "cloud_run_revision" |
| 107 | }, |
| 108 | "severity": "INFO", |
| 109 | "timestamp": "2020-04-02T20:30:04.835224670Z" |
| 110 | } |
| 111 | ``` |
| 112 | |
| 113 | ## Roll your own format |
| 114 | |
| 115 | If the format that JL provides does not suit your needs, All of jl's functionality is available as |
| 116 | API, and it's simple to build your own CLI client. See the [godocs](https://godoc.org/github.com/mightyguava/jl) |
| 117 | for the API. |
| 118 | |
| 119 | To change the compact format, all you need to do is provide another |
| 120 | [`[]FieldFmt` specification](https://github.com/mightyguava/jl/blob/f46b94a89340cc314dcaf07622b94fe7dce8f60a/compact_printer.go#L27) |
| 121 | |
| 122 | In the future, I plan to make jl configurable via either flags or config files, if there is demand |
| 123 | for it. |