Skip to content

Commit

Permalink
nice-ify examples
Browse files Browse the repository at this point in the history
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
hawkw committed Apr 3, 2020
1 parent 9959e95 commit ed2d128
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 49 deletions.
2 changes: 1 addition & 1 deletion examples/examples/all-levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use tracing::Level;
use tracing_subscriber;

fn main() {
tracing_subscriber::FmtSubscriber::builder()
tracing_subscriber::fmt()
// all spans/events with a level higher than TRACE (e.g, info, warn, etc.)
// will be written to stdout.
.with_max_level(Level::TRACE)
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/async_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ async fn write(stream: &mut TcpStream) -> io::Result<usize> {
}

#[tokio::main]
pub async fn main() -> Result<(), Box<dyn Error>> {
pub async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let addr = "127.0.0.1:6142".parse()?;

tracing_subscriber::fmt()
.with_env_filter("async_fn=trace")
.init();
.try_init()?;

// Open a TCP stream to the socket address.
//
Expand Down
9 changes: 4 additions & 5 deletions examples/examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ use tracing::{debug, info, info_span, trace_span, warn};
use tracing_futures::Instrument;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
use tracing_subscriber::{EnvFilter, FmtSubscriber};
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
use tracing_subscriber::EnvFilter;

let subscriber = FmtSubscriber::builder()
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env().add_directive("echo=trace".parse()?))
.finish();
tracing::subscriber::set_global_default(subscriber)?;
.try_init()?;

// Allow passing an address to listen on as the first argument of this
// program, but otherwise we'll just set up our TCP listener on
Expand Down
30 changes: 10 additions & 20 deletions examples/examples/futures-proxy-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ use tracing::{debug, debug_span, info, warn};
use tracing_attributes::instrument;
use tracing_futures::Instrument;

type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

#[instrument]
async fn transfer(
mut inbound: TcpStream,
proxy_addr: SocketAddr,
) -> Result<(), Box<dyn std::error::Error>> {
async fn transfer(mut inbound: TcpStream, proxy_addr: SocketAddr) -> Result<(), Error> {
let mut outbound = TcpStream::connect(&proxy_addr).await?;

let (mut ri, mut wi) = inbound.split();
Expand Down Expand Up @@ -82,7 +81,7 @@ arg_enum! {
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
async fn main() -> Result<(), Error> {
let matches = App::new("Proxy Server Example")
.version("1.0")
.arg(
Expand Down Expand Up @@ -140,25 +139,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

fn set_global_default(matches: &ArgMatches<'_>) -> Result<(), Box<dyn std::error::Error>> {
use tracing_subscriber::{filter::EnvFilter, FmtSubscriber};
fn set_global_default(matches: &ArgMatches<'_>) -> Result<(), Error> {
let filter = tracing_subscriber::EnvFilter::from_default_env()
.add_directive("proxy_server=trace".parse()?);
let subscriber = tracing_subscriber::fmt().with_env_filter(filter);
match value_t!(matches, "log_format", LogFormat).unwrap_or(LogFormat::Plain) {
LogFormat::Json => {
let subscriber = FmtSubscriber::builder()
.json()
.with_env_filter(
EnvFilter::from_default_env().add_directive("proxy_server=trace".parse()?),
)
.finish();
tracing::subscriber::set_global_default(subscriber)?;
subscriber.json().try_init()?;
}
LogFormat::Plain => {
let subscriber = FmtSubscriber::builder()
.with_env_filter(
EnvFilter::from_default_env().add_directive("proxy_server=trace".parse()?),
)
.finish();
tracing::subscriber::set_global_default(subscriber)?;
subscriber.try_init()?;
}
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/hyper-echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
async fn main() -> Result<(), Box<dyn std::error::Error>> {
use tracing_log::env_logger::BuilderExt;

let subscriber = tracing_subscriber::FmtSubscriber::builder()
let subscriber = tracing_subscriber::fmt()
.with_max_level(Level::TRACE)
.finish();
let mut builder = env_logger::Builder::new();
Expand Down
11 changes: 6 additions & 5 deletions examples/examples/instrumented_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::error::Error;
use std::fmt;
use tracing_error::{prelude::*, ErrorLayer};
use tracing_subscriber::{prelude::*, registry::Registry};
use tracing_subscriber::prelude::*;

#[derive(Debug)]
struct FooError {
Expand Down Expand Up @@ -43,11 +43,12 @@ fn do_another_thing(

#[tracing::instrument]
fn main() {
let subscriber = Registry::default()
.with(tracing_subscriber::fmt::Layer::default())
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
// The `ErrorLayer` subscriber layer enables the use of `SpanTrace`.
.with(ErrorLayer::default());
tracing::subscriber::set_global_default(subscriber).expect("Could not set global default");
.with(ErrorLayer::default())
.init();

match do_something("hello world") {
Ok(result) => println!("did something successfully: {}", result),
Err(e) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/log.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
tracing_subscriber::fmt::Subscriber::builder()
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

Expand Down
8 changes: 4 additions & 4 deletions examples/examples/spawny_thing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use tokio;

use futures::future::join_all;
use std::error::Error;
use tracing::{debug, info};
use tracing_attributes::instrument;

Expand Down Expand Up @@ -37,11 +38,10 @@ async fn subtask(number: usize) -> usize {
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let subscriber = tracing_subscriber::fmt::Subscriber::builder()
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.finish();
tracing::subscriber::set_global_default(subscriber)?;
.try_init()?;
parent_task(10).await;
Ok(())
}
5 changes: 2 additions & 3 deletions examples/examples/tower-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ fn req_span<A>(req: &Request<A>) -> tracing::Span {

#[tokio::main]
async fn main() -> Result<(), Err> {
let subscriber = tracing_subscriber::fmt::Subscriber::builder()
tracing_subscriber::fmt()
.with_env_filter("tower=trace")
.finish();
tracing::subscriber::set_global_default(subscriber)?;
.try_init()?;

let mut svc = ServiceBuilder::new()
.timeout(Duration::from_millis(250))
Expand Down
7 changes: 3 additions & 4 deletions examples/examples/tower-load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,18 @@ use tokio::{time, try_join};
use tower::{Service, ServiceBuilder, ServiceExt};
use tracing::{self, debug, error, info, span, trace, warn, Level, Span};
use tracing_futures::Instrument;
use tracing_subscriber::{filter::EnvFilter, reload::Handle, FmtSubscriber};
use tracing_subscriber::{filter::EnvFilter, reload::Handle};
use tracing_tower::{request_span, request_span::make};

type Err = Box<dyn Error + Send + Sync + 'static>;

#[tokio::main]
async fn main() -> Result<(), Err> {
let builder = FmtSubscriber::builder()
let builder = tracing_subscriber::fmt()
.with_env_filter("info,tower_load=debug")
.with_filter_reloading();
let handle = builder.reload_handle();

let _ = tracing::subscriber::set_global_default(builder.finish());
builder.try_init()?;

let addr = "[::1]:3000".parse::<SocketAddr>()?;
let admin_addr = "[::1]:3001".parse::<SocketAddr>()?;
Expand Down
5 changes: 2 additions & 3 deletions examples/examples/tower-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ impl<T> Service<T> for MakeSvc {

#[tokio::main]
async fn main() -> Result<(), Err> {
let subscriber = tracing_subscriber::fmt::Subscriber::builder()
tracing_subscriber::fmt()
.with_env_filter("tower=trace")
.finish();
tracing::subscriber::set_global_default(subscriber)?;
.try_init()?;

let svc = ServiceBuilder::new()
.timeout(Duration::from_millis(250))
Expand Down

0 comments on commit ed2d128

Please sign in to comment.