Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change examples to use tracing-subscriber for logging #389

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lambda-extension/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
15 changes: 10 additions & 5 deletions lambda-extension/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down
15 changes: 10 additions & 5 deletions lambda-extension/examples/custom_events.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);

Expand Down
15 changes: 10 additions & 5 deletions lambda-extension/examples/custom_trait_implementation.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions lambda-integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
tokio = { version = "1", features = ["full"] }
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = "0.3"
14 changes: 11 additions & 3 deletions lambda-integration-tests/src/bin/extension-fn.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
14 changes: 11 additions & 3 deletions lambda-integration-tests/src/bin/extension-trait.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
14 changes: 11 additions & 3 deletions lambda-integration-tests/src/bin/http-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<impl IntoResponse, Error> {
info!("[http-fn] Received event {} {}", event.method(), event.uri().path());
Expand All @@ -13,7 +12,16 @@ async fn handler(event: Request, _context: Context) -> Result<impl IntoResponse,

#[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(lambda_http::handler(handler)).await
}
14 changes: 11 additions & 3 deletions lambda-integration-tests/src/bin/runtime-fn.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -23,7 +22,16 @@ async fn handler(event: Request, _context: Context) -> Result<Response, 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_runtime::run(handler_fn(handler)).await
}
12 changes: 9 additions & 3 deletions lambda-integration-tests/src/bin/runtime-trait.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -37,7 +36,14 @@ impl Handler<Request, Response> 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
}
3 changes: 1 addition & 2 deletions lambda-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
13 changes: 8 additions & 5 deletions lambda-runtime/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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?;
Expand Down
12 changes: 2 additions & 10 deletions lambda-runtime/examples/error-handling.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions lambda-runtime/examples/shared_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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;
Expand Down