use std::env;
use tracing::{Level, debug, error, info, span, warn};
use tracing_better_stack::{BetterStackConfig, BetterStackLayer};
use tracing_subscriber::prelude::*;
#[tokio::main]
async fn main() {
let ingesting_host = env::var("BETTER_STACK_INGESTING_HOST")
.expect("BETTER_STACK_INGESTING_HOST environment variable must be set");
let source_token = env::var("BETTER_STACK_SOURCE_TOKEN")
.expect("BETTER_STACK_SOURCE_TOKEN environment variable must be set");
let better_stack_layer =
BetterStackLayer::new(BetterStackConfig::builder(ingesting_host, source_token).build());
tracing_subscriber::registry()
.with(better_stack_layer)
.with(
tracing_subscriber::fmt::layer()
.with_target(false)
.compact(),
)
.init();
info!("Application starting up");
debug!("Debug information");
warn!("This is a warning");
info!(
user_id = 123,
action = "login",
ip = "192.168.1.1",
"User logged in successfully"
);
let span = span!(Level::INFO, "process_request", request_id = "abc123");
let _enter = span.enter();
info!("Processing request");
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
info!("Request processed successfully");
drop(_enter);
error!(
error_code = "DB_CONNECTION_FAILED",
retry_count = 3,
"Failed to connect to database"
);
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
info!("Application shutting down");
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
}