From 8ebb12b75a0f2edf0da982a3b97779a09c6c68a6 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Jun 2024 20:34:06 +0100 Subject: [PATCH] refactor: use tracing in session examples --- actix-session/Cargo.toml | 4 ++-- actix-session/examples/authentication.rs | 14 +++++++++++--- actix-session/examples/basic.rs | 14 +++++++++++--- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/actix-session/Cargo.toml b/actix-session/Cargo.toml index f6d72dd74..7083e8db6 100644 --- a/actix-session/Cargo.toml +++ b/actix-session/Cargo.toml @@ -43,8 +43,8 @@ redis = { version = "0.24", default-features = false, features = ["tokio-comp", actix-session = { path = ".", features = ["cookie-session", "redis-rs-session"] } actix-test = "0.1.0-beta.10" actix-web = { version = "4", default-features = false, features = ["cookies", "secure-cookies", "macros"] } -env_logger = "0.11" -log = "0.4" +tracing-subscriber = {version = "0.3", features = ["env-filter"]} +tracing = "0.1.30" [[example]] name = "basic" diff --git a/actix-session/examples/authentication.rs b/actix-session/examples/authentication.rs index 329d63126..6413d73ee 100644 --- a/actix-session/examples/authentication.rs +++ b/actix-session/examples/authentication.rs @@ -5,6 +5,8 @@ use actix_web::{ middleware, web, App, Error, HttpResponse, HttpServer, Responder, }; use serde::{Deserialize, Serialize}; +use tracing::level_filters::LevelFilter; +use tracing_subscriber::EnvFilter; #[derive(Deserialize)] struct Credentials { @@ -71,15 +73,21 @@ async fn secret(session: Session) -> Result { #[actix_web::main] async fn main() -> std::io::Result<()> { - env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + tracing_subscriber::fmt() + .with_env_filter( + EnvFilter::builder() + .with_default_directive(LevelFilter::INFO.into()) + .from_env_lossy(), + ) + .init(); // The signing key would usually be read from a configuration file/environment variables. let signing_key = Key::generate(); - log::info!("setting up Redis session storage"); + tracing::info!("setting up Redis session storage"); let storage = RedisSessionStore::new("127.0.0.1:6379").await.unwrap(); - log::info!("starting HTTP server at http://localhost:8080"); + tracing::info!("starting HTTP server at http://localhost:8080"); HttpServer::new(move || { App::new() diff --git a/actix-session/examples/basic.rs b/actix-session/examples/basic.rs index a8a6aa847..ecc847040 100644 --- a/actix-session/examples/basic.rs +++ b/actix-session/examples/basic.rs @@ -1,5 +1,7 @@ use actix_session::{storage::RedisSessionStore, Session, SessionMiddleware}; use actix_web::{cookie::Key, middleware, web, App, Error, HttpRequest, HttpServer, Responder}; +use tracing::level_filters::LevelFilter; +use tracing_subscriber::EnvFilter; /// simple handler async fn index(req: HttpRequest, session: Session) -> Result { @@ -18,15 +20,21 @@ async fn index(req: HttpRequest, session: Session) -> Result std::io::Result<()> { - env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + tracing_subscriber::fmt() + .with_env_filter( + EnvFilter::builder() + .with_default_directive(LevelFilter::INFO.into()) + .from_env_lossy(), + ) + .init(); // The signing key would usually be read from a configuration file/environment variables. let signing_key = Key::generate(); - log::info!("setting up Redis session storage"); + tracing::info!("setting up Redis session storage"); let storage = RedisSessionStore::new("127.0.0.1:6379").await.unwrap(); - log::info!("starting HTTP server at http://localhost:8080"); + tracing::info!("starting HTTP server at http://localhost:8080"); HttpServer::new(move || { App::new()