4 releases

0.1.3 Feb 2, 2021
0.1.2 Jan 28, 2021
0.1.1 Dec 22, 2020
0.1.0 Nov 16, 2020

#14 in Profiling

Download history 412/week @ 2021-05-30 262/week @ 2021-06-06 277/week @ 2021-06-13 334/week @ 2021-06-20 367/week @ 2021-06-27 387/week @ 2021-07-04 225/week @ 2021-07-11 369/week @ 2021-07-18 396/week @ 2021-07-25 300/week @ 2021-08-01 442/week @ 2021-08-08 364/week @ 2021-08-15 458/week @ 2021-08-22 327/week @ 2021-08-29 537/week @ 2021-09-05 565/week @ 2021-09-12

1,613 downloads per month
Used in 23 crates (14 directly)

Custom license

58KB
1K SLoC

observability

Structured Contextual Logging (or tracing)

Why

Watch or Read

Intention of this crate

This crate is designed ot be a place to experiment with ideas around tracing and structured logging. This crate will probably never stabilize. Instead it is my hope to feed any good ideas back into the underlying dependencies.

Usage

There are a couple of ways to use structured logging.

Console and filter

If you want to try and filter in on an issue it might be easiest to simply log to the console and filter on what you want. Here's an example command:

RUST_LOG='core[a{something="foo"}]=debug' my_bin

Or a more simple version using the default Log:

RUST_LOG=trace my_bin
Types of tracing

There are many types of tracing exposed by this crate. The [Output] type is designed to be used with something like structopt so you can easily set which type you want with a command line arg. You could also use an environment variable. The [Output] variant is passing into the [init_fmt] function on start up.

Filtering
RUST_LOG='core[a{something="foo"}]=debug'

Here we are saying show me all the events that are:

  • In the core module
  • Inside a span called a
  • The span a has to have a field called something that is equal to foo
  • They are at least debug level.

Most of these options are optional. They can be combined like:

RUST_LOG='[{}]=error,[{something}]=debug'

The above means show me errors from anywhere but also any event or span with the field something that's at least debug.

See here for more info.

Json

Sometimes there's too much data and it's better to capture it to interact with using another tool later. For this we can output everything as Json using the flag --structured Json. Then you can pipe the output from stdout to you're file of choice. Here's some sample output:

{"time":"2020-03-03T08:07:05.910Z","name":"event crates/sim2h/src/sim2h_im_state.rs:695","level":"INFO","target":"sim2h::sim2h_im_state","module_path":"sim2h::sim2h_im_state","file":"crates/sim2h/src/sim2h_im_stat
e.rs","line":695,"fields":{"space_hashes":"[]"},"spans":[{"id":[1099511627778],"name":"check_gossip","level":"INFO","target":"sim2h::sim2h_im_state","module_path":"sim2h::sim2h_im_state","file":"crates/sim2h/src/s
im2h_im_state.rs","line":690}]}

Every log will include the above information expect for the spans which will only show up if there are parent spans in the context of the event.

You can combine filter with Json as well.

Tools

Some useful tools for formatting and using the json data.

A sample workflow:

RUST_LOG='core[{}]=debug' my_bin --structured Json > log.json
cat out.json | jq '. | {time: .time, name: .name, message: .fields.message, file: .file, line: .line, fields: .fields, spans: .spans}' | json2csv -o log.csv
tad log.csv

Dependencies

~4.5–7MB
~133K SLoC