Traceon - trace on json
A simple log and trace formatter with a structured json output, it flattens events from nested spans, overriding parent fields.
The tracing crate is difficult to understand initially, this crate is designed to be as easy to use as possible with sensible defaults and configuration options. The only two crates you'll need in your Cargo.toml are:
[]
= "0.1"
= "0.1"
For pretty printing the output like the examples below, install jq and run commands like:
|
By default env-filter is used at the info level, to change the level you can set an environment variable e.g. RUST_LOG=warn, all the options are detailed here
Examples
Simple Example
The extra fields outputted below are defaults that can be turned off:
Log levels are converted to numbers by default:
trace: 10
debug: 20
info: 30
warn: 40
error: 50
#[instrument] macro
If you're using normal functions or async, you can use the tracing::instrument macro to capture the parameters for each function call:
async
async
Instrument trait
If you need to add some additional context to an async function, you can create a span and instrument it:
use Instrument;
async
async
The above package_name comes from the environment variable provided by cargo, which gets it from Cargo.toml:
[]
= "testing_traceon"
IMPORTANT! for async functions only ever use the above two methods, which are the #[instrument] macro, and Instrument trait. The guard detailed below should not be used across async boundaries.
Instrument trait and entered span
To combine the output from the two examples above we can enter a span with the arguments added to the trace:
use Instrument;
async
async
You can see above that the nested "span": "add" overrode the parent "span": "math functions"
The add function from above could be rewritten like this:
async
This will cause the span to exit at the end of the function when _span is dropped, just remember to be very careful not to put any .await points when an EnteredSpan like _span above is being held.
Turn off fields
This is an example of changing all the defaults fields to their opposites:
use LevelFormat;
This was using a Cargo.toml with the binary renamed to bootstrap for demonstration purposes:
[[]]
= "bootstrap"
= "src/main.rs"
Write to a file
If you wanted to write to log files instead of std, it's as simple adding the dependency to Cargo.toml:
[]
= "0.2.2"
And initializing it via the builder:
Compose with other layers
You can also use the formatting and storage layer with other tracing layers as you get more comfortable with the tracing ecosystem, e.g. to change the filter:
use Traceon;
use ;
Performance
This crate uses the idea originated from: LukeMathWalker/tracing-bunyan-formatter of storing fields from visited spans in a HashMap instead of a BTreeMap which is more suited for flattening fields, and results in very similar performance to the json formatter in tracing-subcriber:
logging to a sink
units = nanosecond or billionth of a second
logging to stdout
units = microsecond or millionth of a second