Skip to content

Commit 5e324ae

Browse files
authored
Change examples to use tracing-subscriber for logging (#389)
1 parent 6da8fe9 commit 5e324ae

14 files changed

+106
-56
lines changed

lambda-extension/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,5 @@ tokio-stream = "0.1.2"
2424
lambda_runtime_api_client = { version = "0.4", path = "../lambda-runtime-api-client" }
2525

2626
[dev-dependencies]
27-
simple_logger = "1.6.0"
28-
log = "^0.4"
2927
simple-error = "0.2"
28+
tracing-subscriber = "0.3"

lambda-extension/examples/basic.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use lambda_extension::{extension_fn, Error, NextEvent};
2-
use log::LevelFilter;
3-
use simple_logger::SimpleLogger;
42

53
async fn my_extension(event: NextEvent) -> Result<(), Error> {
64
match event {
@@ -16,9 +14,16 @@ async fn my_extension(event: NextEvent) -> Result<(), Error> {
1614

1715
#[tokio::main]
1816
async fn main() -> Result<(), Error> {
19-
// required to enable CloudWatch error logging by the runtime
20-
// can be replaced with any other method of initializing `log`
21-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
17+
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
18+
// While `tracing` is used internally, `log` can be used as well if preferred.
19+
tracing_subscriber::fmt()
20+
.with_max_level(tracing::Level::INFO)
21+
// this needs to be set to false, otherwise ANSI color codes will
22+
// show up in a confusing manner in CloudWatch logs.
23+
.with_ansi(false)
24+
// disabling time is handy because CloudWatch will add the ingestion time.
25+
.without_time()
26+
.init();
2227

2328
let func = extension_fn(my_extension);
2429
lambda_extension::run(func).await

lambda-extension/examples/custom_events.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use lambda_extension::{extension_fn, Error, NextEvent, Runtime};
2-
use log::LevelFilter;
3-
use simple_logger::SimpleLogger;
42

53
async fn my_extension(event: NextEvent) -> Result<(), Error> {
64
match event {
@@ -18,9 +16,16 @@ async fn my_extension(event: NextEvent) -> Result<(), Error> {
1816

1917
#[tokio::main]
2018
async fn main() -> Result<(), Error> {
21-
// required to enable CloudWatch error logging by the runtime
22-
// can be replaced with any other method of initializing `log`
23-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
19+
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
20+
// While `tracing` is used internally, `log` can be used as well if preferred.
21+
tracing_subscriber::fmt()
22+
.with_max_level(tracing::Level::INFO)
23+
// this needs to be set to false, otherwise ANSI color codes will
24+
// show up in a confusing manner in CloudWatch logs.
25+
.with_ansi(false)
26+
// disabling time is handy because CloudWatch will add the ingestion time.
27+
.without_time()
28+
.init();
2429

2530
let func = extension_fn(my_extension);
2631

lambda-extension/examples/custom_trait_implementation.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use lambda_extension::{run, Error, Extension, InvokeEvent, NextEvent};
2-
use log::LevelFilter;
3-
use simple_logger::SimpleLogger;
42
use std::{
53
future::{ready, Future},
64
pin::Pin,
@@ -28,9 +26,16 @@ impl Extension for MyExtension {
2826

2927
#[tokio::main]
3028
async fn main() -> Result<(), Error> {
31-
// required to enable CloudWatch error logging by the runtime
32-
// can be replaced with any other method of initializing `log`
33-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
29+
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
30+
// While `tracing` is used internally, `log` can be used as well if preferred.
31+
tracing_subscriber::fmt()
32+
.with_max_level(tracing::Level::INFO)
33+
// this needs to be set to false, otherwise ANSI color codes will
34+
// show up in a confusing manner in CloudWatch logs.
35+
.with_ansi(false)
36+
// disabling time is handy because CloudWatch will add the ingestion time.
37+
.without_time()
38+
.init();
3439

3540
run(MyExtension::default()).await
3641
}

lambda-integration-tests/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ readme = "../README.md"
1515
lambda_http = { path = "../lambda-http", version = "0.4.1" }
1616
lambda_runtime = { path = "../lambda-runtime", version = "0.4.1" }
1717
lambda_extension = { path = "../lambda-extension", version = "0.1.0" }
18-
log = "0.4"
1918
serde = { version = "1", features = ["derive"] }
20-
simple_logger = { version = "1.15", default-features = false }
21-
tokio = { version = "1", features = ["full"] }
19+
tokio = { version = "1", features = ["full"] }
20+
tracing = { version = "0.1", features = ["log"] }
21+
tracing-subscriber = "0.3"

lambda-integration-tests/src/bin/extension-fn.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use lambda_extension::{extension_fn, Error, NextEvent};
2-
use log::{info, LevelFilter};
3-
use simple_logger::SimpleLogger;
2+
use tracing::info;
43

54
async fn my_extension(event: NextEvent) -> Result<(), Error> {
65
match event {
@@ -17,7 +16,16 @@ async fn my_extension(event: NextEvent) -> Result<(), Error> {
1716

1817
#[tokio::main]
1918
async fn main() -> Result<(), Error> {
20-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
19+
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
20+
// While `tracing` is used internally, `log` can be used as well if preferred.
21+
tracing_subscriber::fmt()
22+
.with_max_level(tracing::Level::INFO)
23+
// this needs to be set to false, otherwise ANSI color codes will
24+
// show up in a confusing manner in CloudWatch logs.
25+
.with_ansi(false)
26+
// disabling time is handy because CloudWatch will add the ingestion time.
27+
.without_time()
28+
.init();
2129

2230
lambda_extension::run(extension_fn(my_extension)).await
2331
}

lambda-integration-tests/src/bin/extension-trait.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use lambda_extension::{Error, Extension, NextEvent};
2-
use log::{info, LevelFilter};
3-
use simple_logger::SimpleLogger;
42
use std::{
53
future::{ready, Future},
64
pin::Pin,
75
};
6+
use tracing::info;
87

98
#[derive(Default)]
109
struct MyExtension {
@@ -31,7 +30,16 @@ impl Extension for MyExtension {
3130

3231
#[tokio::main]
3332
async fn main() -> Result<(), Error> {
34-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
33+
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
34+
// While `tracing` is used internally, `log` can be used as well if preferred.
35+
tracing_subscriber::fmt()
36+
.with_max_level(tracing::Level::INFO)
37+
// this needs to be set to false, otherwise ANSI color codes will
38+
// show up in a confusing manner in CloudWatch logs.
39+
.with_ansi(false)
40+
// disabling time is handy because CloudWatch will add the ingestion time.
41+
.without_time()
42+
.init();
3543

3644
lambda_extension::run(MyExtension::default()).await
3745
}

lambda-integration-tests/src/bin/http-fn.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use lambda_http::{
22
lambda_runtime::{self, Context, Error},
33
IntoResponse, Request, Response,
44
};
5-
use log::{info, LevelFilter};
6-
use simple_logger::SimpleLogger;
5+
use tracing::info;
76

87
async fn handler(event: Request, _context: Context) -> Result<impl IntoResponse, Error> {
98
info!("[http-fn] Received event {} {}", event.method(), event.uri().path());
@@ -13,7 +12,16 @@ async fn handler(event: Request, _context: Context) -> Result<impl IntoResponse,
1312

1413
#[tokio::main]
1514
async fn main() -> Result<(), Error> {
16-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
15+
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
16+
// While `tracing` is used internally, `log` can be used as well if preferred.
17+
tracing_subscriber::fmt()
18+
.with_max_level(tracing::Level::INFO)
19+
// this needs to be set to false, otherwise ANSI color codes will
20+
// show up in a confusing manner in CloudWatch logs.
21+
.with_ansi(false)
22+
// disabling time is handy because CloudWatch will add the ingestion time.
23+
.without_time()
24+
.init();
1725

1826
lambda_runtime::run(lambda_http::handler(handler)).await
1927
}

lambda-integration-tests/src/bin/runtime-fn.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use lambda_runtime::{handler_fn, Context, Error};
2-
use log::{info, LevelFilter};
32
use serde::{Deserialize, Serialize};
4-
use simple_logger::SimpleLogger;
3+
use tracing::info;
54

65
#[derive(Deserialize, Debug)]
76
struct Request {
@@ -23,7 +22,16 @@ async fn handler(event: Request, _context: Context) -> Result<Response, Error> {
2322

2423
#[tokio::main]
2524
async fn main() -> Result<(), Error> {
26-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
25+
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
26+
// While `tracing` is used internally, `log` can be used as well if preferred.
27+
tracing_subscriber::fmt()
28+
.with_max_level(tracing::Level::INFO)
29+
// this needs to be set to false, otherwise ANSI color codes will
30+
// show up in a confusing manner in CloudWatch logs.
31+
.with_ansi(false)
32+
// disabling time is handy because CloudWatch will add the ingestion time.
33+
.without_time()
34+
.init();
2735

2836
lambda_runtime::run(handler_fn(handler)).await
2937
}

lambda-integration-tests/src/bin/runtime-trait.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use lambda_runtime::{Context, Error, Handler};
2-
use log::{info, LevelFilter};
32
use serde::{Deserialize, Serialize};
4-
use simple_logger::SimpleLogger;
53
use std::{
64
future::{ready, Future},
75
pin::Pin,
86
};
7+
use tracing::info;
98

109
#[derive(Deserialize, Debug)]
1110
struct Request {
@@ -37,7 +36,14 @@ impl Handler<Request, Response> for MyHandler {
3736

3837
#[tokio::main]
3938
async fn main() -> Result<(), Error> {
40-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
39+
tracing_subscriber::fmt()
40+
.with_max_level(tracing::Level::INFO)
41+
// this needs to be set to false, otherwise ANSI color codes will
42+
// show up in a confusing manner in CloudWatch logs.
43+
.with_ansi(false)
44+
// disabling time is handy because CloudWatch will add the ingestion time.
45+
.without_time()
46+
.init();
4147

4248
lambda_runtime::run(MyHandler::default()).await
4349
}

lambda-runtime/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@ tokio-stream = "0.1.2"
2828
lambda_runtime_api_client = { version = "0.4", path = "../lambda-runtime-api-client" }
2929

3030
[dev-dependencies]
31-
simple_logger = "1.6.0"
32-
log = "^0.4"
31+
tracing-subscriber = "0.3"
3332
simple-error = "0.2"

lambda-runtime/examples/basic.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
// { "command": "do something" }
33

44
use lambda_runtime::{handler_fn, Context, Error};
5-
use log::LevelFilter;
65
use serde::{Deserialize, Serialize};
7-
use simple_logger::SimpleLogger;
86

97
/// This is also a made-up example. Requests come into the runtime as unicode
108
/// strings in json format, which can map to any structure that implements `serde::Deserialize`
@@ -26,9 +24,14 @@ struct Response {
2624

2725
#[tokio::main]
2826
async fn main() -> Result<(), Error> {
29-
// required to enable CloudWatch error logging by the runtime
30-
// can be replaced with any other method of initializing `log`
31-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
27+
tracing_subscriber::fmt()
28+
.with_max_level(tracing::Level::INFO)
29+
// this needs to be set to false, otherwise ANSI color codes will
30+
// show up in a confusing manner in CloudWatch logs.
31+
.with_ansi(false)
32+
// disabling time is handy because CloudWatch will add the ingestion time.
33+
.without_time()
34+
.init();
3235

3336
let func = handler_fn(my_handler);
3437
lambda_runtime::run(func).await?;

lambda-runtime/examples/error-handling.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/// See https://github.com/awslabs/aws-lambda-rust-runtime for more info on Rust runtime for AWS Lambda
22
use lambda_runtime::{handler_fn, Error};
3-
use log::LevelFilter;
43
use serde::{Deserialize, Serialize};
54
use serde_json::{json, Value};
6-
use simple_logger::SimpleLogger;
75
use std::fs::File;
86

97
/// A simple Lambda request structure with just one field
@@ -51,13 +49,8 @@ impl std::fmt::Display for CustomError {
5149

5250
#[tokio::main]
5351
async fn main() -> Result<(), Error> {
54-
// The runtime logging can be enabled here by initializing `log` with `simple_logger`
55-
// or another compatible crate. The runtime is using `tracing` internally.
56-
// You can comment out the `simple_logger` init line and uncomment the following block to
57-
// use `tracing` in the handler function.
58-
//
59-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
60-
/*
52+
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
53+
// While `tracing` is used internally, `log` can be used as well if preferred.
6154
tracing_subscriber::fmt()
6255
.with_max_level(tracing::Level::INFO)
6356
// this needs to be set to false, otherwise ANSI color codes will
@@ -66,7 +59,6 @@ async fn main() -> Result<(), Error> {
6659
// disabling time is handy because CloudWatch will add the ingestion time.
6760
.without_time()
6861
.init();
69-
*/
7062

7163
// call the actual handler of the request
7264
let func = handler_fn(func);

lambda-runtime/examples/shared_resource.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
// { "command": "do something" }
66

77
use lambda_runtime::{handler_fn, Context, Error};
8-
use log::LevelFilter;
98
use serde::{Deserialize, Serialize};
10-
use simple_logger::SimpleLogger;
119

1210
/// This is also a made-up example. Requests come into the runtime as unicode
1311
/// strings in json format, which can map to any structure that implements `serde::Deserialize`
@@ -47,8 +45,14 @@ impl SharedClient {
4745
#[tokio::main]
4846
async fn main() -> Result<(), Error> {
4947
// required to enable CloudWatch error logging by the runtime
50-
// can be replaced with any other method of initializing `log`
51-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
48+
tracing_subscriber::fmt()
49+
.with_max_level(tracing::Level::INFO)
50+
// this needs to be set to false, otherwise ANSI color codes will
51+
// show up in a confusing manner in CloudWatch logs.
52+
.with_ansi(false)
53+
// disabling time is handy because CloudWatch will add the ingestion time.
54+
.without_time()
55+
.init();
5256

5357
let client = SharedClient::new("Shared Client 1 (perhaps a database)");
5458
let client_ref = &client;

0 commit comments

Comments
 (0)