From 289550184c7bb9bde66e2abbda05694dd80225f0 Mon Sep 17 00:00:00 2001 From: Blake Hildebrand Date: Fri, 31 Dec 2021 17:56:17 -0500 Subject: [PATCH] Change examples to use tracing-subscriber for logging --- lambda-extension/Cargo.toml | 3 +-- lambda-extension/examples/basic.rs | 15 ++++++++++----- lambda-extension/examples/custom_events.rs | 15 ++++++++++----- .../examples/custom_trait_implementation.rs | 15 ++++++++++----- lambda-integration-tests/Cargo.toml | 6 +++--- lambda-integration-tests/src/bin/extension-fn.rs | 14 +++++++++++--- .../src/bin/extension-trait.rs | 14 +++++++++++--- lambda-integration-tests/src/bin/http-fn.rs | 14 +++++++++++--- lambda-integration-tests/src/bin/runtime-fn.rs | 14 +++++++++++--- lambda-integration-tests/src/bin/runtime-trait.rs | 12 +++++++++--- lambda-runtime/Cargo.toml | 3 +-- lambda-runtime/examples/basic.rs | 13 ++++++++----- lambda-runtime/examples/error-handling.rs | 12 ++---------- lambda-runtime/examples/shared_resource.rs | 12 ++++++++---- 14 files changed, 106 insertions(+), 56 deletions(-) diff --git a/lambda-extension/Cargo.toml b/lambda-extension/Cargo.toml index ac155397..34cd9cf2 100644 --- a/lambda-extension/Cargo.toml +++ b/lambda-extension/Cargo.toml @@ -24,6 +24,5 @@ tokio-stream = "0.1.2" lambda_runtime_api_client = { version = "0.4", path = "../lambda-runtime-api-client" } [dev-dependencies] -simple_logger = "1.6.0" -log = "^0.4" simple-error = "0.2" +tracing-subscriber = "0.3" diff --git a/lambda-extension/examples/basic.rs b/lambda-extension/examples/basic.rs index 573b3281..6ed92652 100644 --- a/lambda-extension/examples/basic.rs +++ b/lambda-extension/examples/basic.rs @@ -1,6 +1,4 @@ use lambda_extension::{extension_fn, Error, NextEvent}; -use log::LevelFilter; -use simple_logger::SimpleLogger; async fn my_extension(event: NextEvent) -> Result<(), Error> { match event { @@ -16,9 +14,16 @@ async fn my_extension(event: NextEvent) -> Result<(), Error> { #[tokio::main] async fn main() -> Result<(), Error> { - // required to enable CloudWatch error logging by the runtime - // can be replaced with any other method of initializing `log` - SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); + // The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber` + // While `tracing` is used internally, `log` can be used as well if preferred. + tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + // this needs to be set to false, otherwise ANSI color codes will + // show up in a confusing manner in CloudWatch logs. + .with_ansi(false) + // disabling time is handy because CloudWatch will add the ingestion time. + .without_time() + .init(); let func = extension_fn(my_extension); lambda_extension::run(func).await diff --git a/lambda-extension/examples/custom_events.rs b/lambda-extension/examples/custom_events.rs index 88f040aa..cd3dae46 100644 --- a/lambda-extension/examples/custom_events.rs +++ b/lambda-extension/examples/custom_events.rs @@ -1,6 +1,4 @@ use lambda_extension::{extension_fn, Error, NextEvent, Runtime}; -use log::LevelFilter; -use simple_logger::SimpleLogger; async fn my_extension(event: NextEvent) -> Result<(), Error> { match event { @@ -18,9 +16,16 @@ async fn my_extension(event: NextEvent) -> Result<(), Error> { #[tokio::main] async fn main() -> Result<(), Error> { - // required to enable CloudWatch error logging by the runtime - // can be replaced with any other method of initializing `log` - SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); + // The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber` + // While `tracing` is used internally, `log` can be used as well if preferred. + tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + // this needs to be set to false, otherwise ANSI color codes will + // show up in a confusing manner in CloudWatch logs. + .with_ansi(false) + // disabling time is handy because CloudWatch will add the ingestion time. + .without_time() + .init(); let func = extension_fn(my_extension); diff --git a/lambda-extension/examples/custom_trait_implementation.rs b/lambda-extension/examples/custom_trait_implementation.rs index caef7730..910a7e07 100644 --- a/lambda-extension/examples/custom_trait_implementation.rs +++ b/lambda-extension/examples/custom_trait_implementation.rs @@ -1,6 +1,4 @@ use lambda_extension::{run, Error, Extension, InvokeEvent, NextEvent}; -use log::LevelFilter; -use simple_logger::SimpleLogger; use std::{ future::{ready, Future}, pin::Pin, @@ -28,9 +26,16 @@ impl Extension for MyExtension { #[tokio::main] async fn main() -> Result<(), Error> { - // required to enable CloudWatch error logging by the runtime - // can be replaced with any other method of initializing `log` - SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); + // The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber` + // While `tracing` is used internally, `log` can be used as well if preferred. + tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + // this needs to be set to false, otherwise ANSI color codes will + // show up in a confusing manner in CloudWatch logs. + .with_ansi(false) + // disabling time is handy because CloudWatch will add the ingestion time. + .without_time() + .init(); run(MyExtension::default()).await } diff --git a/lambda-integration-tests/Cargo.toml b/lambda-integration-tests/Cargo.toml index b250e911..eafea4d2 100644 --- a/lambda-integration-tests/Cargo.toml +++ b/lambda-integration-tests/Cargo.toml @@ -15,7 +15,7 @@ readme = "../README.md" lambda_http = { path = "../lambda-http", version = "0.4.1" } lambda_runtime = { path = "../lambda-runtime", version = "0.4.1" } lambda_extension = { path = "../lambda-extension", version = "0.1.0" } -log = "0.4" serde = { version = "1", features = ["derive"] } -simple_logger = { version = "1.15", default-features = false } -tokio = { version = "1", features = ["full"] } \ No newline at end of file +tokio = { version = "1", features = ["full"] } +tracing = { version = "0.1", features = ["log"] } +tracing-subscriber = "0.3" \ No newline at end of file diff --git a/lambda-integration-tests/src/bin/extension-fn.rs b/lambda-integration-tests/src/bin/extension-fn.rs index ad641d6c..fabdb4cd 100644 --- a/lambda-integration-tests/src/bin/extension-fn.rs +++ b/lambda-integration-tests/src/bin/extension-fn.rs @@ -1,6 +1,5 @@ use lambda_extension::{extension_fn, Error, NextEvent}; -use log::{info, LevelFilter}; -use simple_logger::SimpleLogger; +use tracing::info; async fn my_extension(event: NextEvent) -> Result<(), Error> { match event { @@ -17,7 +16,16 @@ async fn my_extension(event: NextEvent) -> Result<(), Error> { #[tokio::main] async fn main() -> Result<(), Error> { - SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); + // The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber` + // While `tracing` is used internally, `log` can be used as well if preferred. + tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + // this needs to be set to false, otherwise ANSI color codes will + // show up in a confusing manner in CloudWatch logs. + .with_ansi(false) + // disabling time is handy because CloudWatch will add the ingestion time. + .without_time() + .init(); lambda_extension::run(extension_fn(my_extension)).await } diff --git a/lambda-integration-tests/src/bin/extension-trait.rs b/lambda-integration-tests/src/bin/extension-trait.rs index de9031e6..62ab2dc7 100644 --- a/lambda-integration-tests/src/bin/extension-trait.rs +++ b/lambda-integration-tests/src/bin/extension-trait.rs @@ -1,10 +1,9 @@ use lambda_extension::{Error, Extension, NextEvent}; -use log::{info, LevelFilter}; -use simple_logger::SimpleLogger; use std::{ future::{ready, Future}, pin::Pin, }; +use tracing::info; #[derive(Default)] struct MyExtension { @@ -31,7 +30,16 @@ impl Extension for MyExtension { #[tokio::main] async fn main() -> Result<(), Error> { - SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); + // The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber` + // While `tracing` is used internally, `log` can be used as well if preferred. + tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + // this needs to be set to false, otherwise ANSI color codes will + // show up in a confusing manner in CloudWatch logs. + .with_ansi(false) + // disabling time is handy because CloudWatch will add the ingestion time. + .without_time() + .init(); lambda_extension::run(MyExtension::default()).await } diff --git a/lambda-integration-tests/src/bin/http-fn.rs b/lambda-integration-tests/src/bin/http-fn.rs index 26be7535..23b4961c 100644 --- a/lambda-integration-tests/src/bin/http-fn.rs +++ b/lambda-integration-tests/src/bin/http-fn.rs @@ -2,8 +2,7 @@ use lambda_http::{ lambda_runtime::{self, Context, Error}, IntoResponse, Request, Response, }; -use log::{info, LevelFilter}; -use simple_logger::SimpleLogger; +use tracing::info; async fn handler(event: Request, _context: Context) -> Result { info!("[http-fn] Received event {} {}", event.method(), event.uri().path()); @@ -13,7 +12,16 @@ async fn handler(event: Request, _context: Context) -> Result Result<(), Error> { - SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); + // The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber` + // While `tracing` is used internally, `log` can be used as well if preferred. + tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + // this needs to be set to false, otherwise ANSI color codes will + // show up in a confusing manner in CloudWatch logs. + .with_ansi(false) + // disabling time is handy because CloudWatch will add the ingestion time. + .without_time() + .init(); lambda_runtime::run(lambda_http::handler(handler)).await } diff --git a/lambda-integration-tests/src/bin/runtime-fn.rs b/lambda-integration-tests/src/bin/runtime-fn.rs index 37057aef..327316c3 100644 --- a/lambda-integration-tests/src/bin/runtime-fn.rs +++ b/lambda-integration-tests/src/bin/runtime-fn.rs @@ -1,7 +1,6 @@ use lambda_runtime::{handler_fn, Context, Error}; -use log::{info, LevelFilter}; use serde::{Deserialize, Serialize}; -use simple_logger::SimpleLogger; +use tracing::info; #[derive(Deserialize, Debug)] struct Request { @@ -23,7 +22,16 @@ async fn handler(event: Request, _context: Context) -> Result { #[tokio::main] async fn main() -> Result<(), Error> { - SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); + // The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber` + // While `tracing` is used internally, `log` can be used as well if preferred. + tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + // this needs to be set to false, otherwise ANSI color codes will + // show up in a confusing manner in CloudWatch logs. + .with_ansi(false) + // disabling time is handy because CloudWatch will add the ingestion time. + .without_time() + .init(); lambda_runtime::run(handler_fn(handler)).await } diff --git a/lambda-integration-tests/src/bin/runtime-trait.rs b/lambda-integration-tests/src/bin/runtime-trait.rs index e1de6bb1..f7175d09 100644 --- a/lambda-integration-tests/src/bin/runtime-trait.rs +++ b/lambda-integration-tests/src/bin/runtime-trait.rs @@ -1,11 +1,10 @@ use lambda_runtime::{Context, Error, Handler}; -use log::{info, LevelFilter}; use serde::{Deserialize, Serialize}; -use simple_logger::SimpleLogger; use std::{ future::{ready, Future}, pin::Pin, }; +use tracing::info; #[derive(Deserialize, Debug)] struct Request { @@ -37,7 +36,14 @@ impl Handler for MyHandler { #[tokio::main] async fn main() -> Result<(), Error> { - SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); + tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + // this needs to be set to false, otherwise ANSI color codes will + // show up in a confusing manner in CloudWatch logs. + .with_ansi(false) + // disabling time is handy because CloudWatch will add the ingestion time. + .without_time() + .init(); lambda_runtime::run(MyHandler::default()).await } diff --git a/lambda-runtime/Cargo.toml b/lambda-runtime/Cargo.toml index 3e567ed9..5bf36f13 100644 --- a/lambda-runtime/Cargo.toml +++ b/lambda-runtime/Cargo.toml @@ -28,6 +28,5 @@ tokio-stream = "0.1.2" lambda_runtime_api_client = { version = "0.4", path = "../lambda-runtime-api-client" } [dev-dependencies] -simple_logger = "1.6.0" -log = "^0.4" +tracing-subscriber = "0.3" simple-error = "0.2" diff --git a/lambda-runtime/examples/basic.rs b/lambda-runtime/examples/basic.rs index d74e10de..d4c962a7 100644 --- a/lambda-runtime/examples/basic.rs +++ b/lambda-runtime/examples/basic.rs @@ -2,9 +2,7 @@ // { "command": "do something" } use lambda_runtime::{handler_fn, Context, Error}; -use log::LevelFilter; use serde::{Deserialize, Serialize}; -use simple_logger::SimpleLogger; /// This is also a made-up example. Requests come into the runtime as unicode /// strings in json format, which can map to any structure that implements `serde::Deserialize` @@ -26,9 +24,14 @@ struct Response { #[tokio::main] async fn main() -> Result<(), Error> { - // required to enable CloudWatch error logging by the runtime - // can be replaced with any other method of initializing `log` - SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); + tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + // this needs to be set to false, otherwise ANSI color codes will + // show up in a confusing manner in CloudWatch logs. + .with_ansi(false) + // disabling time is handy because CloudWatch will add the ingestion time. + .without_time() + .init(); let func = handler_fn(my_handler); lambda_runtime::run(func).await?; diff --git a/lambda-runtime/examples/error-handling.rs b/lambda-runtime/examples/error-handling.rs index a0683f21..0957f4ce 100644 --- a/lambda-runtime/examples/error-handling.rs +++ b/lambda-runtime/examples/error-handling.rs @@ -1,9 +1,7 @@ /// See https://github.com/awslabs/aws-lambda-rust-runtime for more info on Rust runtime for AWS Lambda use lambda_runtime::{handler_fn, Error}; -use log::LevelFilter; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; -use simple_logger::SimpleLogger; use std::fs::File; /// A simple Lambda request structure with just one field @@ -51,13 +49,8 @@ impl std::fmt::Display for CustomError { #[tokio::main] async fn main() -> Result<(), Error> { - // The runtime logging can be enabled here by initializing `log` with `simple_logger` - // or another compatible crate. The runtime is using `tracing` internally. - // You can comment out the `simple_logger` init line and uncomment the following block to - // use `tracing` in the handler function. - // - SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); - /* + // The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber` + // While `tracing` is used internally, `log` can be used as well if preferred. tracing_subscriber::fmt() .with_max_level(tracing::Level::INFO) // this needs to be set to false, otherwise ANSI color codes will @@ -66,7 +59,6 @@ async fn main() -> Result<(), Error> { // disabling time is handy because CloudWatch will add the ingestion time. .without_time() .init(); - */ // call the actual handler of the request let func = handler_fn(func); diff --git a/lambda-runtime/examples/shared_resource.rs b/lambda-runtime/examples/shared_resource.rs index 64fb2e62..8110c15c 100644 --- a/lambda-runtime/examples/shared_resource.rs +++ b/lambda-runtime/examples/shared_resource.rs @@ -5,9 +5,7 @@ // { "command": "do something" } use lambda_runtime::{handler_fn, Context, Error}; -use log::LevelFilter; use serde::{Deserialize, Serialize}; -use simple_logger::SimpleLogger; /// This is also a made-up example. Requests come into the runtime as unicode /// strings in json format, which can map to any structure that implements `serde::Deserialize` @@ -47,8 +45,14 @@ impl SharedClient { #[tokio::main] async fn main() -> Result<(), Error> { // required to enable CloudWatch error logging by the runtime - // can be replaced with any other method of initializing `log` - SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); + tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + // this needs to be set to false, otherwise ANSI color codes will + // show up in a confusing manner in CloudWatch logs. + .with_ansi(false) + // disabling time is handy because CloudWatch will add the ingestion time. + .without_time() + .init(); let client = SharedClient::new("Shared Client 1 (perhaps a database)"); let client_ref = &client;