From 15564bff308ef43f4cf358087ac8aa5b02c5f04d Mon Sep 17 00:00:00 2001 From: Simon Walker Date: Fri, 24 Dec 2021 00:19:32 +0000 Subject: [PATCH] lambda-extension: remove logging When running on AWS lambda I encountered the following error: ``` thread 'main' panicked at 'Could not determine the UTC offset on this system. Possible causes are that the time crate does not implement "local_offset_at" on your system, or that you are running in a multi-threaded environment and the time crate is returning "None" from "local_offset_at" to avoid unsafe behaviour. See the time crate's documentation for more information. (https://time-rs.github.io/internal-api/time/index.html#feature-flags): IndeterminateOffset', /home/simon/.cargo/registry/src/github.com-1ecc6299db9ec823/simple_logger-1.16.0/src/lib.rs:409:85 ``` This means that the simple logger setup could not find the current time. Instead, we can print to stdout which is also collected by CloudWatch. We can leave more sophisticated control of the output to the user. --- lambda-extension/Cargo.toml | 2 -- lambda-extension/README.md | 11 +++-------- lambda-extension/examples/basic.rs | 6 ------ lambda-extension/examples/custom_events.rs | 6 ------ .../examples/custom_trait_implementation.rs | 6 ------ 5 files changed, 3 insertions(+), 28 deletions(-) diff --git a/lambda-extension/Cargo.toml b/lambda-extension/Cargo.toml index f5b101c1..10c65e40 100644 --- a/lambda-extension/Cargo.toml +++ b/lambda-extension/Cargo.toml @@ -24,6 +24,4 @@ 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" diff --git a/lambda-extension/README.md b/lambda-extension/README.md index 4982779f..0dc297d9 100644 --- a/lambda-extension/README.md +++ b/lambda-extension/README.md @@ -10,17 +10,14 @@ The code below creates a simple extension that's registered to every `INVOKE` an ```rust,no_run use lambda_extension::{extension_fn, Error, NextEvent}; -use log::LevelFilter; -use simple_logger::SimpleLogger; -use tracing::info; async fn log_extension(event: NextEvent) -> Result<(), Error> { match event { NextEvent::Shutdown(event) => { - info!("{}", event); + println!("Shutdown {:?}", event); } NextEvent::Invoke(event) => { - info!("{}", event); + println!("Invoke {:?}", event); } } Ok(()) @@ -28,8 +25,6 @@ async fn log_extension(event: NextEvent) -> Result<(), Error> { #[tokio::main] async fn main() -> Result<(), Error> { - SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); - let func = extension_fn(log_extension); lambda_extension::run(func).await } @@ -55,4 +50,4 @@ Then, you can compile the extension against that target: $ cargo build -p lambda_extension --example basic --release --target x86_64-unknown-linux-musl ``` -This previous command will generate a binary file in `target/x86_64-unknown-linux-musl/release/examples` called `basic`. When the extension is registered with the [Runtime Extensions API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html#runtimes-extensions-api-reg), that's the name that the extension will be registered with. If you want to register the extension with a different name, you only have to rename this binary file and deploy it with the new name. \ No newline at end of file +This previous command will generate a binary file in `target/x86_64-unknown-linux-musl/release/examples` called `basic`. When the extension is registered with the [Runtime Extensions API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html#runtimes-extensions-api-reg), that's the name that the extension will be registered with. If you want to register the extension with a different name, you only have to rename this binary file and deploy it with the new name. diff --git a/lambda-extension/examples/basic.rs b/lambda-extension/examples/basic.rs index 573b3281..af089d17 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,10 +14,6 @@ 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(); - 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..f8a116ef 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,10 +16,6 @@ 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(); - let func = extension_fn(my_extension); let runtime = Runtime::builder().with_events(&["SHUTDOWN"]).register().await?; diff --git a/lambda-extension/examples/custom_trait_implementation.rs b/lambda-extension/examples/custom_trait_implementation.rs index caef7730..6f42f437 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,5 @@ 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(); - run(MyExtension::default()).await }