Jon Gjengset 7bc97b1abe
fmt: Timestamped log messages (#96)
Currently there is no good way to get timestamped log entries with the
`tracing-fmt` subscriber. Even with `full`, timestamps are not included.
This is surprising to those coming from other logging crates. It is also
inconvenient to work around, as you would either need to add a custom
field to _all_ spans and events across all your crates, or write your
own subscriber which sort of reduces the usefulness of `fmt`.

This patch introduces a trait for event formatters (`FormatEvent`) so
that it is easier to write more complex formatters (previously they were
just `Fn(...) -> fmt::Result` which would require boxing a closure to
produce). It then implements this new trait for `default::Format`, which
is the new default formatter. It implements "compact"/full formatting
exactly like before, except that is also generic over a timer. The timer
must implement `default::FormatTime`, which has a single method that
writes the current time out to a `fmt::Write`. This method is
implemented for a two new unit structs `SystemTime` and `Uptime`.
`SystemTime` is pretty-printed using `chrono` with the new default
feature `chrono`, and `Debug` of `std::net::SystemTime` is used if the
feature is turned off. `Uptime` prints the fractional number of seconds
since an epoch. Users can also provide `()` as the time formatter to not
produce timestamps in logged entries (accessible through
`default::Format::without_time`).

This patch also switches the default output to the verbose format, with
`compact` offered to return to the compact output format.

Closes #94.
2019-06-26 16:41:31 -07:00

41 lines
896 B
Rust

#[macro_use]
extern crate tracing;
#[macro_use]
extern crate tracing_proc_macros;
extern crate env_logger;
extern crate tracing_fmt;
#[trace]
fn nth_fibonacci(n: u64) -> u64 {
if n == 0 || n == 1 {
debug!("Base case");
1
} else {
debug!("Recursing");
nth_fibonacci(n - 1) + nth_fibonacci(n - 2)
}
}
#[trace]
fn fibonacci_seq(to: u64) -> Vec<u64> {
let mut sequence = vec![];
for n in 0..=to {
debug!("Pushing {n} fibonacci", n = n);
sequence.push(nth_fibonacci(n));
}
sequence
}
fn main() {
env_logger::Builder::new().parse("trace").init();
let subscriber = tracing_fmt::FmtSubscriber::builder().finish();
tracing::subscriber::with_default(subscriber, || {
let n: u64 = 5;
let sequence = fibonacci_seq(n);
info!("The first {} fibonacci numbers are {:?}", n, sequence);
})
}