|
1 |
| -use std::{io, str}; |
2 | 1 | pub use tracing;
|
3 | 2 | pub use tracing_subscriber;
|
4 | 3 |
|
5 |
| -pub fn init() -> tracing::dispatcher::DefaultGuard { |
6 |
| - tracing::subscriber::set_default( |
7 |
| - tracing_subscriber::fmt() |
8 |
| - .with_max_level(tracing::Level::TRACE) |
9 |
| - .with_span_events(tracing_subscriber::fmt::format::FmtSpan::CLOSE) |
10 |
| - .with_writer(PrintlnWriter { _p: () }) |
11 |
| - .finish(), |
12 |
| - ) |
13 |
| -} |
14 |
| - |
15 |
| -struct PrintlnWriter { |
16 |
| - _p: (), |
17 |
| -} |
| 4 | +use tracing_subscriber::layer::SubscriberExt; |
| 5 | +use tracing_subscriber::util::SubscriberInitExt; |
18 | 6 |
|
19 |
| -impl tracing_subscriber::fmt::MakeWriter for PrintlnWriter { |
20 |
| - type Writer = PrintlnWriter; |
21 |
| - fn make_writer(&self) -> Self::Writer { |
22 |
| - PrintlnWriter { _p: () } |
23 |
| - } |
24 |
| -} |
25 |
| - |
26 |
| -impl io::Write for PrintlnWriter { |
27 |
| - fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
28 |
| - let s = str::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; |
29 |
| - println!("{}", s); |
30 |
| - Ok(s.len()) |
31 |
| - } |
32 |
| - |
33 |
| - fn write_fmt(&mut self, fmt: std::fmt::Arguments<'_>) -> io::Result<()> { |
34 |
| - println!("{}", fmt); |
35 |
| - Ok(()) |
36 |
| - } |
| 7 | +pub fn init() -> tracing::dispatcher::DefaultGuard { |
| 8 | + let use_colors = atty::is(atty::Stream::Stdout); |
| 9 | + let layer = tracing_tree::HierarchicalLayer::default() |
| 10 | + .with_writer(tracing_subscriber::fmt::writer::TestWriter::default()) |
| 11 | + .with_indent_lines(true) |
| 12 | + .with_ansi(use_colors) |
| 13 | + .with_targets(true) |
| 14 | + .with_indent_amount(2); |
37 | 15 |
|
38 |
| - fn flush(&mut self) -> io::Result<()> { |
39 |
| - Ok(()) |
40 |
| - } |
| 16 | + tracing_subscriber::registry().with(layer).set_default() |
41 | 17 | }
|
0 commit comments